首頁 >後端開發 >php教程 > PHP中遇到AJAX的有关问题

PHP中遇到AJAX的有关问题

WBOY
WBOY原創
2016-06-13 12:35:16834瀏覽

PHP中遇到AJAX的问题

<?php<br />
header("Content-Type:html/text;Charset=utf-8");<br />
$name = $_POST['name'];<br />
$email = $_POST['email'];<br />
$comment = $_POST['comment'];<br />
$fh = @fopen("14-6.txt","ab");<br />
fwrite($fh,"姓名:".$name,strlen($name));<br />
fwrite($fh,"email:".$email,strlen($email));<br />
fwrite($fh,"评论:".$comment,strlen($comment));<br />
fclose($fh);<br />
echo 1;<br />
?>

function $(id){<br />
	return document.getElementById(id);<br />
}<br />
function addcomment(){<br />
	var url = "14-6.php";<br />
	var status = document.getElementById("divmsg");<br />
	status.value="正在提交》》》";<br />
	var param = "name="+$("name").value+"email="+$("email").value+"comment="+$("comment").value;<br />
	xmlhttp_request.onreadystatechange = function(){<br />
		if(xmlhttp_request.readyState==4 && xmlhttp_request.status == 200){//相应完全显示信息<br />
			alert (xmlhttp_request.responseText);<br />
			if(xmlhttp_request.responseText == "1"){<br />
				status.value = "发表成功!!";<br />
				$("name").value="";<br />
				$("email").value="";<br />
				$("comment").value="";<br />
			}else{<br />
				status.value = "发表失败!请重新发表!";<br />
			}<br />
		}	<br />
	}<br />
	xmlhttp_request.open("POST",url,true);<br />
	xmlhttp_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置头信息<br />
	xmlhttp_request.send(param);<br />
}

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

php?ajax? php ajax
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn