General AJAX Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
titeroy
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2010 12:21 pm

General AJAX Question

Post by titeroy »

Hi, first of all I hope th question doesn't sound too stupid but I am very new to php (but not to programming though).

#1. I need to modify a PHP-variable from inside a JavaScript.
#2. Let's assume it's the onchange-Event of a Combobox or the like.
#3. The JavaScript Code should do something and finally set a couple of local variables.

I thought I could do it with AJAX. And in fact I am as far as
#1-#3 is executed
When executing 3 I finally instantiate an ajax-object and open a php-Page:

Code: Select all

ajax.open('POST', 'http://localhost:6000/ajtest.php', true);
And still this works fine. But what do I do with this variable there????
I would need to get it into my mainscript in order to do something.

Well as a matter of fact I am able to write to a database within
ajtest.php. So I could verify that everything works and the correct data is being processed.

But how do I get this into my actual page (index.php).

And furthermore I don't think this can be the right way to pass some data from Javascript to PHP, by storing the data in a database.

Otherwise it would be easier to redirect the whole document to
something-or-other.php?foo=2
and passing the stuff via URL-parameters.

Ok, I understand that this is not applicable to big datasets, but somehow I think I missed the knack.

Thanks for your help in advance

Any help appreciated
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: General AJAX Question

Post by cpetercarter »

I have only done a limited amount of AJAX programming, and I am sure that others will have better and more detailed advice.

The steps are:
1 a call from Javascript (eg from your onchange event) to a php script on your server. You can pass information to the script using POST or GET variables.

2 the php script processes the information in whatever way is appropriate, and returns (typically using a simple echo or print statement) values to a javascript callback function. Here the php json_encode() function is your friend. It is not available on older versions of php, though a google search will find some scripts which mimic its functionality.

3 the callback function will typically modify the web page (modify the select box, display a message, add a text field etc)

The best thing is probably to find some code examples and work through them to see how they work. I have found that jquery's AJAX methods are really useful.
titeroy
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2010 12:21 pm

Re: General AJAX Question

Post by titeroy »

cpetercarter wrote:The best thing is probably to find some code examples and work through them to see how they work. I have found that jquery's AJAX methods are really useful.
Thanks for your answer.
somehow i am still missing a bit to make it work. I found some articles related to that but either they are incomplete or require some deeper knowledge on <Idnontknowwhat>.

Where do I find some more informatione on jquery's AJAX methods you mentioned?
TIA
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: General AJAX Question

Post by highjo »

Hi you can find jquery ajax method info here http://api.jquery.com/jQuery.ajax/.but i think it's a bit difficult to start working with it right away.try this one http://api.jquery.com/jQuery.post/ .basically it's using the same $.ajax function but then it's easy to use since you might not be very familiar with all the params you could pass for the $.ajax. i hope this helps
Post Reply