PDA

View Full Version : Prototype's Ajax.Request object


spenoir
05-08-2007, 06:43 PM
Can anyone see anything wrong with this script or have any tips on using Prototype for ajax requests?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="prototype.js"></script>
<title>Prototype Form</title>

<script type="text/javascript">

//****************************************

var BtnSubmit = $("submit");


function ajaxcall () {
$("ajaxform").serialize();

new Ajax.Request('createxml.php',
{
method:'get',
onSuccess:
//Get xml node value, used below in parseMyData
function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

//Parse returned XML
function parseMyData(){

var items = responseXML.getElementsByTagName('item');

//alert(images.length);

if (!responseXML.documentElement && responseStream) {
responseXML.load(responseStream);
}
for (var i=0;i< items.length;i++)
{
var x = document.createElement('div');
x.className = 'holder';
var y = document.createElement('h3');
y.appendChild(document.createTextNode(getNodeValue(items[i],'title')));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'desc';
z.appendChild(document.createTextNode('Description: ' + getNodeValue(items[i],'desc')));
x.appendChild(z);
document.getElementById('container').appendChild(x);
}
}

onFailure: function(){ alert('Something went wrong...') }
});
}

</script>

</head>

<body>

<form action="javascript: ajaxcall();" method="get" name="ajaxform" id="ajaxform">
<fieldset><legend>Prototype test</legend>
<label for="title">Title:</label><input type="text" name="title" size="20">
<label for="description">Description</label><input type="text" name="description" size="20">
<input type="submit" id="submit" value="submit" />
</fieldset>
</form>

<div id="container">some XML?</div>

</body>
</html>

Nothing seems to display, although I have had the Ajax.Updater working. I'm sending the request to a php document which creates an xml file from a database. I've used this script before and it has worked. If I can't find anything wrong with my js i'll have a look at the php.

david_kw
05-08-2007, 07:24 PM
It looks like you have 2 functions instead of just one in your object parameter. Also it looks like you are missing a comma before onFailure as well.

david_kw

t0m
08-13-2007, 01:44 AM
I've tried everything I can think of to get my code working, and it doesn't work..I am using Prototype JS to do a chatroom, and I am using .PHP file's to get the users online, and the chat messages for today..Why doesn't <?php echo 'text'; ?> work with the ajax.request() of Prototype??

StupidRalph
08-17-2007, 01:45 PM
You need to start your own thread that thoroughly explains your situation. Your current post doesn't say much. Try posting snippets.