473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

AJAX, PHP, Mysql simple query example anyone?

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

--
The USA Patriot Act is the most unpatriotic act in American history.
Feingold-Obama '08 - Because the Constitution isn't history,
It's the law.

Jun 21 '06 #1
7 3727
Ivan Marsh wrote:
I'm having a heck of a time wrapping mind around AJAX.
A good example that doesn't look like rocket science would be nice.


http://www.google.com/search?q=ajax%20hello%20world ?
Jun 21 '06 #2
Ivan Marsh wrote:
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.


Ivan,

The particular AJAX program actually consists of two parts: server and
client.
Server part is a PHP script that returns the array of data in XML,
JSON, or plain text (or any other you like) format. Client part sends
the request for the data using <IFRAME>, <script> tags or
xmlhttprequest object. Consider using dojotoolkit.org for client part
and just a plain text data format for server part.

Sincerely,
Alexander
http://www.alexatnet.com/

Jun 21 '06 #3
On or about 6/21/2006 5:15 PM, it came to pass that Ivan Marsh wrote:
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.in nerHTML
=\"".$text."\";
";
}

sajax_init();
#$sajax_debug_mode = 1;
sajax_export("VenueLook");
sajax_handle_client_request();


Jun 22 '06 #4
Ivan Marsh wrote:
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?
Hi Ivan,

AJAX has nothing to do with the query to the database.
Just try to call your script directly from your browser, with the right
QUERYstring, and see the result in your browser.
If that works allright, then use AJAX.

eg, first try:
http://www.example.com/getusername.php?userid=12


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.
A great startingpoint for AJAX:
www.w3schools.com
click on AJAX.
It has a crossbrowser approach.
Just study the example, build them yourself with a simple example, then do
the real job. :-)

Good luck.

Regard,
Erwin Moller

-thx


Jun 22 '06 #5
Ivan Marsh wrote:
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


You might consider checking out one of the many AJAX frameworks which would
greatly simplify using AJAX with PHP. I currently use Xajax, which works
gloriously.

http://www.xajaxproject.org

--
-- Edmond Dantes, CMC
And Now for something Completely Different:
http://door.HomeImprovementBase.com
http://tapping.CraftyMen.com
http://holiday-baskets.prosperitysprinkler.com
http://backgammon.MyInfiniteWealth.com
http://acetate.IndustrialMetalz.com
http://dish.GadgetRUs.com
http://Pain-Relief.sillylife.com
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jun 22 '06 #6
A great tutorial for this is:

http://www-128.ibm.com/developerwork...ikiaMasterAJAX

i think u need to register, but thats a small prize, this tutorial
series of 5 parts gives u solid knowledge about the subject.

Jun 22 '06 #7
HC
Hi Ivan,

Here's the code I've been working with. I do things a little bit
differently from what you're asking. Instead of dumping the data in XML
format, I have my PHP script print out Javascript that is evaluated by
the browser. This works well for webapps.

The code is fairly well commented, but feel free to post back if you
have any questions, or comments of your own :)

http://pastebin.com/726871

-HC

Ivan Marsh wrote:
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

Jun 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
0
by: Ivan Marsh | last post by:
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...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
1
by: bcochofel | last post by:
Hi, I'm using Perl CGI and HTML::Template to generate the following XML: ---------------------------------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?>...
6
jafarsalam
by: jafarsalam | last post by:
hi; I'm new to PHP and AJAX MySQL codes. I found a simple code for PHP and AJAX MySQL Database communication on : http://www.w3schools.com/php/php_ajax_database.asp I downloaded the code and...
30
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name',...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
5
by: MelindaM | last post by:
Hi guys, I created a form for searching through a parts library that I have stored in a MySQL database. I'm not new to web programming but this is my first time using PHP and Ajax. I have four...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.