How to get an input automatically based on other input in a from by php and mysql

Dear friends,
I want to get my field value from database based on other input in my form. For example, consider there are 2 inputs in a from. (Family name and Age). If a user enters family name, the program should get age based on his family name from the database before submitting the form.
I’m nearly sure, it is possible with the help of java script.

<form name = "autofill" method="post">
<input type="text" name="family-name" id="family-name" />
<input type="text" name="age" id="age"/>
<input type="submit" name="submit" value="submit"/>
</form>

I guess my php code should be something like this:

<?php
  $family_name  = $_GET['family_name'];
  $sql = "SELECT age FROM users WHERE family_name = '$family_name'";
   $return = mysqli_query($db, $sql);
    $rows = mysqli_fetch_array($return);
     echo json_encode($rows);
 ?>

I have jquery plugin in my file.
How can I do it?
Thanks in advance

You have to use AJAX or FETCH/GET along with the php.

<?php
require_once 'assets/config/config.php';

/*
 * Database Connection 
 */
$db_options = array(
    /* important! use actual prepared statements (default: emulate prepared statements) */
    PDO::ATTR_EMULATE_PREPARES => false
    /* throw exceptions on errors (default: stay silent) */
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    /* fetch associative arrays (default: mixed arrays)    */
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);

    function searchAge($family_name, $pdo) {
        /* Personally I would use camelcase $familyName instead of $family_name */ 
        $query = "SELECT age FROM users WHERE family_name=:family_name";
        $stmt = $pdo->prepare($query);
        $stmt->execute([':family_name' => $family_name]);
        $result = $stmt->fetchALL(PDO::FETCH_ASSOC);
        return $result;
    }

/* Makes it so we don't have to decode the json coming from JQuery */
header('Content-type: application/json');

/*
 * Get Category from the FETCH statment from javascript
 */
$family_name = htmlspecialchars($_GET['family_name']);

/*
 * Call the search Function
 */
$data = search($family_name, $pdo);

output($data); // Send Age back to javascript 
/*
 * Throw error if something is wrong
 */

function errorOutput($output, $code = 500) {
http_response_code($code);
echo json_encode($output);
}

///*
// * If everything validates OK then send success message to Ajax / JavaScript
// */

/*
 * After converting data array to JSON send back to javascript using
 * this function.
 */
function output($output) {
http_response_code(200);
echo json_encode($output);
}

That is the php file of the Javascript/Ajax as for the javascript portion if you are using jQuery I believe you will be using $.get(), but it should be pretty similar to FETCH() function in Vanilla-JS which I code in. I would read up on $.get() and I pretty sure you can find something on the web or maybe someone here can help you with that part? I haven’t tested the script, so there might be some bugs in it.

Thanks for your kind reply,
To be honest, I don’t understand your code!
As I said I use an autocomplete function for one input. I’d like to fill another my input based on that. For Example, if client suggests David on that field, the age should be appeared on the other input.
I explain whatever I’v done, so far:

  1. I added jquery UI plugin to my code:

2)In a search.php file, I have this code to get my intended filed from the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$searchTerm = $_GET[‘term’];
//get matched data from skills table
$query = $db->query(“SELECT * FROM users WHERE family_name LIKE
'%”.$searchTerm."%’ ORDER BY family_name ASC");while ($row = $query-
>fetch_assoc()) {
$data[] = $row[‘family_name’];
}
2. I added this function in my file which has the plugin and inputs:
$(function() { $( “#family-name” ).autocomplete({ source: ‘search.php’ });

Now, it works for me to autocomplete my input(family-name). But, I want to autofill my other input(age) based on that input(family-name)
I was wondering if you could help me with this. (That would be ideal if you could do it based on my code and with more details)
Thank you very much for your time
Regards

Your query is heavier than it needs to be, but you also aren’t including everything that you want out of it. If you only want the name, then you should ask only for the name, not *.

You need to expand the code in the autocomplete to get an entire object out of it, no it isn’t simple for someone new to figure out.

Thanks for your advice, My problem has been solved

Sponsor our Newsletter | Privacy Policy | Terms of Service