LSN Rabbani LSN Rabbani - 7 months ago 49
Javascript Question

Auto-fill input form with PHP, MySQL and jQuery

I had a problem with my auto-fill input form, with PHP MySQL and jQuery of course. So I have the select option form and some 'disabled' textboxes. And I want when I select the one of the option, the textboxes is auto-fill with value from the database

So there is my whole code :

PHP (Database connection) - I put it above my

<html>
code

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_mydatabase';

mysql_connect($host, $user, $pass);
mysql_select_db($db);

$user = '';
$query = mysql_query("SELECT * FROM tb_payment") or die (mysql_error());
?>


HTML

<select name="student" class="input-data">
<option value="">-- SELECT STUDENT --</option>
<!-- Do loop data from database -->
<?php while ($row = mysql_fetch_object($query)): ?>
<option value="<?php echo $row->id_user ?>">
<?php echo $row->name ?>
</option>
<?php endwhile; ?>
</select>
<br />
<table border=1>
<tr>
<th>ID</th>
<td><input type="text" class="output-id" disabled /></td>
</tr>
<tr>
<th>Name</th>
<td><input type="text" class="output-name" disabled /></td>
</tr>
<tr>
<th>Total Payment</th>
<td><input type="text" class="output-total" disabled /></td>
</tr>
</table>


jQuery :

$(document).ready(function(){
$(".input-data").on("change", function(){
<?php $id_user = "$(this).val()"; ?>

<?php $data = mysql_query("SELECT * FROM tb_payment WHERE id_user = '$id_user'") or die (mysql_error()); ?>
<?php while($row = mysql_fetch_object($data)): ?>
$(".output-id").val(<?php $row->id_user ?>);
$(".output-name").val(<?php $row->name ?>);
$(".output-total").val(<?php $row->total ?>);
<?php endwhile; ?>
});
});


But when i try to select the option, the values are wouldn't appear. Can someone help me?

Answer

What about creating new page name process.php contain:

require_once "connectDB.php";

header('Content-type: application/json; charset=utf-8');

if(isset($_POST['one'])){
$json = array();
$id =  trim($_POST['one']);
$query = "SELECT id, name, total FROM tb_payment WHERE id_user = ?";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('s', $id);
$statement->execute();
$statement->bind_result($nId, $nName, $nTotal);
while ($statement->fetch()){
   $user=array('id'=>$nId,'name'=>$nName,'total'=>$nTotal);
    array_push($json,$user);
}
echo json_encode($json, true);

 }

And the Jquery:

$(document).ready(function(){
    $(".input-data").on("change", function(){
            var id = $(".input-data").val();
            var data = 'one=' + id;
            $.ajax({
                type: "POST",
                url: "process.php",
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        for (var i = 0; i < data.length; i++) { //for each user in the json response
                            $(".output-id").val(data[i].id);
                $(".output-name").val(data[i].name);
                $(".output-total").val(data[i].total);
                        } // for

                    } // if
                } // success
            }); // ajax
    });
});
Comments