PHP中遇到AJAX的问题

php中文网
发布: 2016-06-23 14:24:39
原创
1034人浏览过

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);}
登录后复制

不知道为什么,这个responseText返回的是一大面的html代码.

回复讨论(解决方案)

上面就是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>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1182">
                            <img src="https://img.php.cn/upload/ai_manual/001/431/639/68b7af235f31d971.png" alt="AI建筑知识问答">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1182">AI建筑知识问答</a>
                            <p>用人工智能ChatGPT帮你解答所有建筑问题</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="AI建筑知识问答">
                                <span>22</span>
                            </div>
                        </div>
                        <a href="/ai/1182" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="AI建筑知识问答">
                        </a>
                    </div>
                		<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提交不是很了解。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号