2

Okay, so I'm a bit of a newb to JSON. I'm trying to cobble together an AJAX call(a PHP mssql query) that returns the values back to the original form through JSON. I'm getting [object Object] instead of the actual value of the variable. Through some searching I think the issue is related to parsing, but I thought json_encode handled this. Or maybe it does and I haven't structured this correctly.

Here's what Im trying to accomplish: An html form has 3 fields: Account Number, Account Name and Address1. I want the user to be able to enter the Account Number and then have the Name and Address fields populate on blur with the results of the mssql query.

Here's what I have: HTML

<body>
<label>Account Number:</label><input type="text" id="acctnumber" />
<label>Account Name:</label><input type="text" id="acctname" />
<label>Address 1:</label><input type="text" id="address1" />
</body>

Jquery (I put the acctnumber input value in just for testing)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
<script>
$(document).ready(function(){
      $("#acctnumber").blur(function(){ 
          $.ajax({
              url: "ajaxtestscript.php?acctnumber=hfi01",
              dataType: "json", //the return type data is jsonn
              success: function(data){ // <--- (data) is in json format
                $("#acctname").val(data.Name);
                $("#address1").val(data.Address);
                //parse the json data
              }   
        });
    });
});
</script>

PHP

    <?php

    header('Content-Type: application/json');

//mssql connection info//

    $acctnumber  = $_GET["acctnumber"]; // we received this from the json call

    //declare the SQL statement that will query the database
    $query = "select[description],[daddr1] from trCustomer where company = '$acctnumber' and archived = 0";

    //execute the SQL statement and return records
    $rs = $conn->execute($query);

    $test = array();
    $test['Name'] = $rs[description];
    $test['Address'] = $rs[daddr1];

    echo json_encode($test);


    //echo $name.$address; put this in to test the query was working, which it was

    //close the connection and recordset objects freeing up resources 
    $rs->Close();
    $conn->Close();


    $rs = null;
    $conn = null;


    ?>

When I actually try this on the html form, I enter in the Account number value (hardcoded at this point) and click out of the element to fire blur, and in the other two input fields I get [object Object]. Although if I just echo my query back I can see the values so I'm thinking I have done something wrong or left something out with my json_encode. Any assistance would be greatly appreciated.

3
  • you need to share the data for more clarity. Commented May 6, 2016 at 13:32
  • you say, you're getting [Object Object] when you put some alert or console.log at the success function? Commented May 6, 2016 at 13:38
  • When you specify json as the dataType, the string returned from your php script will be parsed as json so that data is a javascript object. If the string is valid json of course... To see what is inside, you can do console.log(data); in the success function. Commented May 6, 2016 at 13:41

2 Answers 2

1

After executing your query, you first need to fetch the resulting rows with whatever function your DB Library is supposed to do so. And should't you prepare your query first?

Otherwise the object that you are trying to json_encode() is an resource object. That is why you get object Object. I would expect code like this

$conn = new PDO(...);
$stmt = $conn->prepare("SELECT ...");
$stmt->execute();

$test = array();
while ($row = $stmt->fetch()) {
    $test[] = array(
        'Name' => $row[description],
        'Address' => $row[daddr1]
    );
}
echo json_encode($test);

$stmt = NULL;
$conn = NULL;
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, your question is unclear and depending on the situation, I would debug and test the outputs.

1. PHP : The query result : var_dump(json_encode($test)); exit(0);

This will help you to know what data is being sent to front. alert or console print the received data in ajax to view.

2. Javascript : console.log(data);

This will help you to know what data is being received and what exactly you need to write to populate in the required field. Check in browser console.

Thanks for upvoting. Cheers !

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.