Home >Backend Development >PHP Tutorial >关于变量与循环语句的有关问题

关于变量与循环语句的有关问题

WBOY
WBOYOriginal
2016-06-13 11:39:101015browse

关于变量与循环语句的问题
各位大牛,消息想问个关于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>

js clienthint代码
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 />}

处理页面代码gethint.php
<?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 />?>

因为在处理页面$hint 是需要传到前端页面的,所以我希望在处理页面将循环内容都做好之后传递,但是我这样写:$hint="for($i=0;$i所以拜托大家有没有什么好的办法解决一下
  在线等,谢谢各位了!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn