ajax+php do search, show and close questions?

Source: Internet
Author: User
Tags http request


I used the ajax+php to make a simple search similar to Google searches, and when I didn't enter the alphabet, it showed, 1.jpg:

When I type the letter, it shows the effect of 2.jpg.

I would like to ask everyone, when I click Elsewhere, so that the pop-up search box automatically closed, how to modify the code as follows


<body>
<style type= "Text/css" >
#livesearch
{
margin:0px;
width:140px;
}
#txt1
{
margin:0px;
}
</style>
<script type= "Text/javascript" >
var xmlHttp
function Showresult (str)
{
if (str.length==0)
{
document.getElementById ("LiveSearch").
Innerhtml= "";
document.getElementById ("LiveSearch").
style.border= "0px";
Return
}
Xmlhttp=getxmlhttpobject ()
if (xmlhttp==null)
{
Alert ("Browser does not support HTTP Request")
Return
}
var url= "suggest_list.php"
url=url+ "q=" +str
url=url+ "&sid=" +math.random ()
Xmlhttp.onreadystatechange=statechanged
Xmlhttp.open ("Get", url,true)
Xmlhttp.send (NULL)
}
function statechanged ()
{
if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete")
{
document.getElementById ("LiveSearch").
Innerhtml=xmlhttp.responsetext;
document.getElementById ("LiveSearch").
Style.border= "1px solid #999999";
}
}
function Getxmlhttpobject ()
{
var xmlhttp=null;
Try
{
Firefox, Opera 8.0+, Safari
Xmlhttp=new XMLHttpRequest ();
}
catch (E)
{
Internet Explorer
Try
{
Xmlhttp=new ActiveXObject ("msxml2.xmlhttp");
}
catch (E)
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<form>
<table align= "center" style= "margin-top:140px" >
<TR>
<TD>
<input type= "text" id= "txt1" size= "" name= "username" >
<div id= "LiveSearch" ></div>
Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
</TD>
</tr>
</TABLE>
</form>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.