Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Understanding AJAX

 
   Database Help (Home) -> PHP RSS
Next:  Does $_POST survive script hopping?  
Author Message
KDawg44

External


Since: Jan 15, 2008
Posts: 23



(Msg. 1) Posted: Tue Feb 05, 2008 5:33 pm
Post subject: Understanding AJAX
Archived from groups: comp>lang>php (more info?)

Hi,

I am writing a PHP app and am trying to utilize AJAX with this. When
I use AJAX to call a PHP script, how do I tell the PHP script what I
want from it? Is there another way to do this other than either
having a different PHP script for every action or passing in a text
querystring to the PHP script and then doing some kind of switch
statement to figure it out?

Thanks for explaining this. Also, it seems like the only time it
makes sense to use the AJAX is if I want content to change without
refreshing the page, so something like having the date in the upper
corner of the page might as well be generated by PHP instead of
calling a PHP script with AJAX, right?

Thanks for your help.

Kevin

 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Tony

External


Since: Feb 05, 2008
Posts: 5



(Msg. 2) Posted: Tue Feb 05, 2008 5:42 pm
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

KDawg44 wrote:
> Hi,
>
> I am writing a PHP app and am trying to utilize AJAX with this. When
> I use AJAX to call a PHP script, how do I tell the PHP script what I
> want from it? Is there another way to do this other than either
> having a different PHP script for every action or passing in a text
> querystring to the PHP script and then doing some kind of switch
> statement to figure it out?

The PHP script that responds to the Ajax call can function in the same
way as any PHP script - you need SOME way to tell it what you want done,
so you're going to have to either pass parameters in, or call a
different script. Is there any other way to get a PHP page to know what
you want to do without having a different script or passing in a query
string?

Ah - yes, there is: You can submit data via POST. This is also possible
with Ajax, but it is somewhat more complicated, and rarely worthwhile, IMO.

> Thanks for explaining this. Also, it seems like the only time it
> makes sense to use the AJAX is if I want content to change without
> refreshing the page, so something like having the date in the upper
> corner of the page might as well be generated by PHP instead of
> calling a PHP script with AJAX, right?

That would seem to be the main point of Ajax Smile Seems like you get it
pretty well.

 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
KDawg44

External


Since: Jan 15, 2008
Posts: 23



(Msg. 3) Posted: Tue Feb 05, 2008 5:44 pm
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Feb 5, 8:42 pm, Tony <nos....RemoveThis@example.com> wrote:
> KDawg44 wrote:
> > Hi,
>
> > I am writing a PHP app and am trying to utilize AJAX with this. When
> > I use AJAX to call a PHP script, how do I tell the PHP script what I
> > want from it? Is there another way to do this other than either
> > having a different PHP script for every action or passing in a text
> > querystring to the PHP script and then doing some kind of switch
> > statement to figure it out?
>
> The PHP script that responds to the Ajax call can function in the same
> way as any PHP script - you need SOME way to tell it what you want done,
> so you're going to have to either pass parameters in, or call a
> different script. Is there any other way to get a PHP page to know what
> you want to do without having a different script or passing in a query
> string?
>
> Ah - yes, there is: You can submit data via POST. This is also possible
> with Ajax, but it is somewhat more complicated, and rarely worthwhile, IMO.
>
> > Thanks for explaining this. Also, it seems like the only time it
> > makes sense to use the AJAX is if I want content to change without
> > refreshing the page, so something like having the date in the upper
> > corner of the page might as well be generated by PHP instead of
> > calling a PHP script with AJAX, right?
>
> That would seem to be the main point of Ajax Smile Seems like you get it
> pretty well.

Thanks for the response! Always good when I get it... Smile

Thanks.
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Tony

External


Since: Feb 05, 2008
Posts: 5



(Msg. 4) Posted: Tue Feb 05, 2008 6:18 pm
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

KDawg44 wrote:
>
> Thanks for the response! Always good when I get it... Smile
>

