0

Array from result;

 {"autor":"Testando","publication":"a"}{"autor":"Testando","publication":"a"}{"autor":"Testando","publication":"asa"}{"autor":"Testando","publication":"a"}    

PHP;

header('Content-Type: application/json');
while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $autor = $rows['Nome']; 
    $publication = $rows['publication'];
    $my = array(
    "autor"=>"$autor",
    "publication"=>"$publication"
    );  
    echo json_encode($my);      
    }

Javascript;

            dataType: "json",
            success: function (data) {
                     $(data).each(function(autor, pubication) {
                     alert(data.autor + " : " + data.publication) 
                     });
            }

My problem: jQuery no alert the data

I think the problem is because there is no comma between the results, because when there is only one result, he warns normally in the future want to make a .append in the data, but I can not simply alert data, can anyone help?

2
  • You simply get no alert because your JS is expecting JSON, and your PHP returns a String that is not valid JSON. So your success function is not executed. And there is a typo in your JS: pubication Commented Aug 23, 2015 at 17:13
  • Use both answers below. Commented Aug 23, 2015 at 17:17

2 Answers 2

2

The problem is exactly what you said. Try to do this:

    header('Content-Type: application/json');
    $arr = array();
    while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $autor = $rows['Nome']; 
        $publication = $rows['publication'];
        $my = array(
        "autor"=>"$autor",
        "publication"=>"$publication"
        );  
        $arr[] = $my;
    }
    echo json_encode($arr); 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for help, my php and js he was wrong; thanks for fix my php
1

It looks like the you are using the each() function wrong.

$(data).each(function(i, value) {
    alert(value.autor + " : " + value.publication) 
});

3 Comments

thanks for help, my php and js he was wrong; thanks for fix my js
@MarleneOliveira well, I did what I could... :)
man, how I append the data after this? tryed $('#response').append(value.autor + " : " + value.publication); and failed :/

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.