使用PHP,MySQL和jQuery自动填充输入表单

时间:2016-02-10 02:02:04

标签: javascript php jquery html mysql

我的自动填充输入表单有问题,当然还有PHP MySQL和jQuery。所以我有选择选项表单和一些'禁用'文本框。我希望当我选择其中一个选项时,文本框会自动填充数据库中的值

所以有我的整个代码:

PHP(数据库连接) - 我把它放在<html>代码

之上
<?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; ?>
    });
});

但是当我尝试选择该选项时,不会出现值。有人能帮助我吗?

2 个答案:

答案 0 :(得分:3)

如何创建新的页面名称process.php包含:

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);

 }

和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
    });
});

答案 1 :(得分:0)

你必须打印

 $(".output-id").val(<?php print $row->id_user ?>);
 $(".output-name").val(<?php print $row->name ?>);
 $(".output-total").val(<?php print $row->total ?>);