【发布时间】:2012-02-27 03:03:53
【问题描述】:
没有适合这个错误的标题,但它是这样的,当按下 jquery ajax 调用控制器时,我有一个带有提交按钮的表单,如果它失败,则表单验证完成如果它通过,则重新绘制表单页面被重定向到带有 Flash 消息成功的主页,这就是错误发生的地方,它会在内容中重绘整个页面(页眉页眉页脚页脚)。我希望眼见为实是有道理的,所以这里是代码
旁注:“autform”是用于创建表单的库,“rest”是用于模板的库。
jquery 代码:
$("form.user_form").live("submit",function() {
$("#loader").removeClass('hidden');
$.ajax({
async :false,
type: $(this).attr('method'),
url: $(this).attr('action'),
cache: false,
data: $(this).serialize(),
success: function(data) {
$("#center").html(data);
$('div#notification').hide().slideDown('slow').delay(20000).slideUp('slow');
}
})
return false;
});
控制器
function forgot_password()
{
$this->form_validation->set_rules('login',lang('email_or_login'), 'trim|required|xss_clean');
$this->autoform->add(array('name'=>'login', 'type'=>'text', 'label'=> lang('email_or_login')));
$data['errors'] = array();
if ($this->form_validation->run($this)) { // validation ok
if (!is_null($data = $this->auth->forgot_password(
$this->form_validation->set_value('login')))) {
$this-> _show_message(lang('auth_message_new_password_sent'));
} else {
$data['message']=$this-> _message(lang('error_found'), false); // fail
$errors = $this->auth->get_error_message();
foreach ($errors as $k => $v){
$this->autoform->set_error($k, lang($v));
}
}
}
$this->autoform->add(array('name'=>'forgot_button', 'type'=>'submit','value' =>lang('new_password')));
$data['form']= $this->autoform->generate('','class="user_form"');
$this->set_page('forms/default', $data);
if ( !$this->input->is_ajax_request()) { $this->rest->setPage(''); }
else { echo $this->rest->setPage_ajax('content'); }
}
}
function _show_message($message, $state = true)
{
if($state)
{
$data = '<div id="notification" class="success"><strong>'.$message.'</strong></div>';
}else{
$data = '<div id="notification" class="bug"><strong>'.$message.'</strong></div>';
}
$this->session->set_flashdata('note', $data);
redirect(base_url(),'refresh');
}
我认为好像重定向调用被 ajax 捕获,而不是向我发送主页,而是在表单的位置加载主页。
感谢您的帮助 问候
【问题讨论】:
标签: php jquery ajax codeigniter redirect