PHP中如何根据所填表格内容 查询数据库 并自动更新另一个表格 有点复杂,具体来说比如网页上有两个文本框:A和B,还有一个SQL数据库。用户在A中填入了一串数字,填写完毕后(onChange)网页自动在数据库内搜索该数字,然后把返回值自动更新到B里。全过程页面不能刷新,只用文本框B自动刷新。单用JS的话可以通过onChange() + document.value实现自动刷新文本框,但是一连接数据库搜索就出问题。不知道搜索数据库这里能不能用PHP来完成。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用AJAX到后台去请求数据,再把得到的数据显示在B中。 onchange事件中写AJAX取数据,显示在B上就OK~! 如果用Ajax 返回过来的数据是一组html代码 怎么把里面的有意义的内容显示出来?JS代码如下var xmlHttpfunction checkpc(str){ xmlHttp=GetXmlHttpObject()if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return }var url="tt.php"url=url+"?q="+strurl=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("txtHint").innerHTML=xmlHttp.responseTextdocument.form1.addr.value= unescape(xmlHttp.responseText) //document.getElementByName("addr").innerHTML=xmlHttp.responseText } }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;}用来显示结果的文本框的代码如下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script src="ttt.js"></script></head><body><form id="form1" name="form1" method="post" action=""> <div id="txtHint"> <input type="postcode" name="postcode" onchange="checkpc(this.value)" value="" /></div> <textarea name="addr"></textarea> </form></div></body></html>最后到文本框里的内容是<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>??????</title></head><body>Allanville, Camperdown</body></html>怎样让Allanville, Camperdown这一句话显示在文本框里?而不是上面这一片? tt.php你的这个页面返回的内容太多了吧,你把他设成只返回 Allanville,Camperdown 就 ok 了。比如:tt.php<? if($_GET['q'] == "条件") exit("Allanville, Camperdown");?>不过,建议 lz 最好用 xml 格式生成数据。 后台只写对你这个请求有意义的代码呀!用AJAX返回的数据需要做编码转换,用js的decodeURIComponent方法。 感谢楼上几位这些代码是从网上找来的 我这个程序是用户像一个文本框里输入内容 然后在数据库里搜索 同时将返回值返回到另一个文本框里 现在问题是返回到文本框里的是一串html代码 怎么能从这html代码里把具体想要的值取出来?连接搜索数据库的代码如下<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'root', '');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("takeaway", $con);$sql="SELECT * FROM postcodes WHERE id = '".$q."'";$result = mysql_query($sql);while($row = mysql_fetch_array($result)) { echo $row['Street']; }mysql_close($con);?> 数据库中存储的html吧!用正则表达式可以把html中的内容取出来。 通过隐藏div和innerText已经解决了 感谢各位 為何不能發email了? 大家想想 echo (int)( ( 0.1 + 0.7 ) * 10 )是多少? EPP如何配置 提问:php写的socket服务器端程序,性能、效率跟稳定性如何? .htaccess中重定向问题 【80分】求一php正则替换函数和.htaccess伪静态规则 高手请指教,odbc_rollback这个函数该如何用?? 一个很简单的问题 请大家帮我看看下面的代码为什么无法实现? 请问如何得到用户的游览器的信息 有没有人知道怎么批量查询啊 ISAPI_Rewrite 防盗链的正则问题. 150分(全部的分都压上了)
function checkpc(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="tt.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("txtHint").innerHTML=xmlHttp.responseText
document.form1.addr.value= unescape(xmlHttp.responseText)
//document.getElementByName("addr").innerHTML=xmlHttp.responseText
}
}
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;
}用来显示结果的文本框的代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="ttt.js"></script>
</head><body><form id="form1" name="form1" method="post" action=""> <div id="txtHint"> <input type="postcode" name="postcode" onchange="checkpc(this.value)" value="" /></div> <textarea name="addr"></textarea>
</form></div>
</body>
</html>最后到文本框里的内容是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>??????</title>
</head><body>
Allanville, Camperdown
</body>
</html>怎样让Allanville, Camperdown这一句话显示在文本框里?而不是上面这一片?
<?
if($_GET['q'] == "条件")
exit("Allanville, Camperdown");
?>
不过,建议 lz 最好用 xml 格式生成数据。
用AJAX返回的数据需要做编码转换,用js的decodeURIComponent方法。
$q=$_GET["q"];$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("takeaway", $con);
$sql="SELECT * FROM postcodes WHERE id = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['Street'];
}
mysql_close($con);
?>