如何使用ajax和jquery重定向URL?
问题描述:
我正在尝试在数据提交上重定向URL.这是我的PHP代码:
I am trying redirect URL on data submit. Here is my PHP code:
<?php
$error = array();
$user_id=$_SESSION['email'];
$apply_id=$_SESSION['id'];
$purpose_amount=$_POST['purpose_amount'];
$amount=$_POST['amount'];
$cheque_name=$_POST['cheque_name'];
if(empty($amount)){
$error[] = "Please Enter Expected Amount";
}
elseif(empty($cheque_name)){
$error[] = "Please Enter Issue Cheque in Favour of";
}
if (count($error)) {
echo "<i class='fa fa-exclamation'> ";
echo implode('<br>', $error);
echo "</i>";
}
else{
$result=mysqli_query($conn, "UPDATE education SET amount='$amount', cheque_name='$cheque_name', purpose_amount='$purpose_amount', date='$date' WHERE id='$apply_id'")or die("Could not insert data: " .mysqli_error($conn));
echo 1;
}
?>
这是JQuery和AJAX代码:
And here is JQuery and AJAX code:
$('.btn_education_step5').click(function(){
var formdata=new FormData($('#education_step5')[0]);
$.ajax({
url:'includes/backend_education_amount.php',
method: "POST",
async: false,
cache: false,
contentType: false,
processData: false,
data : formdata,
success:function(answer_from_actionpage){
if(answer_from_actionpage == 1){
Redirect url code??????
}else{
$('.error').html(answer_from_actionpage);
}
}
})
});
提交数据后,我想重定向URL,例如abc.php?id=$user_id
我可以在success:function()
中做些什么来重定向URL.
When my data is submitted I want to redirect URL like this abc.php?id=$user_id
What can I do in success:function()
to redirect URL.
答
您可以使用以下JavaScript脚本将当前页面重定向到任何url
You can redirect current page to any url by using following JavaScript script
window.location.href = "http://www.example.com";
因此,在您的if语句中,请使用上述脚本进行重定向
So in your if statement use above script for redirection
if(answer_from_actionpage == 1){
window.location.href = "http://www.example.com";
}