0

I want to autofill the user form when a user is selected. How can I pass through an array from PHP to JavaScript/jQuery and then put the values into the correct textboxes?

$("#client").change(function() {
     $.get("../jquery/update_client.php?id=" + $("#client").val(),function(data){
          $("#first").val(data);
     });
});

PHP file:

if(isset($_GET['id'])){
include('../db_connect.php');
$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$query = "SELECT * FROM users WHERE iduser=$id LIMIT 1";
$result = $mysqli->query($query);
$row = $result->fetch_array();
echo $row[];
}

How can I capture the array in JavaScript/jQuery, and then parse the information to go into the right boxes?

3 Answers 3

1

In PHP;

echo json_encode($row);

In JavaScript you will now get a map (array) back as data. This map you can then loop to fill your textbox(es).

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

Comments

0

Maybe if you output JSON it's better to use getJSON method with a simple for loop to set corresponding fields values:

$.getJSON("../jquery/update_client.php?id=" + $("#client").val(), function(data) {
    for (var el in data) {
        $('[name="' + el + '"]').val(data[el]);
    };
});

http://jsfiddle.net/2LuuC/

Comments

0

well in php output the data as json. that is use

echo (json_encode($data));

then as dfsq said

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.