【问题标题】:How to return the result using AJAX in CodeIgniter?如何在 CodeIgniter 中使用 AJAX 返回结果?
【发布时间】:2017-03-09 05:38:42
【问题描述】:

我想问一下如何使用 AJAX 在我的视图上返回 return 'Password successfully updated'; 或 return 'Failed to Update Record'; 之类的响应。在提交它返回响应之后,它在没有 AJAX 之前工作正常。

现在我只是在我的表单中包含 AJAX,表单工作正常。但是提交后没有显示错误和成功消息(响应)。

阿贾克斯

 $(document).ready(function(){
    $('form.jsForm').on('submit', function(e){
        e.preventDefault();
        $.post('change_password_process', $('form.jsForm').serialize(), function(data){
            $('div.response').html(data);
        });
    });
});

型号

public function updatePassword($data){ //update user password
    $oldPassword = $data['oldPassword'];
    $user = $data['user'];

    $this->db->select('password')
             ->from('users')
             ->where('username', $user);
    $query = $this->db->get();
    $row = $query->row();

     if($this->bcrypt->check_password($oldPassword, $row->password)){
        if($this->db->set('password', $data['newPassword'])
                        ->where('username', $user)
                        ->update('users')){
            return 'Password successfully updated';
        } else {
            return 'Failed to Update Record';
        }

    } else {
        return 'Old password is incorrect!';
    }
}

控制器

if($this->form_validation->run() == TRUE){

        $data = array(
                'user' => $this->input->post('username'),
                'oldPassword' => $this->input->post('oldPassword'),
                'newPassword' => $this->bcrypt->hash_password($this->input->post('newPassword')),
                'cPassword' => $this->input->post('cPassword')
        );

        //this is the response from MODEL 

     $this->session->set_flashdata('response',$this->user_accounts_model->updatePassword($data)); 
        //redirect('user_accounts/change_password_form');

    } else {
        $this->change_password_form();
    }
}

查看

<?php if($response = $this->session->flashdata('response')) :?>

    <div class="alert alert-dismissible alert-success">
      <?=$response?>
    </div>
<?php endif; ?> //this is the form result w/out ajax

<div class="response"></div> //this is using ajax

【问题讨论】:

  • 你的change_password_process()函数在哪里。请同时发布这个函数。在模型中尝试echo而不是return。
  • 哦,是的,哈哈,我刚刚尝试过,然后才看到你的答案。也谢谢你。
  • 控制台出现任何错误。
  • 没有错误 :) 我忘了我需要使用 echo 而不是 return hehe
  • oho 那你就解决了。恭喜

标签: ajax codeigniter


【解决方案1】:

在模型中只是 echo 而不是 return。

if($this->db->set('password', $data['newPassword'])
                        ->where('username', $user)
                        ->update('users')){
            echo 'Password successfully updated';
        } else {
            echo 'Failed to Update Record';
        }

    } else {
        echo 'Old password is incorrect!';
    }

【讨论】:

  • =form_hidden('username', $user['username']);?> =form_password(['name' =>'oldPassword', 'class'=>'form-control', 'placeholder'=>'旧密码', 'autofocus'=>'autofocus']);?>
    =form_error('oldPassword');?>
  • 在我的文本框旁边
  • 尝试使用echo form_error('fieldName', '&lt;div class="error"&gt;', '&lt;/div&gt;');
  • 它仍在页面顶部。
  • 你可以使用&lt;?php echo validation_errors(); ?&gt;来显示错误。更多请看这里codeigniter.com/userguide3/libraries/form_validation.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
相关资源
最近更新 更多