Home >Backend Development >PHP Tutorial > 关于变量与循环语句的有关问题
关于变量与循环语句的问题
各位大牛,消息想问个关于AJAX与PHP的问题,现在碰到这样的一个情况
我希望前端页面在点击某标签的时候,在下方可以显示我关于上方点击的标签进行关联的资料,代码如下
index.php
<html><br /> <head><br /> <script src="clienthint.js"></script> <br /> </head><br /> <br /> <body><br /> <br /> <form> <br /> First Name:<br /> <br /> <br /> <?php<br /> <br /> echo "<a>";<br /> echo "<i class=\"dapei_partleft\"></i>";<br /> echo "<span class=\"dapei_partmiddle\" title='teswt' name='test' onclick=\"showHint(1)\">test</span>";<br /> echo "<i class=\"dapei_partright\"></i>";<br /> echo " </a>";<br /> echo "<a>";<br /> echo "<i class=\"dapei_partleft\"></i>";<br /> echo "<span class=\"dapei_partmiddle\" title='teswt1' name='test1' onclick=\"showHint(2)\">test</span>";<br /> echo "<i class=\"dapei_partright\"></i>";<br /> echo " </a>";<br /> ?><br /> </form><br /> <br /> <p>Suggestions: <span id="txtHint"></span></p><br /> <br /> </body><br /> </html>
var xmlHttp<br /> <br /> function showHint(str)<br /> {<br /> if (str.length==0)<br /> { <br /> document.getElementById("txtHint").innerHTML=""<br /> return<br /> }<br /> xmlHttp=GetXmlHttpObject()<br /> if (xmlHttp==null)<br /> {<br /> alert ("Browser does not support HTTP Request")<br /> return<br /> } <br /> var url="gethint.php"<br /> url=url+"?q="+str<br /> url=url+"&sid="+Math.random()<br /> xmlHttp.onreadystatechange=stateChanged <br /> xmlHttp.open("GET",url,true)<br /> xmlHttp.send(null)<br /> } <br /> <br /> function stateChanged() <br /> { <br /> if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")<br /> { <br /> document.getElementById("txtHint").innerHTML=xmlHttp.responseText <br /> <br /> } <br /> }<br /> <br /> function GetXmlHttpObject()<br /> {<br /> var xmlHttp=null;<br /> try<br /> {<br /> // Firefox, Opera 8.0+, Safari<br /> xmlHttp=new XMLHttpRequest();<br /> }<br /> catch (e)<br /> {<br /> // Internet Explorer<br /> try<br /> {<br /> xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");<br /> }<br /> catch (e)<br /> {<br /> xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br /> }<br /> return xmlHttp;<br /> }
<?php<br /> $q=$_GET["q"];<br /> <br /> //$select ="select * from test where cid='$q'";<br /> <br /> $hint="for($i=0;$i<2;$i++){......}";<br /> <br /> //Set output to "no suggestion" if no hint were found<br /> //or to the correct values<br /> if ($hint == "")<br /> {<br /> $response="no suggestion";<br /> }<br /> else<br /> {<br /> $response=$hint;<br /> }<br /> <br /> //output the response<br /> echo $response;<br /> ?>