Home >Backend Development >PHP Tutorial >PHP中遇到AJAX的问题
php ajax php ajax
<?phpheader("Content-Type:html/text;Charset=utf-8");$name = $_POST['name'];$email = $_POST['email'];$comment = $_POST['comment'];$fh = @fopen("14-6.txt","ab");fwrite($fh,"姓名:".$name,strlen($name));fwrite($fh,"email:".$email,strlen($email));fwrite($fh,"评论:".$comment,strlen($comment));fclose($fh);echo 1;?>
function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交》》》"; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert (xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}
上面就是alert(xmlhttp_request.responseText)返回的东西
你请求的是当前页面吗
那不是错误信息吗?
14-6.php 第 4 行的 $_POST['email'] 没有定义
var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value;
应写作
var param = "name="+$("name").value+" &email="+$("email").value+" &comment="+$("comment").value;
我的是从一个html文件转到php文件的。
html代码在下面。我的一共才两个文件,一个html文件,一个php文件
<!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=utf-8" /><title>Insert title here</title><script type="text/javascript">var xmlhttp_request = false; //定义变量try{ if(window.ActiveXObject){ //判断浏览器是否是ActiveXObject对象 for(var i=5;i;i--){ //根据IE浏览器版本使用不同方法创建 try{ if(i==2){ xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP"); }else{ xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP."+i+".0"); xmlhttp_request.setRequestHeader("Content-Type","text/xml"); xmlhttp_request.setRequestHeader("Charset","utf-8"); } break; }catch(e){ xmlhttp_request = false; } } }else if(window.XMLHttpRequest){ //判断浏览器是否支持XMLHttpRequest对象 xmlhttp_request = new XMLHttpRequest(); if(xmlhttp_request.overrideMimeType){ xmlhttp_request.overrideMimeType('text/xml'); } }}catch(e){ xmlhttp_request = false;}function $(id){ return document.getElementById(id);}function addcomment(){ var url = "14-6.php"; var status = document.getElementById("divmsg"); status.value="正在提交...."; var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value; xmlhttp_request.onreadystatechange = function(){ if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息 alert(xmlhttp_request.responseText); if(xmlhttp_request.responseText == "1"){ status.value = "发表成功!!"; $("name").value=""; $("email").value=""; $("comment").value=""; }else{ status.value = "发表失败!请重新发表!"; } } } xmlhttp_request.open("POST",url,true); xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息 xmlhttp_request.send(param);}</script></head><body><form name="form" id="form" method="post" action=""> <p> </p> <p> </p> <p> </p> <p> </p> <table width="400" height="168" border="0" align="center" cellpadding="2" cellspacing="0"> <caption>发表评论</caption> <tr> <td >用户名:</td> <td align="center"> <lable> <input type="text" name="name" id="name" size="30" maxlength="20"/> </lable> </td> </tr> <tr> <td >email:</td> <td align="center"> <lable> <input type="text" name="email" id="email" size="30" maxlength="20"> </lable> </td> </tr> <tr> <td>评论:</td> <td align="center"> <textarea name="comment" id="comment" rows="6" cols="30"></textarea> </td> </tr> <tr> <td colspan="2" align="center"> <lable> <input type="button" name="submit" value="发表" id="submit" onclick="addcomment();"/> </lable> <lable> <input id="divmsg" name="divmsg" value="" readonly /> </lable> </td> </tr> </table> </form></body></html>
问题已解决,就是版主说的错误。提交时三个不同的变量之间用&相隔,因为刚刚接触AJAX,所以对POST提交不是很了解。