请教无刷新验证输入!!!! 一般都是用ajax技术实现的. 现在有很多这样的代码, 搜一下 ajax 关键字吧. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 onchange事件搜索 XmlHttp onblur()<div id="ajax"></div>------<script language="javascript">function loadXMLDoc() { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); } if (req) { req.onreadystatechange = processReqChange; req.open("GET", "xxxxx.php", true); req.send(null); }}function processReqChange(){ var ready=req.readyState; var data=null; if (ready==4){ if(req.responseText) document.getElementById('ajax').innerHTML=req.responseText; getsc(); } }</script>------xxxxx.php来判断是否已存在------然后再getElementById('ajax')里面的东西就行了。 在文本框的onblur事件里写代码,用xmlhttp发送用户名到后台判断返回值,更新图片。 可以写个简单的例子吗,例如在文本框输入0和1,在XXX.php里判断,如果是0的话就在文本框旁边显示显示一个Y.gif的图片,如果是1的话就显示N.gif的图标,急急急,谢谢谢谢! ciaky(小五) 已经贴出代码了.够详尽了 onBlur = loadXMLDoc();req.open("GET", "xxx.php?postValue="+你文本框里的值, true);然后 xxx.php 判断$_GET['postValue']的值 就是PHP是决断了后如何把值传回去。 req.onreadystatechange = processReqChange;这个就是处理回值的。你可以做个简单测试xxx.php-----------switch($_GET){case 0: echo "oh yeah!";break;case 1: echo "oh no!";break;}processReqChange函数下===if (req.readyState == 4 && req.statues == 200){ alert(req.responseText);}看看onblur后alert出什么东西!? 没有反应!!!!--- test.html---<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">function loadXMLDoc() { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); } if (req) { req.onreadystatechange = processReqChange; req.open("GET", "test.php?postValue="+form1.text.value, true); req.send(null); }}function processReqChange(){ if (req.readyState == 4 && req.statues == 200){ alert(req.responseText); }}</script></head><body><form name="form1" method="post"><input type="text" name="text" id='text' onBlur="loadXMLDoc();"><div id="ajax"></div></form> </body></html>--- test.php---<? switch($_GET['postValue']) case 0: echo "oh yeah!"; break; case 1: echo "oh no!"; break; }?> ajax可以,不过目前没用过,关注 IFRAME我都想过,但可以传值回主页吗? 在文本框的onblur事件里写代码,用xmlhttp发送用户名到后台判断返回值,更新图片。 问下啊。ajax运行是否需要什么服务器安装什么软件呢?asp也可以实现类似功能吗?好像.net实现很简单啊。 在刚才的代码里,在两个判断后面再加上如果是“0”就$a = "你选对了!",如果是“1”就$a = “你选错了!”,在test.html的第一个输入框后面再加多一个文本是接收$a的,那应该如何处理? 如果你的判断页里只有这两个输出内容,那么假设你的text的id为test,你可以用document.getElementById('test').value=req.responseText; 我的意思是在test.html里有两个文本框(a\b)和一个<div>,当在a里输入内容失去焦点就在test.php里决断,根据结果在<div>里插入不同的图片,然后在b框里显示出$b的值,如果输入了1,那a\b框都会清空。---test.html---<input type='text' id='a' onblur='loadXMLDoc();'><div id='ajax'></div><input type='text' id='b'>---test.php--<?switch($_GET['postValue']){case 0: echo "oh yeah!"; $b = "你选对了!"; break;case 1: echo "oh no!"; $b = "你选错了!"; break;}?> 那这样根本就不用上面复杂,你上面说要判断用户是否存在,那要与数据库之类的交互,如果像你上面说的,你就可以这样。<script>function test(){ if(document.getElementById('a').value==0){ document.getElementById('ajax').innerHTML="<img scr='xxxx'>"; document.getElementById('b').value=document.getElementById('a').value; }else{ document.getElementById('a').value=''}</script>如果$b是在数据库等读取的话,就要在判断页面输出那个$b才行。 function processReqChange(){ if (req.readyState == 4 && req.statues == 200){ document.getElementById('b').value = req.responseText; }}---test.php--<?switch($_GET['postValue']){case 0: $b = "你选对了!"; echo $b; break;case 1: //echo "oh no!"; $b = "你选错了!"; break;}?> function processReqChange(){ if (req.readyState == 4 && req.statues == 200){ document.getElementById('div2').innerHTML = req.responseText; document.getElementById('b').value = document.getElementById('b1').value; }}---test.php--<?switch($_GET['postValue']){case 0: $b = "<input type=hidden id='b1' value='你选对了!'>"; break;case 1: $b = "<input type=hidden id='b1' value=''>"; break;}------在原来的页面加一个隐藏层,id为div2echo $b;?> 你有QQ或MSN吗,我发一个代码文件给你看。 你应该去下个xmlhttp的手册看一看先。 我msn是[email protected]不过到晚上才能上线 看了,但看不明,这要改对吗?---test.html---<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">function loadXMLDoc() { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); } if (req) { req.onreadystatechange = processReqChange; req.open("GET", "test.php?postValue="+form1.text1.value, true); req.send(null); }}function processReqChange(){ if (req.readyState == 4){ document.getElementById('ajax').innerHTML = req.responseText; form1.text2.value = document.getElementById('b1').value; }}</script></head><body><form name="form1" method="post"><table><tr> <td><input type="text" name="text1" id="text1" onBlur="if(this.value!=''){loadXMLDoc();}"></td> <td><div id="ajax"></div><div id="ajax2"></div></td> <td><input type="text" name="text2" id="text2"></td></tr></table></form> </body></html>---test.php---<? $con = mysql_connect("localhost","root",""); mysql_select_db("test", $con); $res = mysql_fetch_array(mysql_db_query("sf","SELECT * FROM table WHERE id='".$_GET['postValue']."'")); if ($res) { echo "<img src='image/right.png' width='13' height='13' />"; $b = "<input type='hidden' id='b1' value='".$res["YARNDESC"]."'>"; } else { echo "<img src='image/error.png' width='13' height='13' />"; $b = "<input type='hidden' id='b1' value=''>"; }?> 需要的问题都解决了,但这里有个问题,就是在postValue前面加多个row传给PHP,但为什么不能第一取出?---test.html---<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">function loadXMLDoc(num) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); } if (req) { req.onreadystatechange = processReqChange; req.open("GET", "b.php?row="+num+"&postValue="+document.getElementById('a'+num).value, true); req.send(null); }}function processReqChange(){ if (req.readyState == 4){ if (req.responseText) { //alert(document.getElementById('txtRow').value); document.getElementById('ajax1').innerHTML = req.responseText; document.getElementById('b1').value = document.getElementById('txtYARNDESC').value; document.getElementById('c1').value = document.getElementById('txtCOMPOPCT').value; if (!document.getElementById('txtYARNDESC').value) { document.getElementById('a1').value = ""; document.getElementById('a1').focus(); } } }}</script></head><body><form name="form1" method="post"><table><tr> <td><input type="text" name="a1" id="a1" onBlur="if(this.value!=''){loadXMLDoc(1);}"></td> <td><div id="ajax1"></div></td> <td><input type="text" name="b1" id="b1" readonly></td> <td><input type="text" name="c1" id="c1" readonly></td></tr><tr> <td><input type="text" name="a2" id="a2" onBlur="if(this.value!=''){loadXMLDoc(2);}"></td> <td><div id="ajax2"></div></td> <td><input type="text" name="b2" id="b2" readonly></td> <td><input type="text" name="c2" id="c2" readonly></td><tr> <td><input type="text" name="a3" id="a3" onBlur="if(this.value!=''){loadXMLDoc(3);}"></td> <td><div id="ajax3"></div></td> <td><input type="text" name="b3" id="b3" readonly></td> <td><input type="text" name="c3" id="c3" readonly></td></tr></table></form> </body></html>---test.php---<? require_once("linkdata.php"); $res = mysql_fetch_array(mysql_db_query("sf","SELECT * FROM table WHERE id='".$_GET['postValue']."'")); if ($res) { echo "<input type='hidden' name='txtRow' id='txtRow' value='".$_GET['row']."'>"; echo "<img src='image/right.png' width='13' height='13' />"; echo "<input type='hidden' name='txtYARNDESC' id='txtYARNDESC' value='".$res["Name"]."'>"; echo "<input type='hidden' name='txtCOMPOPCT' id='txtCOMPOPCT' value='".$res["Re"]."'>"; } else { echo "<input type='hidden' name='txtRow' id='txtRow' value='".$_GET['row']."'>"; echo "<img src='image/error.png' width='13' height='13' />"; echo "<input type='hidden' name='txtYARNDESC' id='txtYARNDESC' value=''>"; echo "<input type='hidden' name='txtCOMPOPCT' id='txtCOMPOPCT' value=''>"; }?> 把它写成一条也会出错。---test.html---<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">function loadXMLDoc(num) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); } if (req) { req.onreadystatechange = processReqChange; req.open("GET", "a.php?aa="+num, true); req.send(null); }}function processReqChange(){ if (req.readyState == 4){ document.getElementById('b1').value= document.getElementById('txtAA').value; }}</script></head><body><form name="form1" method="post"><table> <tr> <td><input type="text" name="a1" id="a1" onBlur="if(this.value!=''){loadXMLDoc(1);}"></td> <td><input type="text" name="b1" id="b1" readonly></td> </tr></table></form></body></html>--test.php--<? echo "<input type='hidden' name='txtAA' id='txtAA' value='".$_GET["aa"]."'>";?> 按条件选择查询 PHP如何设置周期性日程安排 fopen函数求解 关于24小时计时制怎么换算成am/pm的格式? 请大家帮忙,网站的反馈表让人发了几千条垃圾回复 php程序放在D盘上传视频文件能否上传到E盘 apache 2.0好用还是1.0好用?php 4.3.2好用还是以前的好用?我被php的版本都搞乱套了。 php为什么安上后不能用 兄弟们辛苦了,进来看看! php文件上传 问个很简单,但我确不明白的问题! 关于php中使用 javascript代码(两个下拉框 a的选择会影响b的选择)
搜索 XmlHttp
<div id="ajax"></div>
------
<script language="javascript">
function loadXMLDoc() {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", "xxxxx.php", true);
req.send(null);
}
}function processReqChange(){
var ready=req.readyState;
var data=null;
if (ready==4){
if(req.responseText)
document.getElementById('ajax').innerHTML=req.responseText;
getsc();
}
}
</script>
------
xxxxx.php来判断是否已存在
------
然后再getElementById('ajax')里面的东西就行了。
onBlur = loadXMLDoc();
req.open("GET", "xxx.php?postValue="+你文本框里的值, true);
然后 xxx.php 判断$_GET['postValue']的值
你可以做个简单测试
xxx.php
-----------
switch($_GET)
{
case 0:
echo "oh yeah!";
break;case 1:
echo "oh no!";
break;
}processReqChange函数下
===
if (req.readyState == 4 && req.statues == 200)
{
alert(req.responseText);
}看看onblur后alert出什么东西!?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function loadXMLDoc() {
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); }
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", "test.php?postValue="+form1.text.value, true);
req.send(null);
}
}function processReqChange(){
if (req.readyState == 4 && req.statues == 200){ alert(req.responseText); }
}
</script>
</head><body>
<form name="form1" method="post">
<input type="text" name="text" id='text' onBlur="loadXMLDoc();"><div id="ajax"></div>
</form>
</body>
</html>--- test.php---
<?
switch($_GET['postValue'])
case 0:
echo "oh yeah!";
break;
case 1:
echo "oh no!";
break;
}
?>
ajax运行是否需要什么服务器安装什么软件呢?
asp也可以实现类似功能吗?
好像.net实现很简单啊。
你可以用document.getElementById('test').value=req.responseText;
---test.html---
<input type='text' id='a' onblur='loadXMLDoc();'><div id='ajax'></div><input type='text' id='b'>---test.php--
<?
switch($_GET['postValue']){
case 0:
echo "oh yeah!";
$b = "你选对了!";
break;
case 1:
echo "oh no!";
$b = "你选错了!";
break;
}
?>
function test(){
if(document.getElementById('a').value==0){
document.getElementById('ajax').innerHTML="<img scr='xxxx'>";
document.getElementById('b').value=document.getElementById('a').value;
}else{
document.getElementById('a').value=''
}
</script>如果$b是在数据库等读取的话,就要在判断页面输出那个$b才行。
if (req.readyState == 4 && req.statues == 200){
document.getElementById('b').value = req.responseText;
}
}---test.php--
<?
switch($_GET['postValue']){
case 0:
$b = "你选对了!";
echo $b;
break;
case 1:
//echo "oh no!";
$b = "你选错了!";
break;
}
?>
if (req.readyState == 4 && req.statues == 200){
document.getElementById('div2').innerHTML = req.responseText;
document.getElementById('b').value = document.getElementById('b1').value;
}
}---test.php--
<?
switch($_GET['postValue']){
case 0:
$b = "<input type=hidden id='b1' value='你选对了!'>";
break;
case 1:
$b = "<input type=hidden id='b1' value=''>";
break;
}
------
在原来的页面加一个隐藏层,id为div2echo $b;
?>
不过到晚上才能上线
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function loadXMLDoc() {
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); }
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", "test.php?postValue="+form1.text1.value, true);
req.send(null);
}
}function processReqChange(){
if (req.readyState == 4){
document.getElementById('ajax').innerHTML = req.responseText;
form1.text2.value = document.getElementById('b1').value;
}
}
</script>
</head><body>
<form name="form1" method="post">
<table>
<tr>
<td><input type="text" name="text1" id="text1" onBlur="if(this.value!=''){loadXMLDoc();}"></td>
<td><div id="ajax"></div><div id="ajax2"></div></td>
<td><input type="text" name="text2" id="text2"></td>
</tr>
</table>
</form>
</body>
</html>---test.php---
<?
$con = mysql_connect("localhost","root","");
mysql_select_db("test", $con);
$res = mysql_fetch_array(mysql_db_query("sf","SELECT * FROM table WHERE id='".$_GET['postValue']."'"));
if ($res) {
echo "<img src='image/right.png' width='13' height='13' />";
$b = "<input type='hidden' id='b1' value='".$res["YARNDESC"]."'>";
}
else {
echo "<img src='image/error.png' width='13' height='13' />";
$b = "<input type='hidden' id='b1' value=''>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function loadXMLDoc(num) {
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); }
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", "b.php?row="+num+"&postValue="+document.getElementById('a'+num).value, true);
req.send(null);
}
}function processReqChange(){
if (req.readyState == 4){
if (req.responseText) {
//alert(document.getElementById('txtRow').value);
document.getElementById('ajax1').innerHTML = req.responseText;
document.getElementById('b1').value = document.getElementById('txtYARNDESC').value;
document.getElementById('c1').value = document.getElementById('txtCOMPOPCT').value;
if (!document.getElementById('txtYARNDESC').value) {
document.getElementById('a1').value = "";
document.getElementById('a1').focus();
}
}
}
}
</script>
</head><body>
<form name="form1" method="post">
<table>
<tr>
<td><input type="text" name="a1" id="a1" onBlur="if(this.value!=''){loadXMLDoc(1);}"></td>
<td><div id="ajax1"></div></td>
<td><input type="text" name="b1" id="b1" readonly></td>
<td><input type="text" name="c1" id="c1" readonly></td>
</tr>
<tr>
<td><input type="text" name="a2" id="a2" onBlur="if(this.value!=''){loadXMLDoc(2);}"></td>
<td><div id="ajax2"></div></td>
<td><input type="text" name="b2" id="b2" readonly></td>
<td><input type="text" name="c2" id="c2" readonly></td>
<tr>
<td><input type="text" name="a3" id="a3" onBlur="if(this.value!=''){loadXMLDoc(3);}"></td>
<td><div id="ajax3"></div></td>
<td><input type="text" name="b3" id="b3" readonly></td>
<td><input type="text" name="c3" id="c3" readonly></td>
</tr>
</table>
</form>
</body>
</html>---test.php---
<?
require_once("linkdata.php");
$res = mysql_fetch_array(mysql_db_query("sf","SELECT * FROM table WHERE id='".$_GET['postValue']."'"));
if ($res) {
echo "<input type='hidden' name='txtRow' id='txtRow' value='".$_GET['row']."'>";
echo "<img src='image/right.png' width='13' height='13' />";
echo "<input type='hidden' name='txtYARNDESC' id='txtYARNDESC' value='".$res["Name"]."'>";
echo "<input type='hidden' name='txtCOMPOPCT' id='txtCOMPOPCT' value='".$res["Re"]."'>";
}
else {
echo "<input type='hidden' name='txtRow' id='txtRow' value='".$_GET['row']."'>";
echo "<img src='image/error.png' width='13' height='13' />";
echo "<input type='hidden' name='txtYARNDESC' id='txtYARNDESC' value=''>";
echo "<input type='hidden' name='txtCOMPOPCT' id='txtCOMPOPCT' value=''>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function loadXMLDoc(num) {
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Msxml2.XMLHTTP"); }
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", "a.php?aa="+num, true);
req.send(null);
}
}function processReqChange(){
if (req.readyState == 4){
document.getElementById('b1').value= document.getElementById('txtAA').value;
}
}
</script>
</head><body>
<form name="form1" method="post">
<table>
<tr>
<td><input type="text" name="a1" id="a1" onBlur="if(this.value!=''){loadXMLDoc(1);}"></td>
<td><input type="text" name="b1" id="b1" readonly></td>
</tr>
</table>
</form>
</body>
</html>--test.php--
<?
echo "<input type='hidden' name='txtAA' id='txtAA' value='".$_GET["aa"]."'>";
?>