1

我使用 ajax 在 php 中执行实时搜索。这是我执行任务 googleajax.php 的代码

<?php 

    $id=$_GET['id'];
    if(isset($_POST['submit']))
    {
        $temp=$_POST['name'];echo $temp;
    }
?>
<html>
<head>
<script language="javascript" type="text/javascript">
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(e){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getValue(id)
    {       


        var strURL="googledata.php?id="+id;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('div1').innerHTML=req.responseText;                     
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);

        }
    }

</script>
</head>
<body>
<form method="post">
        <?php
            /*$query="Select * from tblcountry where country_id='".$id."'";
            echo $
            $res=mysql_query($res);
            while($row=mysql_fetch_assoc($res))
            {
                extract($row);*/
            ?>
            <input type="text" id="name" name="name" onKeyUp="getValue(this.value)" value="<?php echo $id;?>" />
    <input type="submit" id="submit" name="submit" value="serch">

    <div id="div1" name="div1">

    </div>

                <?php
            /*<!--}-->*/

        ?>  
    </form>
</body>
</html>

和 googledata.php

<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
    die("error in connection");
}
else
{
    mysql_select_db("hms2012",$con);
}
$id= $_GET['id'];
if($id=="")
{
}
else
{
    $result=mysql_query("select * from tblcountry where country_name like '$id%'");

    while($row=mysql_fetch_assoc($result))
    {
        extract($row);

        echo "<option value='$country_id'><a href='googleajax.php?id=$country_id'>".$country_name."</a><br></option>";

    }

}
?>

执行此代码我无法选择按键的值。我可以选择值有什么用?

4

2 回答 2

1

您的标签列表<select></select>周围没有。<options>

你应该看看使用 jQuery 来处理你的 Ajax 东西。

于 2012-05-04T12:06:22.110 回答
0

什么是 $country_id 和 $country_name。我已经通过假设改变了它。请检查。

    echo '<select name="somename">';    
    while($row=mysql_fetch_assoc($result))
    {
        extract($row);

    echo "<option value='".$row['country_id']."'><a href='googleajax.php?id=".$row['country_id']."'>".$row['country_name']."</a><br></option>";

    }
   echo "</select>";
于 2012-05-04T12:27:28.120 回答