我将数据发送到另一个php文件,然后成功检索数据以显示在div中。但是当我加载我的data.php脚本时,通常会显示通知"未定义的索引:状态在...."代码上的那一行是$ var = $ _POST [' status'];。
这是重新发布/编辑,因为脚本现在实际上有效,但我不明白为什么通知仍在显示?!
<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>
<!-- SKELETON -->
</div>
</div>
</body>
&#13;
我的数据文件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) {
die("Connection Failed: " . $conn->connect_error);
} else {
}
$sql = "INSERT INTO `question`(id, question) VALUES ('', '{$var}')";
$result = $conn->query($sql);
if($result) {
}
else {
echo "failed: " . $conn->error;
}
echo "{$var}" ;
?>
&#13;
答案 0 :(得分:1)
添加以下行/检查
$var = '';
if(isset($_POST['status'])){
$var = $_POST['status'];
}
答案 1 :(得分:0)
试试这个,
<?php
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) {
die("Connection Failed: " . $conn->connect_error);
} else {
}
if(isset($_POST['status']))
{
$var = $_POST['status'];
$sql = "INSERT INTO `question`(id, question) VALUES ('', '{$var}')";
$result = $conn->query($sql);
if($result) {
}
else {
echo "failed: " . $conn->error;
}
echo "{$var}" ;
}
?>