我使用了没有表格的ajax帖子。当我单击提交按钮时,它应该获取其他字段的值。没有发送发布请求,也没有显示输出。它返回此错误Unexpected identifier
以下是输入字段
<input type="text" name="name" id="name" class="center-block Ename" placeholder="Enter you name">
<textarea class="center-block" name="message" id="message" rows="1" placeholder="Enter your message"></textarea>
<input class="center-block sendBtn" type="submit" id="submit" name="submit" value="Submit">
这是ajax请求。
$(document).ready(function(){
var interval = setInterval($('#submit').click(function(){
var values = {
'name': document.getElementById('name').value,
'message': document.getElementById('message').value
};
$.ajax({
type: "POST",
url: "chat.php",
data: values,
success: function(data){
$("#chat").html(data);
}
});
}),1000);
});
它正在向此php页面发送请求
<?php
include 'db.php';
//Here post data is being assigned to variables
$name = $_POST['name'];
$message = $_POST['message'];
$queryInsert = "INSERT INTO chat(`name`, `message`) VALUES('$name', '$message')";
$queryInsertRun = mysqli_query($con, $queryInsert);
if(!$queryInsertRun){
echo mysqli_error($con);
}
//Here is the output which should be shown
$query = "SELECT * FROM `chat` ORDER BY `name` AND `message` DESC ";
$queryRun = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($queryRun)){
$name = $row['name'];
$message = $row['message'];
?>
<span class="name" style="font-weight: bold"><?php echo $name?>:</span>
<span class="message"><?php echo $message.'<br>'?></span>
<hr>
<?php
}
?>
我想知道为什么这不起作用。
答案 0 :(得分:1)
声音您的JSON数据无效。
var data = JSON.stringify(values);
var request = $.ajax({
url: "script.php",
method: "POST",
data: data,
dataType: "html" // read about dataType
});
答案 1 :(得分:1)
答案 2 :(得分:1)
如果单击“提交”按钮,则需要显示数据,使用简单的单击功能。使用setInterval无法帮助您单击提交。 的 JS 强>
$(document).ready(function(){
$('#submit').click(function(){
var values = {
'name': document.getElementById('name').value,
'message': document.getElementById('message').value
};
$.ajax({
type: "POST",
url: "chat.php",
data: values,
success: function(data){
$("#chat").html(data);
}
});
});
});