我正在创建一个页面,单击发布按钮,该页面将位于输入下方,并将输入数据存储到数据库中,但是遇到上述问题! 我在下面共享我的代码
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
function get(){
var input = $("#cmt").val();
$.post('postToPage.php',{comment:input},function(output){
$("#post-data").prepend(output+"<br><hr>")});
}
</script>
<div class="container" style="margin-top:20px;">
<div class="form-group">
<form name="frm" >
<input type="text" class="form-control" name="cmt" id="cmt" placeholder='Why So Empty!! Please Post Something'>
<input class="btn btn-primary" type="button" onclick="get();" value="Post">
</form>
</div>
<br>
<br>
<!--Here the post data will be visible-->
<div id="post-data">
</div>
</div>
postToPage.php文件
<?php
require_once('db.php');
$cmt = $_POST['comment'];
$query = "INSERT INTO post (id,body) VALUES ('','$cmt')";
mysqli_query($conn,$query);
echo $cmt;
?>
数据库配置文件db.php
<?php
$conn = mysqli_connect("localhost","root","","signup_data");
?>
请显示错误。谢谢
答案 0 :(得分:1)
尝试以下代码
<script>
function get(){
var input = $("#cmt").val();
$.ajax({
type: 'post',
url: 'postToPage.php',
data: {
comment: input,
},
success: function (response) {
document.getElementById("post-data").innerHTML = response;
}
});
}
</script>