1

I have some problem. I have created some app that consuming JSON array.

The syntax is using Jquery 1.9

The logic is : getting values from textbox (valobj variable below) then write to toprightsec div area.

$("#btnsearch").click(function() {
        valobj = $('#search_box').val();
        $.getJSON("search.php", { q : valobj }, function(data,result){
            //show result from database
            $('toprightsec').append("Title" + data.title)
                            .append("Intro" + data.intro_text);
            //end show result
        }, JSON);
    });

JSON array is from PHP result. some example

{"content":[{"title":"Test Post 100","intro_text":"Intro Test"}]}

But, it's not working. any helps?

thanks in advance.

UPDATE

$("#btnsearch").click(function() {
    valobj = $('#search_box').val();
    $.getJSON("search.php", { q : valobj }, function(data,result){
        //show result from database
        $('.toprightsec').append("Title" + data.content[0].title)
                        .append("Intro" + data.content[0].intro_text);
        console.log(data)
        //end show result
    }, JSON);

UPDATE 2

$(document).ready(function () {

    $("#btnsearch").click(function() {
    valobj = $('#search_box').val();
    $.getJSON("search.php", { q : valobj }, function(data,result){
        //show result from database
        $.each(data.content, function() {
            $('.toprightsec').append("Title" + data.content[0].title)
                        .append("Intro" + data.content[0].intro_text);
        });

        //end show result
    }, JSON);
});
3
  • 1
    use data.content[0].title and so on Commented Jun 17, 2013 at 10:18
  • hi @x4rf41 it's not working. The section content still the same. nothing changed Commented Jun 17, 2013 at 10:21
  • Try using console.log(data) to look at the structure of the JSON in JS. Commented Jun 17, 2013 at 10:21

2 Answers 2

2

Use the following code:

 $('.toprightsec').append("Title" + data.content[0].title)
                            .append("Intro" + data.content[0].intro_text);

instead of:

 $('toprightsec').append("Title" + data.title)
                            .append("Intro" + data.intro_text);

If toprightsec is id then use the # and if class then use the .. here you have not specify that.

Sign up to request clarification or add additional context in comments.

11 Comments

maybe his selector is wrong too. what would this $('toprightsec') select ?
@x4rf41 I have mention about it in the answer. hope this will be understandable to OP.
hi @x4rf41 the toprightsec is a class. please see the update section
@randytan then use the . before the toprightsec and is there only one tag or more than one with this class name?
only one class: <td width="25px"><div class="toprightsec">Content for class "toprightsec" Goes Here</div></td>
|
0

You are using json array,so you have to access single array element and not whole array:

json_array.title //WRONG 
json_array[0].title //OK

Try something like:

$("#btnsearch").click(function() {
        valobj = $('#search_box').val();
        $.getJSON("search.php", { q : valobj }, function(data,result){
            //show result from database
            if (data.length){
                var _props = data.pop().content;
                $('toprightsec').append("Title" + _props.title)
                                .append("Intro" + _props.intro_text);
            }
            //end show result
        }, JSON);
    });

2 Comments

you are forgetting about the content array
there is another one :D var _props = data.pop().content; should be var _props = data.content.pop();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.