我试图在提交进入数据库后让我的页面刷新,但它不会。
这是代码
PHP页面......
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["username"]) || !isset($_POST["message"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$username = filter_var(trim($_POST["username"]), FILTER_SANITIZE_STRING);
$message = filter_var(trim($_POST["message"]), FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($username)<4) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short!'));
die($output);
}
if(strlen($message)<5) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short message!'));
die($output);
}
require('config.php');
$sql="INSERT INTO guestbook(name, comment, datetime) VALUES('$username','$message', now())";
header("Location:thanks.php");
}
?>
这是JS页面
$(document).ready(function(){
$("#submit").click(function() {
//get input field values
var name = $('#name').val();
var message = $('#comment').val();
var flag = true;
/********validate all our form fields***********/
if(name==""){
$('#name').css('border-color','red');
flag = false;
}
if(message=="") {
$('#comment').css('border-color','red');
flag = false;
}
/********Validation end here ****/
/* If all are ok then we send ajax request to email_send.php *******/
if(flag)
{
$.ajax({
type: 'post',
url: "thanks.php",
dataType: 'json',
data: 'username='+name+'&message='+message,
beforeSend: function() {
$('#submit').attr('disabled', true);
$('#submit').after('<span class="wait"> <img src="image/loading.gif" alt="" /></span>');
},
complete: function() {
$('#submit').attr('disabled', false);
$('.wait').remove();
},
success: function(data)
{
if(data.type == 'error')
{
output = '<div class="error">'+data.text+'</div>';
}else{
output = '<div class="success">'+data.text+'</div>';
$('input[type=text]').val('');
$('#guestform textarea').val('');
}
$("#result").hide().html(output).slideDown();
}
});
}
});
//reset previously set border colors and hide all message on .keyup()
$("#guestform input, #guestform textarea").keyup(function() {
$("#guestform input, #guestform textarea").css('border-color','');
$("#result").slideUp();
});
});
我把标题信息放在PHP中,但每次点击提交时,它都会将数据保存在数据库中然后只停留在页面上... textarea中的文本也会保留在那里..我希望它能刷新页面所以它看起来像一个空白的形式。
答案 0 :(得分:0)
如果您想要刷新整个页面,可以在回调函数结束时使用reload()发出请求。
location.reload();
答案 1 :(得分:0)
你可以编辑你的php文件,
$sql= mysqli_query($db, "INSERT INTO guestbook(name, comment, datetime) VALUES('$username','$message', now())");
// if you using mysqli.
if($sql){
header("Location:thanks.php");
}else{
$output = json_encode(array('type'=>'error', 'text' => 'failed to insert data'));
die($output);
}
没有mysql_query / mysqli_query或您使用的任何内容,您的查询都无法执行。
和你的AJAX
$.ajax({
type: 'post',
url: "thanks.php",
dataType: 'json',
data: 'username='+name+'&message='+message,
beforeSend: function() {
$('#submit').attr('disabled', true);
$('#submit').after('<span class="wait"> <img src="image/loading.gif" alt="" /></span>');
},
complete: function() {
$('#submit').attr('disabled', false);
$('.wait').remove();
},
success: function(data)
{
if(data.type == 'error')
{
output = '<div class="error">'+data.text+'</div>';
$("#result").hide().html(output).slideDown();
}
else
{
output = '<div class="success">'+data.text+'</div>';
$("#result").hide().html(output).slideDown();
$('input[type=text]').val('');
$('#guestform textarea').val('');
// reload
location.reload();
}
}
});