Jump to content

[SOLVED] Pulling Multiple Values with PHP via ajax


SharkBait

Recommended Posts

 

I'm working on a small project and wondering what is the best way to about extracting multiple variables using ajax and php.

 

I have a form with various fields and with one field when you enter a value it can do a database look up and hopefully automatically fill out the related fields in the same form.

 

Now I could put all the values I retrieve (via MySQL) into a single string and delimit it with a ; or something via PHP and then have javascript process the string and split the variables up to go back into the various form fields I have. Seems ok right?

 

Or would I look at using XML and having PHP create some sort of XML response that I can then use javascript to parse though and insert the values into the appropriate locations?

 

What the users enter is a single value and queries the database for the exact match which will return up to 5 other values which then the user would not have to enter manually. (its a search type feature).

 

Hope this makes sense, if not I can screenshot the form and draw in what goes where ;)

XML works fine, but the easiest way to do this is using JSON.

 

Very, very easy:

 

echo json_encode(array('inputField1'=>'someValue', 'inputField2'=>'someOtherValue'));

 

response = eval("(" + http.responseText + ")");
getElementById('inputField1').value = response.inputField1;

 

etc...

 

This is hardly an Application Design question though. More of an Ajax question IMO.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.