0

I new in CI and want to pass json encode value to view for further processing, please see below,

Jquery post:

$('#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);  
            }
        }
    });

});

controller:

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

}

View file '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>

How could I pass / retrive the email value which is in array $output_str to view 'change_user_password.php'? I had try many way but still never clear out this issue, please help and many thanks.

Added:

Is it Codeigniter couldn't accept url pass parameters in this way => page?email=abc@xyz.com?

Amaan Iqbal
  • 761
  • 2
  • 9
  • 25
conmen
  • 2,377
  • 18
  • 68
  • 98
  • I'm not following here.. is what you are trying to say that the jquery is in a different view than `change_user_password.php` ? – mamdouh alramadan Jan 30 '13 at 14:20
  • @mamdouh the Jquery is request whether the email is return true, if so will redirect to change_user_password view. – conmen Jan 30 '13 at 14:24
  • and your `window.location.request()` is for what exactly..? – mamdouh alramadan Jan 30 '13 at 14:27
  • try my answer. it should help – mamdouh alramadan Jan 30 '13 at 14:34
  • codeigniter uses the third parameter in the url as function argument see [this](http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri) – mamdouh alramadan Jan 30 '13 at 14:43
  • @conmen you should definitely not pass the email address in the url because CI limits the characters allowed in a url. https://stackoverflow.com/q/4170418/2943403 Using a POST request is definitely the better way to pass the email string from the view to the controller. – mickmackusa Oct 13 '21 at 04:20
  • Does this answer your question? [Codeigniter & jquery Ajax](https://stackoverflow.com/questions/42006000/codeigniter-jquery-ajax) – mickmackusa Oct 13 '21 at 04:31
  • ...and https://stackoverflow.com/q/23911438/2943403 and https://stackoverflow.com/q/27031489/2943403 and https://stackoverflow.com/q/46214475/2943403 – mickmackusa Oct 13 '21 at 04:33

1 Answers1

0

Ok, try this:

in your controller:

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";
    }

Then in your 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);  
            }
        }
    });

});

Lastly the change_password function should be like:

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

Update:

And you can use the session if you like:

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";
    }

then retrieve it in your view using:

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

so you don't have to pass the email in the string query.

mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
  • the page show error with `An Error Was Encountered The URI you submitted has disallowed characters.' i think is an alias '@' causes the error. – conmen Jan 30 '13 at 14:43
  • I want to thank you so much, your updates example is work perfect!! but when i in change_user_password view, the URL is display 'http://localhost/project/site/change_user_password/undefined` – conmen Jan 30 '13 at 14:49
  • @conmen - just delete the `+output_str.email` from your jquery :) – mamdouh alramadan Jan 30 '13 at 14:52
  • We should never see any querying from the Controller layer. We should also see Active Record query building techniques implemented instead of directly injecting user values into a sql query. If you are only checking if the email exists, then you don't need `*` or `num_rows()`; instead fetch the `COUNT()` directly in the sql. – mickmackusa Oct 11 '21 at 22:09
  • Under some conditional outcomes, `$output_str` is never printed in the controller. This answer is misleading if not incorrect. – mickmackusa Oct 11 '21 at 22:18
  • Oh man! This answer is from over 10 years ago! Whether it’s correct or not it’s irrelevant given using codeigniter as a framework is a bad practice to start with @mickmackusa – mamdouh alramadan Oct 13 '21 at 03:07
  • 1
    That is not a defense that I would ever use if I knew my answer was incorrect/misleading. Age of a post is completely irrelevant because researchers don't care when the question was asked nor when it was answered. Stack Overflow is only a useful resource to researchers if it contains helpful, correct advice; anything else is a waste of precious time. – mickmackusa Oct 13 '21 at 04:09
  • I'm humoring you for one last time. To start, SO is not for research, it's a Q&A - Look at how to ask a question under the FAQ. The answer worked for the OP hence it's accepted. Time is relevant, always, and I'm not here to teach you anything. Last but not least, to use PHP nevertheless Codeigniter at this time and age is simply putting you below research level. Just know by you caring here about a language or a framework that should never be used again outside of building small blogs and nothing else, doesn't help taking you seriously. @mickmackusa – mamdouh alramadan Nov 08 '21 at 16:15
  • The only thing remotely factual that you have said is "_The answer worked for the OP hence it's accepted._". Everything else is baseless opinion. – mickmackusa Nov 08 '21 at 19:57