我有这个脚本,必须将输入的数据发送到处理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的新手,但我相信对于那些已经使用它一段时间的人来说,这是一个非常简单的问题。
答案 0 :(得分:0)
$header = $mysqli->real_escape_string(htmlentities($_POST['header']));
$content = $mysqli->real_escape_string(nl2br(htmlentities($_POST['content'])));
答案 1 :(得分:0)
使用此功能。
<button id="whatever" onclick="sendData()">Send</button>
<script>
function sendData(){
var header = jQuery('.header_input').val();
var content = jQuery('.content_input').val();
jQuery.ajax({
type: 'POST',
url: 'parts/post_functions.php',
data: {
header: header,
content: content
},
success: function( data ) {
alert(data);
}
});
}
</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 = mysqli_real_escape_string($con,htmlentities($_POST['header']));
$content = mysqli_real_escape_string($con,nl2br(htmlentities($_POST['content'])));
$sql = mysqli_query($con,"INSERT INTO posts (Header, Content) VALUES
('$header','$content')");
if($sql)
{
echo 'done';
}
else
{
echo 'error';
}
mysqli_close($con);
?>
答案 2 :(得分:0)
你可以做这样的事情
<script>
function sendData() {
var header = $('.header_input').val();
var content = $('.content_input').val();
var dataString = 'header='+header'&content='+content;
$.ajax({
type: 'POST',
url: 'parts/post_functions.php',
data: dataString,
success: function(data) {
alert(data);
}
});
}
</script>
现在在PHP页面中,您可以通过
访问值<?php
$header = $_POST['header'];
$content = $_POST['content'];
// ANYTHING THAT YOU `echo` here will be alerted in jquery alert
?>
希望你有所了解