简体   繁体   中英

Using AJAX and JSON to display HTML content from PHP

I am trying to retrieve information from my php file which contains the following code. My code contains the colums Email, FirstName, LastName, and State.

$query = 'SELECT * FROM users WHERE LOWER(Email) = :email';
$stmt = $dbh->prepare($query);
$stmt->bindValue(':email', $email);
$stmt->execute();

if ($stmt->rowCount() == 1) {
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $firstName = $row['FirstName'];
    $lastName = $row['LastName'];
    $state = $row['State'];     
} echo json_encode($row);

My Ajax code is:

$.ajax({
    datatype: 'json',
    type: "POST",
    url: 'json-data.php',         
      success: function(data) {
        //called when successful
        $('#firstname').append(data.FirstName);     
      },
      error: function(e) {
        //called when there is an error
        //console.log(e.message);
      }

});

When I type $('#firstname').append(data); , it shows me the following output:

{"FirstName":"Foo","LastName":"Bar","State":"Florida","Email":"foo@bar.com"}

How do I make it so I can get only the first name and append it to a div?

try with:

var obj = jQuery.parseJSON(data);
$('#firstname').append(OBJ.FirstName); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM