Send data from controller to pop up modal using bo

2019-06-14 12:37发布

问题:

Hello I was going to make a forgot password function using modal bootstrap.. before I have made it without pop up modal.. I do know how to pass the data.. but now I don't know how to send data from controller to pop up modal..

Here is my code:

    <div id="forgot-password" class="modal hide fade in" style="display: none; ">
            <div class="modal-header" style="background-color:#f39c12; color:#fff;">
                <h3 id="helpModalLabel"> Forgot Password</h3>
            </div>
            <div class="modal-body">
                <form class="form-horizontal well" method="post" id="forgot_form" action="<?php echo base_url(); ?>forgotpassword/doforget">
                    <fieldset>
                        <legend></legend>
                            <div class="control-group">
                            <?php if( isset($info)): ?>

                                <div class="alert alert-block alert-success fade in" id="alert-message" >
                                    <img src="<?php echo base_url("assets")?>/image/Done.png" width="20px" height="20px">
                                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    <?php echo($info) ?>
                                </div>
                            <?php elseif( isset($error)): ?>
                                <div class="alert alert-block alert-error fade in" id="alert-message">
                                <img src="<?php echo base_url("assets")?>/img/icon/warning.png" width="20px" height="20px">
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                    <?php echo($error) ?>
                                </div>
                            <?php endif; ?>
                                <label for="Username" class="control-label span3" >Username</label>
                                <div class="controls">
                                    <input class="span3" type="text" id="Username" name="Username" placeholder="Input the Registered Username" name="Username"/>
                                    <span id="UsernamehelpText"></span>
                                </div>

                            </div>
                            <div class="form-actions">
                                <input type="submit" class="btn btn-primary" value="Send Password" name="submitbtn"/>


                            </div>

                    </fieldset>
                </form>
            </div>
            <div class="modal-footer">
              <a href="#" class="btn btn-primary round" data-dismiss="modal">Close</a>
            </div>
        </div>

Controller

public function forget()
{
    if (isset($_GET['info'])) {
           $data['info'] = $_GET['info'];
          }
    if (isset($_GET['error'])) {
          $data['error'] = $_GET['error'];
          }

    $page_content["page_title"] = "";
    $page_content["title"] = "Forgot Password";
    $page_content["icon_title"] = "home";

    $menu_params["sub_current_navigable"] = "";
    $page_content["navmenu"] = $this->load->view("nav_menu", "", true);
    $page_content["menu"] = $this->load->view("main_menu_login", $menu_params, true);
    if(isset($data)){
    $page_content["content"] = $this->load->view("forgotpassword",$data, true);
    }else{
    $page_content["content"] = $this->load->view("forgotpassword","", true);
    }
    $this->load->view("template/main_template", $page_content);
}


    public function doforget()
{
    $this->load->helper('url');
    $username= $_POST['Username'];
    $checkuser = $this->forgotpassword_model->getusername($username);
    print_r($checkuser);
    $getemail = $this->forgotpassword_model->getemail($username);
    if(!empty($checkuser))
    {
        $user = $checkuser['0'];
        $email = $getemail['0'];
        if(!empty($email['email']))
        {
            $this->sendpassword($user,$email);
            $info= "Password has been reset and has been sent to email id: ". $email['email'];
            echo $info;
            redirect('login/forget?info='.$info, 'refresh');
        }else{
            $error = "You don't have email, please contact your admin library ";
            echo $error;
            redirect('login/forget?error=' . $error, 'refresh');
         }
    }else
    {
        $error= "You have input wrong username ";
        redirect('login/forget?error=' . $error, 'refresh');
        echo $error;
    }

} 

Can someone help me to migrate this to pop up modal? do I need ajax? I also want to send the error message to pop up modal.. now I'm using 'login/forget?error=' . $error, 'refresh') Is it possible for me for still using this?

回答1:

EDIT

to pass your data from Controller to bootstrap modal you need json. to do that try this:

in your controller

 $this->output->set_content_type('application/json');
 $this->output->set_output(json_encode(  $your_data_array ));

this code change your array data to JSON and then send it to whatever you call this method. with your AJAX call this method and in success get this data parse it with JSON.pars() method and put them to your modal.


In CodeIgniter, before using a model you need to load it. For example you write a model in model folder with name userAuth. Creating a model is as follows:

class UserAuth extends CI_Model{
   //your methods
}

In your model you can have some method like forgetPass that will give you an array or data like:

public function forgetPass($dataArray){
 //your Code
}

OR

public function forgetPass( $data1, $data2, $data3){
//your code
}

When you want to use your model in a controller method you can load it handily in your controller method or load it in controller constructor like this:

$this->load->model('userAuth');

When you need a method like that, according to the example given you can pass your data to it as:

$this->userAuth->forgetPass($data);

addition:

In CodeIgniter, we have in-built methods to get POST / GET data. They are easy to use and safer. You can use:

$variable = $this->input->post('your_input_name');

OR

$variable = $this->input->get('your_key');

Instead of $_GET['your_key'] or $_POST['your_input_name']