[英]Redirect to a other page after successful form submission through AJAX
I have created a simple form and store the values from the form in a database. 我创建了一个简单的表单并将表单中的值存储在数据库中。 This part is working fine now.
这部分现在工作正常。 Now I want to redirect user to a another page after successful form submission.
现在我想在成功提交表单后将用户重定向到另一个页面。 How can I do that?
我怎样才能做到这一点? Currently I am getting alert box saying successfully saved, but when I click that alert box it does not redirect to the other page (actually I want to redirect it to the page called first.php).
目前我正在获取成功保存的警告框,但是当我单击该警报框时,它不会重定向到另一个页面(实际上我想将其重定向到名为first.php的页面)。
Here is my code 这是我的代码
Controller 调节器
public function user_add() {
$data_save = array(
"Mnumber" => $this->input->post("Mnumber"),
"email" => $this->input->post("email"),
"fname" => $this->input->post("fname"),
"address" =>$this->input->post("address"),
"sitename" =>$this->input->post("sitename"),
/* "reqnum" => $this->input->post("reqnum"),*/
"title" => $this->input->post("title"),
"descr" => $this->input->post("descr"),
/*"payment" => $this->input->post("payment"),*/
"uniquekey" => $this->input->post("uniquekey")
/*"subscription" => $this->input->post("subscription"),
"email_sent" => $this->input->post("email_sent"),*/
);
if ($this->user_mod->AddUser($data_save)) {
echo "Successfully Saved";
}
else {
echo "error";
}
}
Model 模型
public function AddUser($data_save) {
if ($this->db->insert('users', $data_save)) {
return true;
} else {
return false;
}
}
View 视图
<script>
function save_user_new() {
var Mnumber = $('#Mnumber').val();
var email = $('#email').val();
var fname = $('#fname').val();
var address = $('#address').val();
var sitename = $('#sitename').val();
/*var reqnum = $('#reqnum').val();*/
var title = $('#title').val();
var descr = $('#descr').val();
var uniquekey = $('#uniquekey').val();
/*var subscription = $('#subscription').val();
var email_sent = $('#email_sent').val();
var payment = $('#payment').val();*/
if (sitename != "" && email != "") {
$.ajax({
type: "post",
async: false,
url: "<?php echo site_url('form_con/user_add'); ?>",
data: {
"Mnumber": Mnumber,
"email": email,
"fname": fname,
"address": address,
"sitename": sitename,
/*"reqnum": reqnum,*/
"title": title,
"descr": descr,
"uniquekey": uniquekey
/*"subscription": subscription,
"email_sent": email_sent,
"payment":payment*/
},
dataType: "html",
success: function (data) {
alert(data);
if (data == 'error') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Something wrong.");
} else if (data == 'have') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : This Sitename is already exists.");
} else {
$('#error_msg1').hide();
$('#success_msg').show();
$('#success_msg').html("User details successfully saved.");
}
}
});
} else {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Please enter User Details.");
}
}
</script>
Use window.location.href
or window.location.replace
. 使用
window.location.href
或window.location.replace
。
function save_user_new() {
var Mnumber = $('#Mnumber').val();
var email = $('#email').val();
var fname = $('#fname').val();
var address = $('#address').val();
var sitename = $('#sitename').val();
/*var reqnum = $('#reqnum').val();*/
var title = $('#title').val();
var descr = $('#descr').val();
var uniquekey = $('#uniquekey').val();
/*var subscription = $('#subscription').val();
var email_sent = $('#email_sent').val();
var payment = $('#payment').val();*/
if (sitename != "" && email != "") {
$.ajax({
type: "post",
async: false,
url: "<?php echo site_url('form_con/user_add'); ?>",
data: {
"Mnumber": Mnumber,
"email": email,
"fname": fname,
"address": address,
"sitename": sitename,
/*"reqnum": reqnum,*/
"title": title,
"descr": descr,
"uniquekey": uniquekey
/*"subscription": subscription,
"email_sent": email_sent,
"payment":payment*/
},
dataType: "html",
success: function(data) {
alert(data);
if (data == 'error') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Something wrong.");
} else if (data == 'have') {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : This Sitename is already exists.");
} else {
$('#error_msg1').hide();
$('#success_msg').show();
$('#success_msg').html("User details successfully saved.");
window.location.href = 'http://example.com/view-details'; // Keeping current page history
// or
window.location.replace = 'http://example.com/view-details'; // Current page history will be replaced
}
}
});
} else {
$('#success_msg').hide();
$('#error_msg1').show();
$('#error_msg1').html("Error : Please enter User Details.");
}
}
If you want the user to see your message, do this 如果您希望用户看到您的消息,请执行此操作
$('#success_msg').html("User details successfully saved.");
setTimeout(function() { location="first.php"},2000);
If you want to redirect using JavaScript and jQuery you can use the following properties of Javascript. 如果要使用JavaScript和jQuery重定向,可以使用Javascript的以下属性。
location.href = 'first.php';
OR 要么
location.replace('first.php');
place your code where you want to redirect to occure. 将代码放在要重定向到的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.