1

I'm a newb to the whole PHP, jQuery (Ajax), Mysql stack, and I can't figure this out.

I have some PHP code making a call to Mysql and returning one row, in that same page I read the json being returned from php into jquery and I use the JSON.parse function and I can retrieve the information in that object like this: client.firstname, client.lastname and so on.

PHP:

        $query="SELECT * FROM clients WHERE  clientid='$clientid'";
        $result=mysqli_query($con,$query) or die(mysql_error());
        $num_rows=mysqli_num_rows($result);         
        $row = mysqli_fetch_array($result);
        $client=json_encode($row);

jQuery:

$(document).ready(function() {
    data='<?php echo $client?>';
    client=JSON.parse(data);
    $("#company").val(client.company);
    $("#firstname").val(client.firstname);
});

However my problem begins when I try to make an ajax call to a PHP file which executes the same query, and returns the same data. Once I receive the json object on the ajax call I can print it out and see I am receiving the row back, but as soon as I call the JSON.parse(data) function I get an error message

PHP:

$query="SELECT * FROM clients WHERE  clientid='$clientid'";
$result=mysqli_query($con,$query) or die(mysql_error());
$num_rows=mysqli_num_rows($result);         
$row = mysqli_fetch_array($result);
$client=json_encode($row);
echo $client; //this is the only different line

jQuery

$.ajax({
    type: "POST",
    url: "ClientDetails.php",
    data: "clientid="clientid,
    datatype: "json",
    success: function(data){
        client=JSON.parse(data);
        $("#company").val(client.company);
        $("#firstname").val(client.firstname);
    }
});

Once I run it I get this error message in Firebug:

SyntaxError: JSON.parse: unexpected character

Not sure what I'm doing wrong, any help is greatly appreciated.

2
  • what does <?php echo $client?> output exactly? Also you forgot the semicolon at the end of it. <?php echo $client; ?> Commented Sep 19, 2013 at 22:12
  • is that the contents of ClientDetails.php you posted there or is ClientDetails.php a separate php document? Commented Sep 19, 2013 at 22:19

1 Answer 1

2

You don't need to parse it, jQuery does that for you since you specified the dataType.

success: function(client){
    //client=JSON.parse(data);
    $("#company").val(client.company);
Sign up to request clarification or add additional context in comments.

1 Comment

Hey guys, thanks for the insights to all of you, I was doing something very stupid, I had my html standard document tags in my PHP file, got rid of all the tags and that got rid of the "JSON.parse: unexpected character" error message. However I still could not see anything being printed when I did $("#company").val(client.company); I tried creating a complete new ajax calling page and it worked, something must have been in cache I think. Thanks all.

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.