我有这个脚本,它必须将输入的数据发送到处理 php 页面
<button id="whatever" onclick="sendData()">Send</button>
<script>
function sendData() {
var header = $('.header_input').val();
var content = $('.content_input').val();
$.ajax({
type: 'POST',
url: 'parts/post_functions.php',
data: {
header: header,
content: content
}
});
}
</script>
处理页面如下所示:
<?php
$con=mysqli_connect("localhost", "root", "shit", "data");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$header = mysql_real_escape_string(htmlentities($_POST['header']));
$content = mysql_real_escape_string(nl2br(htmlentities($_POST['content'])));
$sql = mysqli_query($con,"INSERT INTO posts (Header, Content) VALUES
('{$header}','{$content}')");
mysqli_close($con);
?>
那么我如何正确接收数据,当然,将其发送到查询?
我对 AJAX 完全陌生,但我相信对于那些已经使用它一段时间的人来说,这是一个非常简单的问题。