School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,474 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,732 people online right now. Registration is fast and FREE... Join Now!




Ajax display responseText in multiple fields

 

Ajax display responseText in multiple fields

sherbet

25 Jun, 2009 - 05:39 PM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2009
Posts: 13


My Contributions
[b]
Hi I am very new to Ajax and programming in general, I need to display the results from Ajax into several different text fields. I can currently display all fields into a Div, but can not split and display as separate elements. I need to display the PHP results in the corresponding HTML fields.
[b/]

CODE

//Ajax code
ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;


//PHP result
while($row = mysql_fetch_array($qry_result)){    
    $display_name .= "$row[currency_name]";
    $display_symbol .= "$row[currency_symbol]";
    $display_code .= "$row[currency_code]";
}

echo $display_name;
echo $display_code;
echo $display_symbol;


//HTML fields for results to be displayed.
Name:<input type="text" id='display_name'/>
Code:<input type="text" id='display_code'/>
Symbol:<input type="text" id='display_symbol'/>






User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Ajax Display ResponseText In Multiple Fields

25 Jun, 2009 - 07:51 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
You will want to use JSON or another type of thing. Basically, you need to have a script back in your Javascript to go through each of the elements.

Here is the type of structure I suggest:
CODE

<?php
echo "json = {
  'id1': 'new HTML',
  'id2': 'new HTML',
  'id3': 'new HTML' // no comma after the last one
};";
?>


Then in the Javascript, once you get the stuff back you need to get Javascript to see it as an object, so you use something like so:
CODE
returnedStuff = eval(jsonVar);


The only problem there is that you could be letting users run scripts on your site if you don't implement protection. If you go here:
http://www.json.org/
they have a Javascript library that gets rid of malicious things in JSON so you don't have to worry about what the users get in it.

Hope that makes sense.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:56AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month