我无法弄清楚为什么 PHP无法获取AJAX发送的数据。这是我的代码:
index.js
var receiver;
function setUser(obj){
$("#user_inf").css({"display":"none"});
$("#selected").attr('name', obj.value);
$("#selected").attr('value', obj.value);
$("#selected").html(obj.value);
$("#selected").css({"display":"block"})
receiver = obj.value;
$.ajax({
type: 'POST',
data : {data:receiver},
url: 'index.php',
success: function(data){
alert("Data sent");
}
});
}
function getVal(){
return receiver;
}
index.php
<?php
$rec = $_POST['data'];
//include 'db.php';
$query="SELECT * FROM chit where sender='$myusername' and receiver='$rec' ORDER BY id";
$run=$con1->query($query);
while($row=$run->fetch_array()) :
?>
<div id="chat_data">
<span style="color:green"><?php echo $row['name']?></span>:
<span style="color:brown"><?php echo $row['message']?></span>
<span style="float:right"><?php echo formatDate($row['date'])?></span>
</div>
<?php endwhile;?>
</div>
<div id="textarea">
<form method="post" action="index.php">
<input type="hidden" value="getVal()" name="receiver">
<textarea type="text" name="message" id="message"></textarea>
<input type="checkbox" name="veb" id="via">
<label id="chktxt">Via Executive Board</label>
<input type="button" id="sndmsg" name="submit" value="SEND" onclick="">
</form>
</div>
<?php
if(isset($_POST['submit']))
{
$sender=$myusername;
$receiver=$_POST['receiver'];
$message=$_POST['message'];
$eb=$_POST['veb'];
$query="INSERT INTO CHIT (sender,receiver,message,eb)values ('$sender','$receiver','$message','$eb')";
$run = $con1->query($query);
if($run)
echo "<embed loop='false' src='chat.wav' hidden='true' autoplay='true'/>";
}
?>
这是我得到的错误 致命错误:未捕获错误:在C:\ xampp \ htdocs \ chat \ home.php中调用boolean上的成员函数fetch_array():84堆栈跟踪:#0 {main}抛出 C:\ xampp \ htdocs \ chat \ home.php 在线 2
另外:该表单在网页中的任何位置都不可见。它甚至不在页面的源代码中。