Jump to content

PHP w/AJAX


joeyjgarcia

Recommended Posts

I wanted to use AJAX on the client side and PHP for the server side technology and I wanted to be able to have a user select many check boxes and send the values to the PHP code and the PHP code would then contact a web service to obtain some information and return the data to the AJAX client.

Question: Since AJAX is asynchronous how will PHP handle multiple requests fired at it? I know I could batch them up and send one batch of values, but I was wonder how PHP will handle multiple request to that same server-side php page. Would I need to use session on the server side to handle these?
Link to comment
Share on other sites

the script would be run once per request. just like pressing the submit button several times. the only difference between the ajax method and regular posting method is that ajax posts just the little piece of info, receives the results and updates one piece of the page, instead of the whole thing. All the other principles apply here. It's just on a smaller scale.

in other words:

if your php script were to look like this:

$x = $_REQUEST['x'];
$y = $_REQUEST['y'];
$z = $_REQUEST['z'];

$total = $x + $y + $z;

if you sent a request to the server containing all 3 of those variables, $total would be the sum of all 3 of them. However, if you did 3 seperate requests, one for each variable, $total would not be the sum of all 3. It would be 3 seperate instances of $total:

$total = $x + null + null;
$total = null + $y + null;
$total = null + null + $z;

unless you were to like, idk, setup some session variables or something. that might work the way you want it.
Link to comment
Share on other sites

Cool! Seems very straight forward.


I was thinking of creating a list of websites that I link to, I may want to select about 10 of them and then send the urls to my php page to ask Google about the page rank for each one. So you are saying that if I make 10 requests (for 10 selected urls), php won't know the difference and the AJAX will update the web page as the results come back in from the php one at a time.

BTW, I am aware of the daily limit that Google puts on their API requests.


Thanks for the help!
Link to comment
Share on other sites

[!--quoteo(post=381073:date=Jun 7 2006, 12:30 PM:name=joeyjgarcia)--][div class=\'quotetop\']QUOTE(joeyjgarcia @ Jun 7 2006, 12:30 PM) [snapback]381073[/snapback][/div][div class=\'quotemain\'][!--quotec--]
So you are saying that if I make 10 requests (for 10 selected urls), php won't know the difference and the AJAX will update the web page as the results come back in from the php one at a time.
[/quote]
if you program your ajax/js that way, then yes. php will treat each of the 10 ajax postings just like it would treat clicking a normal submit button 10 times.
Link to comment
Share on other sites

You could also try something like this (PHP5)

[code]
<?php
    $xml = simplexml_load_file('http://www.somedomain.com/somepage.php');
    $xsl = simpleXML_load_file('myformat.xsl');
    $proc = new XsltProcessor();
    $proc->importStylesheet($xsl);
    $newxml = $proc->transformToDoc($xml);
    print $newxml->saveXML();
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.