我有一個按鈕onclick="sendNews()"
和一個PHP腳本,它可以完成數據庫工作。 問題是當sendNews
運行時,$_POST
陣列爲空。爲什麼ajax不能使用POST方法?
的Javascript:
function sendNews()
{
var title=document.getElementById("title").innerHTML;
var body=document.getElementById("boddy").innerHTML;
var params="title="+title+"&body="+body;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//Send the proper header information along with the request
xmlhttp.open("POST","sendnews.php",true);
xmlhttp.send(params);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("newsAddResult").innerHTML=xmlhttp.responseText;
}
}
}
PHP:
<?php
include("../inc/functions.php");
if(!loginCheck())
header('Location: index.php');
[email protected]$_POST['title'];
[email protected]$_POST['body'];
var_dump($_POST);
$q=sprintf("insert into `news`(`id`,`title`,`body`,`time`) values(NULL,'%s','%s','%s')",$title,$body,jgmdate('l jS F Y h:i:s A'));
mysql_query($q) or die("خطا".mysql_error());
echo "با موفقیت ارسال شد";
?>
在哪裏的問題?
你的代碼乍看起來很好看,所以它更像是一個關於我們看不見/知道的事情的問題? 「params」的內容是什麼? '#title'和'#boddy'是什麼樣的元素?如果它們是表單元素,則可能需要'value'而不是'innerHTML'。 – 2012-01-01 00:31:17
如果修改'sendnews.php'來將'$ _POST'內容轉儲到文件中,它仍然是空白的嗎? – sarnold 2012-01-01 00:32:52
順便提一句,你正在請求一個名爲'boddy'的對象的'.innerHTML' - 是一個錯字還是故意的? – sarnold 2012-01-01 00:33:42