Click to See Complete Forum and Search --> : AJAX return data from MySQL to JS


ecksley
07-25-2007, 11:25 PM
So, I'm getting the hang of AJAX for making changes to my database using JS and PHP. I use th following basic structure which seem to work great when I want to remove items from the database, or pass new datat to is to edit it etc. ...


function revertElement(argument){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(confirm success);
= javascript variable name;
}
}

//Pass to PHP
var queryString = "?argument=" + argument ;
ajaxRequest.open("GET", "revertRows.php" + queryString, true);
ajaxRequest.send(null);
}


Now I would like to actually use JS to dial up a PHP file to query the database, and return some data back to the JS so it can do stuff with it. Seems doable, but I haven't found any examples on the web. I'm wondering is I should "POST" instead of "GET" or what needs to happen in the PHP to pass back a variable. An Echo? A Return? If so, where would it return to? would it be sort of an attribute of "ajaxRequest". Like "ajaxRequest.newvariable"?
I'm here with the PHP...


<?php
$link=mysql_connect ("localhost", "username", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");
$argument = $_GET['argument'];
$query = "SELECT * FROM Products WHERE id= $argument";
$results = mysql_query($query);

// Now what? Do I echo?
echo "results=Return Something To JS";

mysql_close($link);
?>


Thanks for your Thoughts. Hope this is a good forum for this discussion. Figured the pass back needed the most work and that's all PHP.

ecksley
07-26-2007, 12:07 AM
Found an interesting tutorial on this very thing here:

http://ajaximpact.com/ajaximpact.php?n=10&id=115&back=/detail_Tutorials_id_115_Retrieving_database_information_with_AJAX,_PHP_and_MySQL.html
Seems as though the PHP does Echo, and indeed the response is found in the "ajaxRequest". More specifically,


ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var description = ajaxRequest.responseText; //The content data which has been retrieved ***
if( description ){
alert(description);
}
}
}


Its not working exactly for me, but I'm getting closer.

ecksley
07-26-2007, 01:28 AM
The funny thing is when I test the PHP all by itself it echos the data correctly, but when returning the data to the JS I get the following:


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>406 Not Acceptable</TITLE>
</HEAD><BODY>
<H1>Not Acceptable</H1>
An appropriate representation of the requested resource /018/revertRows.php could not be found on this server.<P>
<HR>
<ADDRESS>Apache/1.3.37 Server at www.mydomain.com Port 80</ADDRESS>
</BODY></HTML>


Any thoughts on what I'm doing wrong?

ecksley
07-26-2007, 01:01 PM
In case anyone cares, I figured out my problem... I was using a "POST" where I should have been using a "GET". It was something I was experimenting with since my initial post and forgot to change back. Now it works like a charm.