我通过AJAX将所有数据从一个页面丢弃到另一个页面,而我在MySQL中的表格正在获取我发送的所有数据。这意味着在header()
之前只有一行,我的所有代码都很好但我的标题没有重定向到页面。但是我也尝试了Javascript window.location
。我个人更喜欢标题,因为如果某人禁用了Javascript windows.location
无法正常工作。我想这就是我使用header方法的原因。
我写的代码如下。如果有人能帮助我,我将不胜感激。
if(isset($_POST['recaptcha'])){
$secret = "something";
$response = $_POST['recaptcha'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
$content = json_decode($url, TRUE);
if($content['success'] ==1){
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$fname = test_input($_POST["fname"]);
$lname = test_input($_POST["lname"]);
$email = test_input($_POST["email"]);
$cname = test_input($_POST["cname"]);
$discount = test_input($_POST["discount"]);
$website = test_input($_POST["website"]);
$address = test_input($_POST["address"]);
$desc = test_input($_POST["desc"]);
$expirydate = test_input($_POST["expirydate"]);
$servername = "localhost";
$username = "user";
$password = "something";
try {
$conn = new PDO("mysql:host=$servername;dbname=something", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT INTO table(firstname, lastname, email, companyname, discountoffer, websitename, companyaddress, description, expirydate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$statement = $conn->prepare($query);
$statement->execute(
array(
$fname,
$lname,
$email,
$cname,
$discount,
$website,
$address,
$desc,
$expirydate
) );
$conn = null;
header("Location: http://somethingpage.com/");
exit;
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
exit;
}
if($content['success'] !=1){
echo "notok";
exit;
$conn = null;
}
}
这是AJAX在这里工作我发送所有"形式"通过ajax上面的数据它也工作正常,因为如果我不进行重新接收它会返回错误,我的html块在屏幕上呈现,这意味着我的最后一个块也执行正常。这是ajax的代码
"use strict";
$(document).ready(function(){
var fname;
var fnameerr;
var lname;
var lnameerr;
var email;
var emailerr;
var cname;
var cnameerr;
var discount;
var discounterr;
var website;
var websiteerr;
var address;
var addresserr;
var expirydate;
var expirydateerr;
var desc;
var descerr;
var recaptcha;
var recaptchaerr;
var errbox
var errcontent
function showerror(errbox, errcontent){
if($(errbox).hasClass("hidden")){
$(errbox).removeClass("hidden");
$(errbox).html(errcontent);
}
}
function hideerror(errbox, errcontent){
if(!$(errbox).hasClass("hidden")){
$(errbox).html(errcontent);
$(errbox).addClass("hidden");
}
}
var alphabets = /^[a-zA-Z.,]*$/;
$("#fname").keyup(function(){
fname = $('#fname').val();
if(!alphabets.test(fname)){
errbox = $("#fnameerrbox");
errcontent = "Only alphabets are allowed in name field";
showerror(errbox, errcontent);
}
if(alphabets.test(fname)){
errbox = $("#fnameerrbox");
errcontent = "";
hideerror(errbox, errcontent);
fnameerr = "no";
}
});
$("#lname").keyup(function(){
lname = $('#lname').val();
if(!alphabets.test(lname)){
errbox = $("#lnameerrbox");
errcontent = "Only alphabets are allowed in name field";
showerror(errbox, errcontent);
}
if(alphabets.test(lname)){
errbox = $("#lnameerrbox");
errcontent = "";
hideerror(errbox, errcontent);
lnameerr = "no";
}
});
var emailfilter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
$("#email").focusout(function(){
email = $('#email').val();
if(!emailfilter.test(email)){
errbox = $("#emailerrbox");
errcontent = "invalid email";
showerror(errbox, errcontent);
$("#email").focus();
keepcheckingemail();
}
if(emailfilter.test(email)){
errbox = $("#emailerrbox");
errcontent = "";
hideerror(errbox, errcontent);
emailerr = "no";
}
});
function keepcheckingemail(){
$("#email").keyup(function(){
email = $('#email').val();
if(!emailfilter.test(email)){
errbox = $("#emailerrbox");
errcontent = "invalid email";
showerror(errbox, errcontent);
}
if(emailfilter.test(email)){
errbox = $("#emailerrbox");
errcontent = "";
hideerror(errbox, errcontent);
emailerr = "no";
}
});
}
var alphanumeric = /^[a-zA-Z 0-9.,]*$/;
$("#cname").keyup(function(){
cname = $('#cname').val();
if(!alphanumeric.test(cname)){
errbox = $("#cnameerrbox");
errcontent = "Only alphabets & numbers are allowed in company name field";
showerror(errbox, errcontent);
}
if(alphanumeric.test(cname)){
errbox = $("#cnameerrbox");
errcontent = "";
hideerror(errbox, errcontent);
cnameerr = "no";
}
});
var dis = /^[a-zA-Z 0-9.,%]*$/;
$("#discount").keyup(function(){
discount = $('#discount').val();
if(!dis.test(discount)){
errbox = $("#dnameerrbox");
errcontent = "Only alphabets, numbers & % are allowed in company name field";
showerror(errbox, errcontent);
}
if(dis.test(discount)){
errbox = $("#dnameerrbox");
errcontent = "";
hideerror(errbox, errcontent);
discounterr = "no";
}
});
$("#website").keyup(function(){
website = $('#website').val();
if(!alphanumeric.test(website)){
errbox = $("#weberrbox");
errcontent = "https not allowed. write, for ex. homesolver.ca OR www.homesolver.ca";
showerror(errbox, errcontent);
}
if(alphanumeric.test(website)){
errbox = $("#weberrbox");
errcontent = "";
hideerror(errbox, errcontent);
websiteerr = "no";
}
});
$("#address").keyup(function(){
address = $('#address').val();
if(!alphanumeric.test(address)){
errbox = $("#adderrbox");
errcontent = "Only alphabets & numbers are allowed in company address field";
showerror(errbox, errcontent);
}
if(alphanumeric.test(address)){
errbox = $("#adderrbox");
errcontent = "";
hideerror(errbox, errcontent);
addresserr = "no";
}
});
var todaydate = new Date();
$("#expirydate").datepicker({
autoclose: true,
format: "yyyy/mm/dd",
startDate: todaydate
});
$("#expirydate").change(function () {
expirydate = $("#expirydate").val();
expirydateerr = "no";
});
$("#desc").keyup(function(){
desc = $('#desc').val();
if(!alphanumeric.test(desc)){
errbox = $("#deserrbox");
errcontent = "Only alphabets & numbers are allowed in description field";
showerror(errbox, errcontent);
}
if(alphanumeric.test(desc)){
errbox = $("#deserrbox");
errcontent = "";
hideerror(errbox, errcontent);
descerr = "no";
}
});
$("#promotionsubmit").click(function(){
if((descerr !="no") || (expirydateerr !="no") || (addresserr !="no") || (discounterr!="no") || (cnameerr!="no") || (emailerr !="no") || (lnameerr !="no") || (fnameerr !="no")){
errbox = $("#robotError");
errcontent = "All * fields are mendatory";
showerror(errbox, errcontent);
}
if((descerr == "no") && (expirydateerr =="no") && (addresserr =="no") && (discounterr =="no") && (cnameerr =="no") && (emailerr =="no") && (lnameerr =="no") && (fnameerr =="no")){
errbox = $("#robotError");
errcontent = " ";
hideerror(errbox, errcontent);
$.ajax({
url:"somepage.php",
method:"POST",
data:{recaptcha: grecaptcha.getResponse(), fname:fname, lname:lname, email:email, cname:cname, discount:discount, address:address, desc:desc, website:website, expirydate: expirydate},
success: function(data){
$data = data;
if(data == "notok"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "please verify you are not robot";
showerror(errbox, errcontent);
}
}
});
}
});
});
答案 0 :(得分:1)
您无法使用header()直接从ajax脚本重定向用户。
当您在AJAX中调用PHP脚本时,请求由AJAX调用执行,而不是客户端所在的当前页面,因此它不会更改实际正在查看的页面上的标题。
因此,您需要在成功回调函数中处理来自ajax的响应。然后从那里重定向用户
$.ajax({
url:"somepage.php",
method:"POST",
data:{recaptcha: grecaptcha.getResponse(), fname:fname, lname:lname, email:email, cname:cname, discount:discount, address:address, desc:desc, website:website, expirydate: expirydate},
success: function(data){
$data = data;
if(data == "ok"){
window.location='someotherpage.php'; //redirect user from here
}
}
});
希望这有帮助。