0

我是 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 传递参数 => page?email=abc@xyz.com 吗?

4

1 回答 1

0

好的,试试这个:

在您的控制器中:

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');

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

于 2013-01-30T14:33:39.480 回答