我發送數據到另一個php文件,然後數據檢索成功顯示在一個div。然而,當我加載我的data.php腳本通知總是顯示「未定義的索引:狀態....」,在代碼上的行是$ var = $ _POST ['狀態'];。JQuery/AJAX腳本可以工作,但通知仍然顯示?
這是一個重新發布/編輯,因爲腳本實際上現在工作,但我只是不明白爲什麼通知仍然顯示?!?
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//daddy code
$ (document).ready(function() {
//mama code
$("button#postbutton").click(function() {
var data = $("#formpost").serialize();
$.ajax({
type: "POST",
url: "data.php",
data: data,
success: function(data) {
$("#statustext").html(data);
}
});
});
});
</script>
</head>
<body>
<div id="global">
<form id="formpost" action="" method="post" onsubmit="return false">
<textarea id="text1" name="status" ></textarea>
<button id="postbutton">POST</button>
<a href="logout.php">LOGOUT</a>
</form>
<br/>
<br/>
<div id="allstatus">
<!-- SKELETON -->
<div id="wholestatus">
<div id="statuspic">
</div>
<div id="statusinfo">
<div id="statusname">JOnathan</div>
<div id="statustext"> </div>
<div id="statusoption"><button id="likestatus">LIKE</button></div>
<div id="statusoption"><button id="commentstatus">COMMENT</button></div>
<div id="statusoption"><button id="sharestatus">SHARE</button></div>
<div id="statusoption"><button id="statustime">TIME</button></div>
</div>
</div> \t
<!-- SKELETON -->
</div>
</div>
</body>
我的數據文件data.php
<?php
$var = $_POST['status'];
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = '';
const DB_NAME = 'forum';
//connecting
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($conn->connect_error) {
\t die("Connection Failed: " . $conn->connect_error);
} else {
}
$sql = "INSERT INTO `question`(id, question) VALUES ('', '{$var}')";
$result = $conn->query($sql);
if($result) {
\t
}
else {
\t echo "failed: " . $conn->error;
}
echo "{$var}" ;
?>
很顯然,你不會在你的AJAX POST參數傳遞'status'變量。這就是爲什麼它是未定義的。請使用瀏覽器的開發人員工具查看通過AJAX實際傳遞給服務器的內容。 – 2015-02-06 10:58:48