tags:

views:

149

answers:

2

Hi again, I am using ajax post and am receiving data in the form of html. I need to split up the data and place pieces of the data all over the page. I built my response data to be something like <p id='greeting'> Hello there and Welcome </p> <p id='something'>First timer visiting our site eh'</p> It is a little more complicated and dynamic but I can figure it out if get this question answered. Thanks

$.ajax({
            type:'POST',
            url: 'confirm.php',
            data: "really=yes&sure=yes",
            success:function(data){
                    //Need to split data here
            }
        });
+1  A: 

Update: Just realized you should probably do this:

success:function(data) {
    data = $('<div/>').append(data);
    $('#greeting',data).appendTo('#one')
    $('#something',data).appendTo('#two')
}

As you cant use .find properly since it isnt a child but if you append it to an empty node you can. The other alternative would be using .filter

$.ajax({
            type:'POST',
            url: 'confirm.php',
            data: "really=yes&sure=yes",
            success:function(data){
                    $('#greeting',data).appendTo('#one')
                    $('#something',data).appendTo('#two')
            }
        });

You can extract from data and append where you want to. You can also do something like return JSON instead, and instead of extracting html from html just access the html from an object.

$(data.greeting).appendTo('#one')
$(data.something).appendTo('#two')

The response would have to be like:

({ 'greeting':'html', 'something' :'other html' })
meder
Thanks for the reply, but for some reason I cannot just grab one tag via id. If it works I grab the entire string.
Theopile
what does alert(data) alert?
meder
Could you elaborate on the JSON solution?
Theopile
basically, return JSON from server-side which is a string parsed as Javascript. `json_encode` if you're using php will help. `dataType:'json'` will be needed as well on the client-side.
meder
A: 

(Meder's response will work if you are comfortable with JSON, but regular expressions are probably a little easier and will work just as well for this.)

You will probably need to break up the response text using regular expressions. For example, if the response text is:

<p id='greeting'> Hello there and Welcome </p>
<p id='something'>First timer visiting our site eh'</p>

Then you could use some JavaScript like this:

var greeting = response_text.match(/<p id='greeting'>.*</p>/);
var something = response_text.match(/<p id='something'>.*</p>);

(This site is great for learning about regular expressions: http://gskinner.com/RegExr/)

Computerish
1. My answer isn't specific to JSON, the JSON was an alternative tip 2. You should *never* use regular expressions to parse html. Only a matter of time before it's prone to failure - jQuery internally parses and grabs with DOM methods which is what you should be doing
meder
Take a look at Coding Horror's take on regex and HTML:http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.htmlAs they would say, regex and html is a bad idea, but not always. For example, if you were just inserting a welcome message into a couple places after the user logged in (for example, "Welcome, Theopile"), then it doesn't seem like a couple lines of regex is such a big crime.To the OP - If you have something significantly more complicated than what you example is or if any part of the AJAX response is determined by the user, then Meder is completely right.
Computerish
Also, for some reason I can't add a comment to Meder's solution, but if you are using PHP, this site has a great example of using JSON:http://www.extjs.com/learn/Tutorial:Creating_JSON_Data_in_PHP
Computerish