开发者

Problem with <select> tag in php and ajax

开发者 https://www.devze.com 2023-04-06 06:04 出处:网络
I have this code (below) that works on FF&Chrome but doesn\'t work on IE*. I am trying to populate the select tag with email address. Thanks

I have this code (below) that works on FF&Chrome but doesn't work on IE*. I am trying to populate the select tag with email address. Thanks

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

开发者_C百科
   <?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>";
    }
  ?>


Your $lsts contains the array of names not email.

Secondly, instead generating the HTML server side, it's better to return the values of $lsts as a JSON encoded array and, in JavaScript, dynamically generate the select list.

0

精彩评论

暂无评论...
验证码 换一张
取 消