【发布时间】:2014-09-29 14:44:44
【问题描述】:
我在 codeigniter 中重定向到另一个页面时遇到问题,这是我的 js:
<script type="text/javascript">
$(document).ready(function () {
var url = $('#baseurl').val();
var form = $('#customerinfo');
$('#next').click(function (e) {
if ($("form")[0].checkValidity()) {
// Prevent submit.
e.preventDefault();
//Start loading
var checkbox = $('#Accept');
if (checkbox[0].checked == true)
{
$.post(url + 'customerinfo/next', form.serialize(), function(response) {window.location.href=url + 'paymentinfo';});
}
else
{
$("#errmsg .msg").text("You need to read and accept the terms and conditions before you can continue!");
$("#errmsg").css("display", "block");
}
$(".loading-add-ticket").css("display", "block");
// Send request.
}
else console.log ( "invalid form" );
});
});
</script>
它的作用: 单击下一个按钮时,它通过 ajax 提交表单,然后在服务器上处理表单,完成后,用户将使用以下代码块重定向:
<?php
class CustomerInfo extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// Write to $title
$this->template->write('title', 'Customer Info');
// Write to $content
$this->template->write_view('content', 'tickets/customerinfo');
// Write to $sidebar
$this->template->write_view('sidebar', 'sidebar');
// Render the template
$this->template->render();
}
function next()
{
if ($_POST) {
if (isset($_POST['Accept']))
{
$data[0] = array(
'TravelInsurance' => $_POST['TravelInsurance'],
'LuggagePayment' => $_POST['LuggagePayment'],
'Donations' => $_POST['Donations'],
'FirstName' => $_POST['FirstName'],
'LastName' => $_POST['LastName'],
'CityTown' => $_POST['CityTown'],
'ContactNo' => $_POST['ContactNo'],
'Address' => $_POST['Address'],
'Accept' => $_POST['Accept']
);
$this->session->set_userdata('customerinfo', $data);
redirect(site_url('paymentinfo'));
}
}
}
}
?>
问题: 重定向永远不会发生,当我在浏览器中使用 firebug 检查发布响应时,我注意到目标页面已返回:
我想要什么:我需要将页面重定向到目标页面,我是 codeigniter 的新手,所以我不知道我做错了什么。
当前修复: 在我的 ajax 函数的成功函数中,我使用 javascript 重定向到下一页,但我不希望在客户端处理重定向。
【问题讨论】:
-
然后直接提交,不要使用
$.post(记住ajax是异步的)...干杯 -
是控制器中以函数 next() 开头的代码块吗?你能张贴表格吗?
-
是的。我已经添加了完整的控制器代码。
-
你能发布你的html表单吗?
-
没关系,罗伯特的回答对我有用。谢谢o7
标签: php jquery ajax codeigniter redirect