Forum Moderators: open

Message Too Old, No Replies

Newbie to AJAX

         

andrewsmd

9:53 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I literally just started using AJAX so bear with me. I am calling a PHP file with JS right now and I was wondering if anyone knows if I can pass anything into that PHP file. Basically I have a site that is something like this

job

Job name

notes

some notes
some more notes
yet more notes

Now each job has a unique ID in a database and I would like to display an edit button right by the notes for each job. Then, when the user clicks the edit button it goes to the corresponding job edit page. I have the php echoing hello world right on a button click so I am that far. My problem is that I don't know how I can distinguish between the different jobs i.e. if they click edit button for job1 or the edit button for job2 etc. Can I pass some data into a php file w/ JS somehow? So it would be something like
pseudo
onButtonClick(execute(somefile.php(passing in an id)));
If not, does anyone have any ideas how I would be able from the client side to tell which job it is? Thanks,

idev

9:58 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Hello Andrew,

Read this on using jquery for ajax:
[ibm.com...]

andrewsmd

7:05 pm on Mar 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything there that is what I am looking for. Am I overlooking something? Any other ideas or directions to point me in.

blang

10:57 pm on Mar 24, 2009 (gmt 0)

10+ Year Member



@andrewsmd>

Yes, it's totally possible to pass a query string via JavaScript to the PHP script, have the PHP script take those values and return some dynamic content (i.e. that specific record's data based on the `ID` value).

And while that article is a nice introduction to using jQuery's Ajax methods, it's not very clear on how to do what you want. If you look at "Listing 4", you can see how the POST method Ajax call sends some data to the script, but it doesn't express how you would use a GET string to retrieve some content. If you are interested in using jQuery (which I recommend), you can view the jQuery.get method examples [docs.jquery.com] in the jQuery API.

Otherwise, it's as simple as sending the query string to the URL, e.g.


// instantiate the xhr object
var xhr= ( window.XMLHttpRequest ) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
// the URL to send (note the value attached to it)
var URL= 'yourscript.php?id=' + theIDvalue;
// open the connection
xhr.open("GET", URL, true);
// callback function
xhr.onreadystatechange= function() {
// handle your request here
};
// send method (send null with GET method)
xhr.send(null);

If this confuses you, there are plenty of resources out there on the XMLHttpRequest object [lmgtfy.com] and how to implement it. This is just a glib example of how to pass data in the query string.