0

I have the following php:

1) echo json_encode(array('message' => 'Invalid Login Details: '.$user));

I also have the following:

2) $row = mysql_fetch_assoc($result);
   echo(json_encode($row));

Now consider the following jQuery:

 $.ajax({
           type: "POST",
           url: "get_login.php",
           data: {username: getusername, password:getpassword, usertype:getusertype},
           dataType: "json",
           success: function(data) {
              $("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
           }
        })

This succeeds for (1) but not for (2). This is obviously because jQuery expects a php response containing a message variable. (2) does not conform to this...I don;t know how to make this work as I am using different methods for creating the arrays...

How can I make $row in the php compatible with the data.message in the jQuery?

2
  • 1
    echo json_encode(array('message' => 'Invalid Login Details: '$user)); is wrong, near $user because it will display a php fatal error Commented Aug 16, 2011 at 12:04
  • @Dogbert - $row contains a tuple from a relation (i.e. user details - fName, lName, e-mail...) Commented Aug 16, 2011 at 12:11

3 Answers 3

2

try:

$data = array();
$data["message"] = "Valid request";
$data["row"] = mysql_fetch_assoc($result);
   echo(json_encode($data));

message = row:

$data = array();
$data["message"] = mysql_fetch_assoc($result);
   echo(json_encode($data));

or two line solution (inspired by val):

$row = mysql_fetch_assoc($result);
   echo(json_encode(array("message" => $row)));
Sign up to request clarification or add additional context in comments.

2 Comments

great, so this works...but it displays [Object object] -> how do I get it to display the actual string values?
data.message will be an object. If you want to display the contents, you have to iterate over its properties.
1

Try

$row = mysql_fetch_assoc($result);
   echo(json_encode(array('message'=>'I am the message','result'=>$row));

Then to answer your second question on the comments something similar to this could show the information ....

 $.ajax({
           type: "POST",
           url: "get_login.php",
           data: {username: getusername, password:getpassword, usertype:getusertype},
           dataType: "json",
           success: function(data) {
              $("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
              var html = '';
              for(var i = 0; i<data.result.length;i++){
                 html +='<div>'+data.result[i].fname+'</div>';
               }
              $('#result').html(html);
           }
        })

ofcourse you need to edit it abit where applicable .

Comments

0

i believe you don't have $row['message'] from mysql_fetch_assoc. you have to explicitely define the message key on the array before json_encode.

    $row = mysql_fetch_assoc($result);
    $row['message'] = 'Hello '.$row['username'];
    echo(json_encode($row));

Comments

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.