0

I'm trying to check if a user exists in my database and then change the values of my input fields to match that user's information. I have the following code but it doesn't seem to work.

    <button onclick="checkAvailability()">Check Availability</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function checkAvailability()
{
    $(function()
    {
        $.ajax(
        {
            type: 'POST',
            url: 'test.php',
            data: "name=" + $("#name").val(),
            dataType: 'json',
            success: function(row)
            {
                $('#name').val(row[0]);
                $('#address1').val(row[1]);
                $('#phone1').val(row[2]);
                alert('success');
            }
        });
    });
}
</script>

The alert goes off but none of the values are changed. I checked the response using Firebug and the response is a JSON object with the user's information. I'm not sure where the error is. Thank you for any input.

1
  • Can you please add the exact response JSON to your question. If the JSON is correctly formatted, you should access an object, not an array. Commented Jul 9, 2014 at 18:16

2 Answers 2

2

If you have a json object you must use: $("#name").val(row.name);

Sign up to request clarification or add additional context in comments.

Comments

0

In case you are getting a json then it might look like this

var json = [{"name": "sample"},{"phone":"sample"},{"address":"sample"}];

When you are doing row[0]. what you get is an object["name":"sample"] So you must make the following change

 success: function(row)
            {
                $('#name').val(row.name);
                $('#address1').val(row.address);
                $('#phone1').val(row.phone);
                alert('success');
            }

Also make sure you have input types with id's name, address1 and phone1

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.