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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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