0

I have got this ajax

$.ajax({
                        type: "GET",
                        url: '../connect.php',
                        data: "OrB=" + ajaxsend+"&&IOr="+i,
                        success: function(data)
                        { 
                             var x = $.parseJSON(data);
                             var el='<div class="CommentsAw Comment_Hs">\
                                      <img src="../users/'+x[0]+'">\
                                      <span>'+x[1]+'</span>\
                                      <span class="s2">'+x[2]+'</span>\
                                    </div>'
                                    $(".F_W_comments").html().remove();
                                    $(".F_W_comments").html(el);
                        }
        });

and php

if (isset($_GET['OrB'])) {
        $OB=$_GET['OrB'];
        $I=$_GET['IOr'];
        if ($OB=='OO') {
            $OB='`Date` ASC';
        }else if ($OB=='No') {
            $OB='`Date` DESC';
        }
        $query=$con->query("SELECT id,comment FROM uploads WHERE Rand='$I'");
        $row=$query->fetch_row();
        $Commentsq=$con->query("SELECT * FROM (SELECT * FROM comments WHERE Post_id='$row[0]' ORDER BY $OB LIMIT 4) AS sub ORDER BY `DATE` ASC") or die($con->error);
        while ($CommentRow=$Commentsq->fetch_row()) {
                $CommenterPp=$con->query("SELECT Profile_pic FROM user_opt WHERE Username='$CommentRow[3]'");        
                $CommenterPicture=$CommenterPp->fetch_row();
                $CommenterPp=$con->query("SELECT Username FROM users WHERE Id='$CommentRow[3]'");        
                $CommenterName=$CommenterPp->fetch_row();
                echo json_encode(array($CommenterPicture,$CommenterName,$CommentRow));             
        }
    }

But it gives me error in console like this one

VM654:2 Uncaught SyntaxError: Unexpected token [ in JSON at position 107
[["5734919677561.jpg"],["Murad"],["1842","3","21","1","2016-05-08 21:56:52"]]                    
                [["5734919677561.jpg"],["Murad"],["1843","GOodm","21","1","2016-05-08 21:56:54"]]                    
                [["5734919677561.jpg"],["Murad"],["1845","re","21","1","2016-05-08 21:56:54"]]                    
                [["5734919677561.jpg"],["Murad"],["1844","re","21","1","2016-05-08 21:56:54"]]                    

What i want is connect.php to get data from database and then pass it to ajax. But the result was nothing probably bcause there is some error in my code

2 Answers 2

1

Echoing several json-encoded strings does not mean a valid json.

What you need to do is json_encode your data and echo it once.

$commenters = array();    // result array
while ($CommentRow=$Commentsq->fetch_row()) {
    $CommenterPp=$con->query("SELECT Profile_pic FROM user_opt WHERE Username='$CommentRow[3]'");        
    $CommenterPicture=$CommenterPp->fetch_row();
    $CommenterPp=$con->query("SELECT Username FROM users WHERE Id='$CommentRow[3]'");        
    $CommenterName=$CommenterPp->fetch_row();
    $commenters[] = array($CommenterPicture,$CommenterName,$CommentRow);             
}
// while loop over
echo json_encode($commenters);

And in your js you should iterate over an array of objects, not a simple object, for example:

success: function(data) {
    var x = $.parseJSON(data);
    for (var k in x) {
        console.log(x[k]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Removing the data in the arrays for a moment, the resulting JSON was structured like this:

[][][][]

That's not valid JSON. The parser expects a single object or array, not several arrays concatenated together like that.

Instead of outputting the response in the loop, build the total response and output it once after the loop. Basically create an empty array before the while loop in PHP, then push elements onto the array within the loop, then echo the JSON-encoded array after the loop.

My PHP is very rusty, but in PHP-ish pseudo-code it would be structured like this:

$result = array();
while ($CommentRow=$Commentsq->fetch_row()) {
    // other code...
    $result[] = array($CommenterPicture,$CommenterName,$CommentRow);
}
echo json_encode($result);

2 Comments

@TomOdell: It's not uncommon for similar answers to be given simultaneously on Stack Overflow. We don't really coordinate with each other.
I know i am just saying that it will more fair to accept his answer

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.