php和ajax中<select>标签的问题</select>

时间:2011-09-20 08:13:04

标签: php ajax

我有这个代码(如下)适用于FF&amp; Chrome但不适用于IE *。我正在尝试使用电子邮件地址填充选择标记。感谢

question.php

<html>
<head>
<script type="text/javascript">
function myFunction(val)
{   
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","qsrcipt.php?q="+val,true);
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
 }
 }
xmlhttp.send();
}
</script>
</head>
<body onload="myFunction(document.myForm.mySelect.value)">
<form name="myForm" style="float:left; margin-right: 10px; overflow: hidden;">
    <select name="mySelect" onchange="myFunction(this.value)" size="10">
        <option value="1" selected="selected">level 1</option>
        <option value="2">level 2</option>
        <option value="3">level 3</option>
    </select>
</form>
<div>
    <select id="myDiv" size="10"></select>
</div>
</body>
</html>

qsrcipt.php

   <?php
  // Fill up array with names
  $lsts = array("Anna","Brittany","Cinderella","Diana","Eva","Fiona");

  //get the q parameter from URL
  $q=$_GET['q'];
  if ($q == '1')
    {
    foreach($lsts as $lst){
        echo "<option id='1'>".$lst."</option>";
    }
    }
  else
    {
    echo "<option id='1'>this is a test</option>";
    }
  ?>

1 个答案:

答案 0 :(得分:1)

您的$lsts包含不是电子邮件的名称数组。

其次,改为生成HTML服务器端,最好将$lsts的值作为JSON编码数组返回,并在JavaScript中动态生成选择列表。