這是我的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