如何使用下拉菜单中的Ajax将值插入文本框?结果应该出现在Form Texbox上?

时间:2010-06-20 05:31:45

标签: php ajax

<script type="text/javascript">
    function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        }
        // code for IE7+, Firefox, Chrome, Opera, Safari
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                //document.myForm.text1.value=.innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "getuser.php?q=" + str, true);
        xmlhttp.send();
    }
</script>

</head>

<body>
<form name="myForm">
    <p>Selec the Menu :
        <select name="users" onchange="showUser(this.value)">
            <option value="">Select ID</option>
<?php do { ?>
            <option value="<?php echo $row_rscustomer['customer_number']?>"><?php echo $row_rscustomer['customer_number']?></option>
<?php } while ($row_rscustomer = mysql_fetch_assoc($rscustomer));
    $rows = mysql_num_rows($rscustomer);
        if ($rows > 0) {
            mysql_data_seek($rscustomer, 0);
            $row_rscustomer = mysql_fetch_assoc($rscustomer);
        } ?>
        </select>
    </p>
    <p>&nbsp;</p>
    <p>
        <input name="text1" type="text"
               value="<?php include (" getuser.php");?>"/>
    <p>
</form>
<div id="txtHint"></div>

1 个答案:

答案 0 :(得分:2)

嘿 - 我注意到,在您的AJAX onreadystatechange函数中,您已将xmlhttp.status == 200添加到条件中。在某些浏览器中,由于AJAX是客户端语言,因此不支持此操作。如果用户的浏览器不支持成员 status ,那么它的默认值将是 undefined 或0,无论哪种值永远不会是200,因此块内部条件不会被执行。

我建议删除 xmlhttp.status == 200 条件,并依赖 xmlhttp.readyState 。我第一次使用AJAX时遇到了这个问题。