Php 无法从第二个动态相关下拉框中获取值
我希望我这一整天的麻烦至少现在已经过去了。我有两个下拉框。提交后,我在操作页面中只有first的下拉框值。我的第二个下拉框取决于在第一个下拉框中选择的值 这是head部分中的ajax代码Php 无法从第二个动态相关下拉框中获取值,php,javascript,jquery,html,ajax,Php,Javascript,Jquery,Html,Ajax,我希望我这一整天的麻烦至少现在已经过去了。我有两个下拉框。提交后,我在操作页面中只有first的下拉框值。我的第二个下拉框取决于在第一个下拉框中选择的值 这是head部分中的ajax代码 <script> function getXMLHTTP() { //function to return the xml http object var xmlhttp=false; try{ xm
<script>
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getScreen(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('screendiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
函数getXMLHTTP(){//返回xml http对象的函数
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}
返回xmlhttp;
}
函数getScreen(strURL){
var req=getXMLHTTP();
如果(请求){
req.onreadystatechange=函数(){
如果(req.readyState==4){
//只有在“OK”的情况下
如果(请求状态==200){
document.getElementById('screendiv')。innerHTML=req.responseText;
}否则{
警报(“使用XMLHTTP:\n“+req.statusText时出现问题”);
}
}
}
请求打开(“获取”,strURL,true);
请求发送(空);
}
}
这是页面的主体
<form method="post" action="a.php" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Device Name</td>
<td width="150"><select name="device" onChange="getScreen('finddevice.php?device='+this.value)">
<option value="">Select device</option>
<option value="1">Iphone 3</option>
<option value="2">Iphone 4</option>
<option value="3">Ipad </option>
<option value="4">android </option>
</select></td>
</tr>
<tr style="">
<td>Screen</td>
<td ><div id="screendiv"><select name="screen">
<option>Select Screen</option>
</select></div></td>
</tr>
</table>
<input type="submit" value="submit" />
</form>
设备名
选择设备
iPhone3
iPhone4
Ipad
安卓
屏风
选择屏幕
它调用finddevice.php页面,该页面包含以下代码
<? $device=$_REQUEST['device'];
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('future');
$query="select screen from screen where device_id=$device";
$result=mysql_query($query);
?>
<select name="screen">
<option>Select Screen</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['screen']?></option>
<?php $row['device_id'] ?>
<? } ?>
</select>
选择屏幕
它可以完美地工作,并在从第一个下拉框中选择值时将表中的值带入第二个下拉框。但是当我提交数据时,我只能使用$\u POST方法访问a.php文件中的第一个下拉框值。这意味着我只能获得
$\u POST['device']
的值,但没有$\u POST['screen']
的值。请帮助我。在finddevice.php的第14行(如上所述),您似乎错过了第二个下拉列表中选项的值我的意思是应该是
尝试在第二个下拉列表中为选项填写id或唯一值
除非您填写选项的值,否则第二个下拉列表将向a.php提交空白值
finddevice.php:
<?php
$device=$_REQUEST['device'];
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('future');
$query="select id, screen from screen where device_id=$device";
//if screen table contains id field ...
//replace id with primary key or any unique identifier for screen table
//if there aint any primary key in "screen" table, use a field that is unique
//or create a primary key
$result=mysql_query($query);
?>
<select name="screen">
<option>Select Screen</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<!-- <option value><?=$row['screen']?></option> -->
<option value="<?php echo $row['id']; ?>"><?php echo $row['screen']; ?></option>
<?php } ?>
</select>
选择屏幕