如何基于下拉选择和单击按钮从MySql数据库检索数据?

时间:2019-03-12 12:08:15

标签: javascript php jquery mysql ajax

我试图在选择更改和单击按钮时从MySql数据库中检索一些数据。来自结果行的数据应该显示在一个隐藏的div的2个文本框中,单击按钮即可显示。

empstatus.php

sql = "select * from mx_party_master";
$result = mysqli_query($db , $sql);
<body>

<?php myNav(); ?> <!-- A function to display a sidebar -->

<div class="container-fluid">
<div class="jumbotron">
<form action="#" method="GET">

<!-- DROPDOWN LIST -->
<select class="form-control" name="selectemp" id="selectemp">
<option  value="">Select Party..</option>
<?php while($res= mysqli_fetch_assoc($result)){ ?>
<option value="<?php $res['KUNNR']; ?>"><?php echo $res['KUNNR'].' 
'.$res['NAME1']; ?></option>
<?php }?>
</select>
<br>

<!-- BUTTON TO SHOW THE HIDDEN DIV AND RETRIEVE DATA FROM THE DATABASE -->
<button type="button" class="btn btn-lg btn-dark" name="subemp" 
id="subemp">Search</button>

<!-- DIVISION WITH INPUT FIELDS WHERE DATA IS TO BE DISPLAYED-->    
<div class="hiddendiv" style="display: none;">
<label for="open">Open Order: </label>
<input type="text"  class="form-control" name="open" id="open"/>
<br>
<label for="open">Open Delivery: </label>
<input type="text"  class="form-control" name="opendel" id="opendel" />
</div>

</form>
</div>
</div>
</body>

<!-- AJAX CODE -->

<script>
$(document).ready(function () {
    (function ($) {
        $('#selectemp').change(function () {
            $('#subemp').click(function(){
                $('.hiddendiv').toggle();

                    $.ajax({
                        type: "POST",
                        url: "empstatest.php",
                        dataType: 'JSON',
                        data: $('#selectemp').serialize()
                    }).done(function (data) {
                        $('#open').val(data.ordblock); <!-- I'M SURE I'M DOING THIS WRONG -- >
                        $('#opendel').val(data.ordblock);
                    });
            });
        });
    })(jQuery);
});
</script>

empstatest.php

<?php
session_start();
header('Content-type: application/json');
$db = mysqli_connect("credentials");

$value =  $_POST['selectemp'];
$sql = "select ordblock, delblock from mx_crlimit where kunnr = '$value'";
$res = pg_query($db, $sql);
$is = '';
$data = pg_fetch_assoc($res);
$is = json_encode($data);
echo $is;
?>

我是Jquery的新手,ajax代码可能有问题,尤其是在我试图在输入字段中显示数据的地方。请帮助我解决这个问题。

0 个答案:

没有答案