Here is the sample code of how to make the call:
//first I am passing a variable that I call id, this would be the row id for the database table being used, for example: if I'm updating the record with the id of 4, I would call: javascript:dosomethingAjax('4');
//the id is passed to the file dosome.php that will handle whatever it is you are trying to do
//if you want to pass more variables through the ajax call you would use commas to separate them, for example: parameters: {u: id, name: 'John', age: 24}
function dosomethingAjax(id) {
var myAjax = new Ajax.Request('dosome.php',
{method: 'get', parameters: {u: id},
onComplete: handleResponse});
}
//once dosome.php gets processed, the ajax call is complete and the following function is called, in this case, I am putting the results inside a div with the id of displayResults
function handleResponse(transport) {
document.getElementById('displayResults').innerHTML = transport.responseText;
}
//here is how to setup the dosome.php
<?php
//the variable u is being passed by the ajax call listed above
$id = $_GET['u'];
//do something like update or insert or select
echo "Success!";
?>
[ add comment ] ( 23 views ) | permalink | related link |




( 3 / 190 )function ajax_get(ctnid) {
if (ua.indexOf('msie') != -1) {
ajax_get_request = new ActiveXObject("Msxml2.XMLHTTP");
} else {
ajax_get_request = new XMLHttpRequest();
}
ajax_get_request.onreadystatechange = function ajax_get_xml() {
if (ajax_get_request.readyState == 4) {
if (ajax_get_request.status == 200) {
var number_of_items = ajax_get_request.responseXML.getElementsByTagName("item").length;
var item_id = ajax_get_request.responseXML.getElementsByTagName("id");
if (number_of_ajax > 0) {
for (var i=0; i < number_of_items; i++) {
// do something
}
} else {
// do something
}
} else {
alert("There was an error while retrieving the URL: " + ajax_get_request.statusText);
}
}
return true;
}
ajax_get_request.open("POST", "/path/to/your/php/script", true);
ajax_get_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//ctnid is a variable that is being passed
ajax_get_request.send("ctnid="+ctnid);
}
Your php script would need to return a valid XML document. In the example, something like:
<items>
<item>
<id>1</id>
</item>
<item>
<id>2</id>
</item>
</items>
[ add comment ] ( 6 views ) | permalink |




( 3.2 / 208 )
Calendar


