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.
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.