2

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":"[email protected]"}

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

3
  • 1
    Your code looks correct. What specifically is not working? Commented Jan 30, 2013 at 22:24
  • It doesn't display when I append using $('#firstname').append(data.FirstName); but it does when I use $('#firstname').append(data); but this method outputs the entire array when I am only looking for one element. Commented Jan 30, 2013 at 23:18
  • I found my error, instead of dataType, I was using datatype... >.< Commented Jan 31, 2013 at 4:52

1 Answer 1

1

try with:

var obj = jQuery.parseJSON(data);
$('#firstname').append(OBJ.FirstName); 
Sign up to request clarification or add additional context in comments.

2 Comments

Had to make an edit to have it work but it did. Thank you very much. $('#firstname').append(obj.FirstName); Is there a way to do it without parsing it again like Sedz said?
I found my error, instead of 'dataType', I was using 'datatype'

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.