English 中文(简体)
利用Ajax搜索PHP
原标题:For Search In PHP using Ajax
  • 时间:2012-05-04 12:01:34
  •  标签:
  • 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?

问题回答

页: 1

你们应该看看一下如何用j Query对付你Ajax的 st子。

数额是国名和国名。 我的假设改变了这个概念。 请检查。

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




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签