Jump to content


Photo

PHP w/AJAX


  • Please log in to reply
4 replies to this topic

#1 joeyjgarcia

joeyjgarcia
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 June 2006 - 02:53 PM

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?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 04:18 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 joeyjgarcia

joeyjgarcia
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 June 2006 - 05:30 PM

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!

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 06:19 PM

[!--quoteo(post=381073:date=Jun 7 2006, 12:30 PM:name=joeyjgarcia)--][div class=\'quotetop\']QUOTE(joeyjgarcia @ Jun 7 2006, 12:30 PM) View Post[/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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 June 2006 - 07:26 PM

You could also try something like this (PHP5)

<?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();
?> 

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users