mercredi 6 mai 2015

How to pass mysql result as as jSON via ajax

I'm not sure how to pass the result of mysql query into html page via ajax JSON. ajax2.php

$statement = $pdo->prepare("SELECT * FROM posts WHERE subid IN (:key2) AND Poscode=:postcode2");
    $statement->execute(array(':key2' => $key2,':postcode2'=>$postcode));
 // $row = $statement->fetchAll(PDO::FETCH_ASSOC);
 while( $row = $statement->fetch())
 {
     echo $row['Name'];//How to show this in the html page?
      echo $row['PostUUID'];//How to show this in the html page?
      $row2[]=$row;
 }
    echo json_encode($row2);

How to pass the above query result to display in the html page via ajax below?

my ajax

$("form").on("submit",function() {

    var data = {
      "action": "test"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "ajax2.php", //Relative or absolute path to response.php file
      data: data,
      success: function(data) {
          //how to retrieve the php mysql result here?
        console.log(data);// this shows nothing in console,I wonder why?
      }
    });
    return false;

});

Aucun commentaire:

Enregistrer un commentaire