【问题标题】:Codeigniter pass value from controller to view with ajax requestCodeigniter 将值从控制器传递到使用 ajax 请求查看
【发布时间】:2013-01-30 14:12:25
【问题描述】:

我是 CI 新手,想将 json 编码值传递给查看以进行进一步处理,请参见下文,

jQuery 帖子:

$('#btn_reset_password').click(function(){
    var parameters = $('#reset_password_form').serialize();
    //alert(parameters);
    $.ajax({
        url: baseurl + 'site/check_email_exist',
        type: 'POST',
        data: parameters,
        dataType: 'json',
        success: function(output_str){
            if(output_str.flag == "true"){
                // redirect to change password page
                window.location.replace(baseurl + 'site/change_user_password');

            }else if(output_str == "false"){
                $('#result_msg').html("Your email is not in our database");

            }else{
                $('#result_msg').html(output_str);  
            }
        }
    });

});

控制器:

class Site extends CI_Controller {

public function change_user_password() {
    $this->load->view('header');
    $this->load->view('change_user_password');
    $this->load->view('footer');
}


public function check_email_exist(){
    $email = $this->input->post('email');

    if(!empty($email)){
        $query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');

        if($query->num_rows() > 0){
            $row = $query->row();

            $output_str = array('email' => $row->email,
                                'flag' => 'true'
                                );

            //$this->load->view('change_user_password', $output_str);
            //$output_str = "true";

        }else{
            $output_str = "false";
        }

    }else{
        $output_str = "Email cannot leave blank";
    }

    echo json_encode($output_str);
}

}

查看文件'change_user_password.php':

<?php 
//$obj = json_decode($output_str);
echo $email;
?>

<p><div id="result_msg"></div></p>
<p>
<form id="reset_password_form">
<label><b>New Password: </b></label>
<input type="password" name="new_password" style="width:250px;" />
<input type="button" id="btn_change" name="" value="Change" /><br />
</form>
</p>

如何传递/检索数组 $output_str 中的电子邮件值以查看“change_user_password.php”?我尝试了很多方法,但仍然没有解决这个问题,请帮助,非常感谢。

添加:

是不是 Codeigniter 不能接受这种方式的 url 传递参数 => 页面?email=abc@xyz.com?

【问题讨论】:

  • 我没有关注这里.. 你想说的是 jquery 与change_user_password.php 的观点不同吗?
  • @mamdouh jquery 请求邮件是否返回 true,如果是,将重定向到 change_user_password 视图。
  • 而你的 window.location.request() 究竟是为了什么..?
  • 试试我的答案。它应该有帮助
  • codeigniter 使用 url 中的第三个参数作为函数参数见this

标签: json ajax codeigniter


【解决方案1】:

好的,试试这个:

在您的控制器中:

public function check_email_exist(){
    $email = $this->input->post('email');

    if(!empty($email)){
        $query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');

        if($query->num_rows() > 0){
            $row = $query->row();

            $output_str = array('email' => $row->email,
                                'flag' => 'true'
                                );

            //$this->load->view('change_user_password', $output_str);
            echo json_encode($output_str);

        }else{
            $output_str = "false";
        }

    }else{
        $output_str = "Email cannot leave blank";
    }

然后在你的 jquery 中:

$('#btn_reset_password').click(function(){
    var parameters = $('#reset_password_form').serialize();
    //alert(parameters);
    $.ajax({
        url: baseurl + 'site/check_email_exist',
        type: 'POST',
        data: parameters,
        dataType: 'json',
        success: function(output_str){
            if(output_str.flag == "true"){
                // redirect to change password page
                window.location.replace(baseurl + 'site/change_user_password/'+output_str.email);

            }else if(output_str == "false"){
                $('#result_msg').html("Your email is not in our database");

            }else{
                $('#result_msg').html(output_str);  
            }
        }
    });

});

最后change_password 函数应该是这样的:

public function change_user_password($email = '') {
$data['email'] = $email;
    $this->load->view('header');
    $this->load->view('change_user_password', $data);
    $this->load->view('footer');
}

更新:

如果你愿意,你可以使用会话:

public function check_email_exist(){
    $email = $this->input->post('email');

    if(!empty($email)){
        $query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');

        if($query->num_rows() > 0){
            $row = $query->row();

            $output_str = array('email' => $row->email,
                                'flag' => 'true'
                                );

            //$this->load->view('change_user_password', $output_str);
            $this->session->set_userdata('email',$row->email);
            //echo json_encode($output_str);

        }else{
            $output_str = "false";
        }

    }else{
        $output_str = "Email cannot leave blank";
    }

然后在您的视图中检索它:

$this->session->userdata('email');

因此您不必在字符串查询中传递电子邮件。

【讨论】:

  • 页面显示错误,“遇到错误您提交的 URI 包含不允许的字符。”我认为是别名“@”导致错误。
  • 非常感谢您,您的更新示例非常完美!但是当我在 change_user_password 视图中时,URL 显示为'localhost/project/site/change_user_password/undefined`
  • @conmen - 只需从您的 jquery 中删除 +output_str.email :)
  • 我们不应该看到来自控制器层的任何查询。我们还应该看到实现了 Active Record 查询构建技术,而不是直接将用户值注入到 sql 查询中。如果您只是检查电子邮件是否存在,那么您不需要*num_rows();而是直接在 sql 中获取 COUNT()
  • 如果我知道我的答案不正确/具有误导性,我永远不会使用这种防御方法。帖子的年龄完全无关紧要,因为研究人员不在乎何时提出问题或何时回答问题。只有在 Stack Overflow 包含有用、正确的建议时,它才是对研究人员有用的资源;其他任何事情都是浪费宝贵的时间。
猜你喜欢
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
相关资源
最近更新 更多