donglvlao8367 2012-05-04 12:01
浏览 27

对于使用Ajax的PHP搜索

I perform live search in php using ajax. This is my code for performing task 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:
" + 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>

and 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>";

    }

}
?>

Performing this code I can not select value for press key. What was the use that I can select the value?

  • 写回答

2条回答 默认 最新

  • duanlumei5941 2012-05-04 12:06
    关注

    You have no <select></select> around your <options> tags list.

    You should take a look on using jQuery for your Ajax stuff.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度