[英]dynamic dropdown fetching data from database not working
I have a table named tblstationerystock
having three columns like :-我有一个名为
tblstationerystock
的表,其中tblstationerystock
三列,例如:-
uin, orderdate, quantity. uin,订单日期,数量。
There are multiple record on different orderdate
against uin
.针对
uin
不同orderdate
有多个记录。
My table Structure is:-我的表结构是:-
i have a form in which there are two input drop down (uin and orderdate ) which takes input from table tblstationerystock
.我有一个表单,其中有两个输入下拉列表(uin 和 orderdate),它们从表
tblstationerystock
获取输入。
If i select the uin
in the first input drop down box i want the second dropdown box should show only those date
which belong to that particular uin
.如果我在第一个输入下拉框中选择
uin
我希望第二个下拉框应该只显示属于该特定uin
那些date
。
My Problem:-我的问题:-
But the second dropdown shows all the value all the time .但是第二个下拉列表始终显示所有值。
my code for form is我的表格代码是
<?php
include('includes/config.php');
?>
<div class="form-group col-md-12">
<label> User Name<span style="color:red;">*</span></label>
<select class="form-control" name="user" id="uin" onchange="fnorderdate()" >
<option value=""> </option>
<?php
$sql = "SELECT uin from tblstationerystock group by uin order by uin asc ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->uin);?>"><?php echo htmlentities($result->uin);?></option>
<?php }} ?>
</select>
</div>
<div class="form-group col-md-12">
<label> User Name<span style="color:red;">*</span></label>
<select class="form-control" name="user" id="orderdate" >
<option value=""> </option>
<?php
$sql = "SELECT orderdate from tblstationerystock group by orderdate order by orderdate asc ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->orderdate);?>"><?php echo htmlentities($result->orderdate);?></option>
<?php }} ?>
</select>
</div>
<script>
function fnorderdate()
{
uin=$('#uin').val();
$.ajax({
method:"post",
url:"ajax.php"'
data:{uin:uin},
success:function(result)
{
$('#orderdate').html(result);
}
</script>
my ajax code is我的ajax代码是
<?php
include('includes/config.php');
if(isset($_POST[uin]))
{
$uin=$POST['uin'];
$select="select orderdate from tblstationerystock where uin='$uin' ";
$query=mysqli_query($conn,$select);
while($data=mysqli_fetch_assoc($query))
{
echo "<option value='".$data['orderdate']."'>".$data['orderdate']."</option>
}
}
?>
View Part:-查看部分:-
<?php
include('includes/config.php');
?>
<div class="form-group col-md-12">
<label> UIN<span style="color:red;">*</span></label>
<select class="form-control" name="uin" id="uin" >
<option value="" > </option>
<?php
$sql = "SELECT uin from tblstationerystock group by uin order by uin asc";
$query = $dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->uin);?>">
<?php echo htmlentities($result->uin);?>
</option>
<?php }} ?>
</select>
</div>
div class="form-group col-md-12">
<label> ORDER DATE<span style="color:red;">*</span></label>
<select class="form-control" name="orderdate" id="orderdate" >
</select>
</div>
jQuery / AJAX Code:- jQuery / AJAX 代码:-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#uin').on("change",function () {
var uin = $(this).val();
$.ajax({
url: "ajax.php",
type: "POST",
data: "uin="+uin,
success: function (response) {
console.log(response);
$("#orderdate").html(response);
},
});
});
});
</script>
make a php file name it ajax.php in same directory在同一目录中创建一个 php 文件,将其命名为ajax.php
and put this code:-并输入此代码:-
<?php
include('includes/config.php');
$uin = $_POST['uin'];
echo "<option>Select ORDER DATE</option>";
$sql = "SELECT orderdate from tblstationerystock WHERE uin=$uin";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<option value="<?php echo htmlentities($result->orderdate);?>">
<?php echo htmlentities($result->orderdate);?>
</option>
<?php }}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.