这是我的Ajax代码,我可以很容易地提交我的表单的PHP页面 后端如何通过AJAX重定向到不同的页面在PHP
<script type="text/javascript">
$("#contact-form").submit(function(e){
e.preventDefault();
$.ajax({
url: 'customerdata.php',
async: true,
cache: false,
data: $('#contact-form').serialize(),
type:'post',
success: function(response) {
document.getElementById("loader").style.display = "none";
if(response.hasOwnProperty('id')) {
window.location.href = 'index.php?booking=success&booking_id=' + response.id + '&user=' + response.email;
}
}
});
});
</script>
现在,这里是我的PHP代码,
<?php
$name=trim(htmlspecialchars($db -> real_escape_string($_POST['name'])));
$mail=trim(htmlspecialchars($db -> real_escape_string($_POST['email'])));
$phone=trim(htmlspecialchars($db -> real_escape_string($_POST['important_number'])));
$booking="ZOHO".crypto_rand_secure(10000,100000);
$typeofcab=trim(htmlspecialchars($db -> real_escape_string($_POST['the_cab'])));
$alt=trim(htmlspecialchars($db -> real_escape_string($_POST['alternate_number'])));
$drop=trim(htmlspecialchars($db -> real_escape_string($_POST['drop_address'])));
$pickup=trim(htmlspecialchars($db -> real_escape_string($_POST['pickup_address'])));
$source=trim(htmlspecialchars($db -> real_escape_string($_SESSION["source"])));
$destination= trim(htmlspecialchars($db -> real_escape_string($_SESSION["destination"])));
$type=trim(htmlspecialchars($db -> real_escape_string($_SESSION['type']))) ;
$date=trim(htmlspecialchars($db -> real_escape_string($_POST['journey_date'])));
$gotime= trim(htmlspecialchars($db -> real_escape_string($_SESSION["time"])));
$price= trim(htmlspecialchars($db -> real_escape_string($_SESSION["price"])));
$status="Pending";
$takenby="Pending";
$avail="Pending";
$driver="Pending";
$carnumber="Pending";
$password=$phone=trim(htmlspecialchars($db -> real_escape_string($_POST['important_number'])));
$need=trim(htmlspecialchars($db -> real_escape_string($_POST['need'])));
$vendorprice = price($price);
if(!isset($_POST['return_date'])){
$return='NOT APPLICABLE';
$returntime='NOT APPLICABLE';
}
else {
$return=$_POST['return_date'];
$returntime=$_SESSION["timeo"];
}
$stmt=$db->prepare("SELECT `email` FROM `users` WHERE email=?");
$stmt->bind_param("s",$mail);
$rock=$stmt->execute();
$stmt->bind_result($email);
while($stmt->fetch()) {
}
if($email !=$mail)
{
$query=$db->prepare("INSERT INTO `customerdata`(`vendorprice`,`price`,`name`, `email`, `phone`, `bookingid`, `source`, `destination`, `sourceaddress`, `destinationaddress`, `alternate`, `typeofjourney`, `journeydate`, `returndate`, `journeytime`, `returntime`, `status`, `availability`, `takenby`, `cabtype`, `drivername`, `carnumber`, `customerneed`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$query->bind_param("sssssssssssssssssssssss",$vendorprice,$price,$name,$mail,$phone,$booking,$source,$destination,$pickup,$drop,$alt,$type,$date,$return,$gotime,$returntime,$status,$avail,$takenby,$typeofcab,$driver,$carnumber,$need);
$run=$query->execute();
$query_user=$db->prepare("INSERT INTO `users`(`password`,`email`, `phone`, `bookings`,`name`) VALUES (?,?,?,?,?)");
$query_user->bind_param("sssss",$password,$mail,$phone,$booking,$name);
$run_query=$query_user->execute();
$mailsent=sendmail($name,$mail,$booking,$type,$date,$gotime,$source,$destination,$price,$password,$typeofcab);
if ($run && $run_query) {
echo json_encode(array("id" => $booking, "email" => $email));
// session_destroy();
//header('Location: index.php?booking=success&booking_id='.$booking."&user=".$email."price=".$price."phone=".$phone);
//exit;
}
else {
echo json_encode(array("error" => mysqli_error($db)));
// echo("Error description: " . mysqli_error($db));
// header('Location:index.php?failed');
// exit;
}
}
else {
// $yourURL="bookingconfirmation.php?route=oneway";
//echo ("<script>alert('This email already exists with us,please try different email'); location.href='$yourURL'</script>"); // redirect with javascript, after page loads
echo json_encode(array("failed" => $email));
//echo "this email already exists";
}
?>
我要的是,当查询sucessfull,我想我的 用户重定向到不同的页面使用Ajax,就像我与PHP中使用头做 前,通过给所有neccessary URL参数,任何帮助 将非常感激牛逼
你需要做的是,你已经拥有了同样的方式在你的代码中,使用'window.location.href = ...'。您不能在此使用服务器端重定向 - 因为您在此处发出_background_请求,并且您在HTTP级别上执行的所有操作都在此处保持“在后台”。因此,如果您的PHP脚本动态确定目标URL,则必须将该值传递回您的AJAX脚本,然后将其指定为新的位置。 – CBroe
您可以尝试 'window.location.replace('index.php?booking = success&booking_id ='+ id +'&user ='+ email);' 但我不是100%确定您期望看到什么。你想添加来自Ajax响应的头来重定向请求吗? – inser
与之前提到的问题相同,您认为'id'和'email'来自哪里? ;)@inser – eisbehr