PHP中遇到AJAX的有關問題

來源:互聯網
上載者:User
PHP中遇到AJAX的問題
header("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代碼.
php?ajax? php ajax

分享到:


------解決方案--------------------
你請求的是當前頁面嗎
------解決方案--------------------
那不是錯誤資訊嗎?
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;
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.