SharkBait Posted October 24, 2007 Share Posted October 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
448191 Posted October 24, 2007 Share Posted October 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted October 24, 2007 Author Share Posted October 24, 2007 Oh thanks for the info. Well I guess it is a bit more AjAX question than application design. Is a mod would like to, please move it at your leisure Quote Link to comment Share on other sites More sharing options...
448191 Posted October 24, 2007 Share Posted October 24, 2007 Oh thanks for the info. No problem, glad to help. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted October 25, 2007 Author Share Posted October 25, 2007 I had to make sure i had the php5-json package installed in my ubuntu release (dapper) to make sure the PECL extensions where there but its all good now! Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.