[英]How to get selected value of SELECT OPTION
i am having a problem with my code, i have two SELECT, the first one is "drop_drop" and the second one is "salidhan_ini", which will change the option of the "salidhan_ini" every time the "drop_drop" is change, it will hold the value of the "salidhan_ini" from the database using php, but my code is not working. 我的代码有问题,我有两个选择,第一个是“ drop_drop”,第二个是“ salidhan_ini”,每次更改“ drop_drop”时,它都会更改“ salidhan_ini”的选项,将使用php从数据库保存“ salidhan_ini”的值,但我的代码无法正常工作。
INDEX.PHP 的index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="jquery-1.8.3.min.js"></script>
<script src="jquery-1.11.1.min.js"></script>
<script>
$(document).ready( function() {
$('#drop_drop').on('change', function (e) {
$.ajax({
type: 'POST',
url:"process.php",
data:{keyname:$('#drop_drop option:selected').val()},
success: function(simbag){
$("#salidhan_ini").html(simbag);
}
});
});
});
</script>
</head>
<body>
<div id="dropdown1">
<select name="taskOption" id="drop_drop">
<option value="1">What</option>
<option value="2">When</option>
<option value="3">Where</option>
</select>
</div>
<div id="dropdown2">
<select id="salidhan_ini">
</select>
</div>
</body>
</html>
process.php process.php
<?php
$id_man = $_POSt['taskOption'];
$con=mysqli_connect("localhost","root","","sample_sample");
// Check connection
if (mysqli_connect_errno()){
die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
$result = mysqli_query($con,"SELECT * FROM sample_hatak where id = $id_man");
if(!$result){
echo 'Error: '. mysql_error();
}else{
while($row = mysqli_fetch_array($result))
{
echo "<option value=". $row['FirstName'] .">". $row['LastName'] ."</option>";
}
}
mysqli_close($con);
?>
what i want to achive is everytime the "drop_drop" is change, the value of selected option from "drop_drop" will be past into "process.php" and then it hold all the data that has the corresponding id which is the value of selected option of "drop_drop" and then it will return all as the option for the "salidhan_ini". 我想实现的是每次“ drop_drop”更改时,“ drop_drop”中所选选项的值将被粘贴到“ process.php”中,然后保存所有具有相应ID(即所选值)的数据选项“ drop_drop”,然后它将全部返回为“ salidhan_ini”的选项。 thnks for helping!
感谢您的帮助!
You need to populate 2nd select as follows- 您需要填充第二选择,如下所示:
$(document).ready( function() {
$('#drop_drop').on('change', function (e) {
$.ajax({
type: 'POST',
url:"process.php",
data:{keyname:$('#drop_drop option:selected').val()},
success: function(simbag){
$.each(simbag, function(val, text) {
$("#salidhan_ini").append( $('<option></option>').val(val).html(text) )
});
}
});
});
});
Also include only one version of jQuery 还仅包含一个版本的jQuery
Thanks for help! 感谢帮助! i found my problem,the problem is in
process.php
我发现了我的问题,这个问题在
process.php
<?php
$id_man = $_POSt['taskOption'];
$con=mysqli_connect("localhost","root","","sample_sample");
// Check connection
if (mysqli_connect_errno()){
die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
$result = mysqli_query($con,"SELECT * FROM sample_hatak where id = $id_man");
if(!$result){
echo 'Error: '. mysql_error();
}else{
while($row = mysqli_fetch_array($result))
{
echo "<option value=". $row['FirstName'] .">". $row['LastName'] ."</option>";
}
}
mysqli_close($con);
?>
the problem is " $POSt
" so i change it to, " $_POST
", 问题是“
$POSt
”,所以我将其更改为“ $_POST
”,
and this echo "<option value=". $row['FirstName'] .">". $row['LastName']
并且此
echo "<option value=". $row['FirstName'] .">". $row['LastName']
echo "<option value=". $row['FirstName'] .">". $row['LastName']
echo "<option value=". $row['FirstName'] .">". $row['LastName']
the FirstName
and LastName
is not part of my attribute in my database, because it is firstname
and lastName
. echo "<option value=". $row['FirstName'] .">". $row['LastName']
FirstName
和LastName
不是我数据库中属性的一部分,因为它是firstname
和lastName
。
thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.