One thing to bear in mind - which can be useful - is that cookies and
such are passed through on the Ajax call as well (for the most part,
it's not much different than a regular HTTP request), so you can use
session variables in the Ajax script if you need.
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Rob

External


Since: Jan 15, 2008
Posts: 25



(Msg. 5) Posted: Wed Feb 06, 2008 2:25 am
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Feb 6, 1:33 am, KDawg44 <KDaw....RemoveThis@gmail.com> wrote:
> Hi,
>
> I am writing a PHP app and am trying to utilize AJAX with this.  When
> I use AJAX to call a PHP script, how do I tell the PHP script what I
> want from it?  Is there another way to do this other than either
> having a different PHP script for every action or passing in a text
> querystring to the PHP script and then doing some kind of switch
> statement to figure it out?
>
> Thanks for explaining this.  Also, it seems like the only time it
> makes sense to use the AJAX is if I want content to change without
> refreshing the page, so something like having the date in the upper
> corner of the page might as well be generated by PHP instead of
> calling a PHP script with AJAX, right?
>
> Thanks for your help.
>
> Kevin

I would suggest taking the headache our of AJAX by using PROTOTYPE.
Below is a PHP example of this. As you can see, AJAX is invoked in a
single line 'new Ajax.Updater'.

It can also pass parameters to the PHP script, in this case as
parameter called 'task' that has the value 'details', and is picked up
by the PHP script :-


<?php
if (@$_POST['task'] == "details") {
echo "This is some text returned from the PHP engine via
AJAX";
exit;
}

?>

<html>

<head>
<title>Ajax Test</title>
<script src="prototype.js"></script>

<script>

function update_content() {
new Ajax.Updater('dynamic', 'ajaxtest.php', { method: 'post',
parameters: { task: 'details' } });
}
</script>
</head>

<body>

<table border="1" width="100%">
<tr>
<td width="33%" onclick="update_content()"><a href="#">Update</a></
td>
<td width="33%"> </td>
<td width="34%"> </td>
</tr>
<tr>
<td width="33%"> </td>
<td width="33%"><div id="dynamic">I am going to put some content
here</div></td>
<td width="34%"> </td>
</tr>
<tr>
<td width="33%"> </td>
<td width="33%"> </td>
<td width="34%"> </td>
</tr>
</table>

</body>

</html>
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Iván_Sánchez_Ortega

External


Since: Dec 27, 2007
Posts: 146



(Msg. 6) Posted: Wed Feb 06, 2008 2:48 am
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

KDawg44 wrote:

> I am writing a PHP app and am trying to utilize AJAX with this. When
> I use AJAX to call a PHP script, how do I tell the PHP script what I
> want from it?

When you visit a .php page with a web browser, how do you tell what do you
want from it?

- GET variables in the URL, like in "foobar.php?foo=foo&bar=baaz"
- POST variables (like a form), via HTTP
- Some other complicated mechanisms (cookies, custom headers, source IP)

So, now, guess how can you tell a .php script called from ajax (instead of
from a web browser) what fo you want from it.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Paco Fernández - Café del Mar, volumen doce (2005) - [5]
Junto al mar (6:39) (95.500000%)
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Alexey Kulentsov

External


Since: Feb 04, 2008
Posts: 30



(Msg. 7) Posted: Wed Feb 06, 2008 8:24 am
Post subject: What do you really need to know about AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

1. First of all, XMLHttpRequest have idiotic API. onreadystatechange
handler have no parameters! (Thanks to Microsoft) So if you have more
then one request on same page for different resources you need to have
separate handler for every request because event handler have no way to
determine which exactly object calls it.
2. It's impossible to have separate handler for every request if you
have variable amount of resources to request (for example tree with
dynamically requested branches). Thanks God javascript was developed by
wise programmers so in normal browsers this problem is solved using
javascript closures.
3. But in IE this is impossible due memory leak. IE garbage collector
can't remove circular references, and this problem is not solved even in
IE7. (Thanks Microsoft again)
4. So we have to make separate component called like 'AjaxChannel'
implementing queue of requests and other components must transfer data
throw it's API.
5. IE don't support readyState==3 so you can't use technique of endless
connection for server-generated events. You need to close down
connection after every server event and reopen it again after processing
data because in IE data will be available ONLY on readyState==4. One
more thank to Microsoft.
6. IE don't support national encodings in XMLHttpRequest so if you have
non-english site you have to use UTF-8 only. If site MUST BE in any
national encoding (like cp-1251 as in my case) you have to implement
recoding routines in javascript and PHP to convert your_site_encoding
<-> UTF-8. BIG thanks to Microsoft.
7. After all I recommend to make additional shutdown like this
(specially for IE again):

<script type="text/javascript" for="window" event="onunload">
// Hack for IE
try{downAjax.abort();}catch(e){}
downAjax=0;
</script>

downAjax is the XMLHttpRequest object. Do it for every XMLHttpRequest
object used.

Have nice ajax! Smile
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
henribaeyens

External


Since: Feb 05, 2008
Posts: 15



(Msg. 8) Posted: Wed Feb 06, 2008 9:13 am
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Tue, 05 Feb 2008 17:33:16 -0800, KDawg44 wrote:

> Hi,
>
> I am writing a PHP app and am trying to utilize AJAX with this. When
> use AJAX to call a PHP script, how do I tell the PHP script what I want
> from it? Is there another way to do this other than either having a
> different PHP script for every action or passing in a text querystring
> to the PHP script and then doing some kind of switch statement to figure
> it out?
>
> Thanks for explaining this. Also, it seems like the only time it makes
> sense to use the AJAX is if I want content to change without refreshing
> the page, so something like having the date in the upper corner of the
> page might as well be generated by PHP instead of calling a PHP script
> with AJAX, right?
>
> Thanks for your help.
>
> Kevin

you mean you want USE ajax. you just pass a query string, like

adiv.innerhtml = ajaxrequest('script.php?param1=this&param2=that');
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Tony

External


Since: Feb 05, 2008
Posts: 5



(Msg. 9) Posted: Wed Feb 06, 2008 12:56 pm
Post subject: Re: Understanding AJAX [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Rob wrote:
>
> I would suggest taking the headache our of AJAX by using PROTOTYPE.
> Below is a PHP example of this. As you can see, AJAX is invoked in a
> single line 'new Ajax.Updater'.

Not much headache to Ajax, really.
And there are much better libraries to use than Prototype, which was
really designed so that server-side programmers don't actually have to
learn javascript. Personally, I never understood why that's such a problem.

http://blog.metawrap.com/blog/CommentView,guid,42b961d5-b539-4d9a-b1e0...8e546ae
http://abcdefu.wordpress.com/2006/12/27/prototypejs/

Try jQuery or Yahoo if you really need a library.
 >> Stay informed about: Understanding AJAX 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
AJAX IDE and AJAX TOOL--The Release of JoyiStar AJAX WebSh.. - The JoyiStar AJAX WebShop development team is proud to announce the release of JoyiStar AJAX WebShop 3.0 Beta for Personal Edition. This release is a major improvement in the AJAX WebShop series, which includes a large number of new features and..

Help understanding 3 lines of code - Hi folks, can someone help me understand the meaning behind this snippet of code: $result = mysql_query('SELECT category_id,category_name FROM gallery_category'); while($row = mysql_fetch_array($result)) { $photo_category_list .= <<<__HTML...

Help understanding object-oriented approach - Hi, I've been using PHP for a long time, I have designed and developed a number of mid-range systems. I've always used a procedural approach. I fully understand the concept of OO, I know all the basics and I know how to code OO. What I'm having..

Understanding and Working With Classes (Example using MySQ.. - Hello, I am trying to learn how-to use Classes effectively and started working with an MySQL Wrapper Class. Problem being, I can't get it to work. Normally I'd do something like $Q = "SELECT *" ." FROM events&quot...

ajax help - i want to have a bunch of radio buttons i.e. O = radio button O cpu 1 O cpu 2 + 150 O cpu 3 + 500 when they click either or i want it to submit the form to be processed using php and AJAX, so that their total refreshes with out the page refreshing. ....
   Database Help (Home) -> PHP All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]