0

I have a table in MySQL database with following columns: name, address, occupation, phone_number and serial_number. In my html form, among other fields, I have text fields for inputting Name, Phone number and Serial number. How can I, using jQuery autocomplete and PHP/MySQL populate the Name and Phone number fields using the Serial number field? I've managed to set autopopulate on one input field, but I don't know how to set it to populate the other two fields using the one field which has autocomplete. Thanks

these are the fields

   <input type="text" name="name" id="autocomplete" />
   <input type="text" name="phone_number" id="autocomplete" />
   <input type="text" name="serial_number" id="autocomplete" />

this is my php script for getting the strings from db

<?php
$dbhost = 'YOUR_SERVER';
$dbuser = 'YOUR_USERNAME';
$dbpass = 'YOUR_PASSWORD';
$dbname = 'YOUR_DATABASE_NAME';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$return_arr = array();

if ($conn)
{
    $fetch = mysql_query("SELECT name, phone_number, serial_number FROM users");


    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['value'] = $row['name'] . " " . $row['phone_number'] " . $row['serial_number'] ";         
        array_push($return_arr,$row_array);
    }


}
mysql_close($conn);
echo json_encode($return_arr);
?>
1
  • add some code to show what you've done Commented Jul 3, 2012 at 11:25

1 Answer 1

1

What you need to do is, when the autocomplete of the serial number is finished, run an ajax-call to retrieve the name and phone number based on the serial number and then populate those fields, something like this:

$('input[name=serial_number]').autocomplete({
    onComplete: function (val) {
        $.getJSON('/url-to-fetch-name-and-phone.php?serial_number=' + val, function (data) {
            $('input[name=name]').val(data.name);
            $('input[name=tel]').val(data.tel);
        });
    }
});

Obviously you'll need to change some function and variable names depending on your setup.

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

3 Comments

So, I have to make another script for fetching the name and phone no separately?
Yes, well it needs to be handled somewhere. Whether it's in another file or in the same doesn't really matter.
Tried it, doesn't work... autocomplete works fine, it returns the selected values from the tables, but it doesn't populate desired fields.

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.