Hey Folks,
I'm having a heck of a time wrapping mind around AJAX.
Anyone know of a simple, straight-forward example for pulling a simple
query from mysql with PHP using AJAX?
As I understand it I need a PHP script that pulls the query and dumps the
data into XML format, that is called by triggering a javascript event that
reads that file with XMLhttprequest.
Is there a cross-browser way of doing this or do I have to detect whether
I'm using IE or Mozilla?
A good example that doesn't look like rocket science would be nice.
-thx
Take a look here.
http://www.modernmethod.com/sajax/
Debugging AJAX code can be a bit of an issue but SAJAX does have some
capabilities that do help.
It works for me in IE, Firefox and Opera. Actually IMHO and I will may
get flamed for this by AJAX purists, I find the easiest method of using
this technology is to build Javascript in my PHP code, then Javascript
EVAL the result on the client. My example is pulled from a much larger
script.
Example: Update a select field. Let's assume I have a select box that I
want to contain selected venues/clubs etc from a large MySql table. The
SAJAX database search is triggered when 3 or more characters are keyed
into the search field, updating the Select field. Note: due to problems
with IE it is necessary to replace the complete Select HTML definition
or it wont work.
On the client side I have the following fields as part of a form
<INPUT TYPE="TEXT" id="Venue_Search" size="25" value="partial/full
venue" autocomplete="off" onkeyup="Venuesearch(this.value)"
onfocus="Venuesearch(this.value)" onblur="VenueReset(this.value)">
<SPAN><select style='width:282px' id='Venue_Id' Name='Venue_Id'><OPTION
VALUE=''></select></SPAN>
On the client side I have the following Javascript(SAJAX support scripts
not shown)
function show_result(result)
{
eval(result)
}
function Venuesearch ($Likedata)
{
//executes VenueLook on the server
$Likedata=$Likedata.replace(/\s*$/, ""); //dump trailing spaces
$Likedata=$Likedata.replace(/^\s*/, ""); //dump leading spaces
if ($Likedata == "partial/full venue")
document.getElementById('Venue_Search').value = ""
else
if ($Likedata.length > 2)
{
sajax_request_type = 'POST'
x_VenueLook(
$Likedata,
show_result);
}
}
function VenueReset ($Likedata)
{
$Likedata=$Likedata.replace(/\s*$/, ""); //dump trailing spaces
$Likedata=$Likedata.replace(/^\s*/, ""); //dump leading spaces
if ($Likedata == "")
document.getElementById('Venue_Search').value = "partial/full venue"
}
On the Server side I have the following PHP code
function VenueLook()
{
// Updates the Span around the select due to IE bug, including IE7
// Put locations starting with search argument first in select list
$Like = $_POST['rsargs'][0];
$query = "SELECT Id, Name, City, State, IF (`Name` LIKE '". $Like . "%',
'1', '2') AS Order_alias FROM `Venue` WHERE `Name` LIKE '%" . $Like .
"%' ORDER BY Order_alias, Name ASC";
$result = mysql_query( $query )
or die('Result is no good: ' . mysql_error());
$text= "<select style='width:282px' id='Venue_Id'
Name='Venue_Id'><OPTION VALUE=''>". mysql_num_rows($result) ."
Venues</option>";
while($i = mysql_fetch_row($result))
{
if (mysql_num_rows($result) == 1)
$text.= "<OPTION SELECTED VALUE='" . $i[0] . "'>" . $i[1] . " (".
$i[2] . " " . $i[3] . ")</option>";
else
$text.= "<OPTION VALUE='" . $i[0] . "'>" . $i[1] . " (". $i[2] . " " .
$i[3] . ")</option>";
}
$text.= "</select>";
//return $text as Javascript which is executed by the
//show_result script on the client side
return "document.getElementById('Venue_Id').parentNode.innerHTML
=\"".$text."\";
";
}
sajax_init();
#$sajax_debug_mode = 1;
sajax_export("VenueLook");
sajax_handle_client_request();