2016-02-01 53 views
0

我目前正在做一個註冊表格,我想要的是一個模式,當用戶成功註冊到數據庫時會出現什麼,我怎麼能做到這一點? 這是我的看法如何在數據成功插入數據庫後彈出模態?

<!DOCTYPE html> 
    <html lang="en"> 
     <head> 
      <meta charset="utf-8"> 
      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <meta name="description" content=""> 
      <meta name="author" content=""> 
      <title>Register</title> 
      <?php echo link_tag('css/bootstrap.min.css'); ?> 
      <?php echo link_tag('jumbotron-narrow.css'); ?> 
      <?php echo link_tag('styles/menu.css'); ?> 
      <?php echo link_tag('styles/form.css'); ?> 

     </head> 








    <body class='bg'> 

    <div class='header'> 
     <img src = "<?php echo base_url() . '/images/tf_header.png' ?>"/> 
    </div> 



     <?php if (validation_errors()): ?> 
          <div class="alert alert-warning alert-dismissible" role="alert"> 
           <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
           <?php echo validation_errors(); ?> 
          </div> 

         <?php endif; ?> 
<div class ="container center-block"> 
    <br> 
    <div class ="panel panel-primary"> 
     <div class="panel-heading"> 
      <h3 class="panel-title">Registration</h3> 
     </div> 

     <div class="panel-body"> 
     <form class="form" action="<?php echo base_url() . 'user/addDelegate'; ?>" method="post" class="form-horizontal" role="form"> 

     <div class="form-group"> 
     <div class="row"> 
      <label class="col-sm-2 control-label">Surname:</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter Surname" name="surname" value="<?php echo set_value('surname'); ?>"> 
      </div> 
      </div> 
      </div> 

     <div class="form-group"> 
      <div class="row"> 
       <label class="col-sm-2 control-label">First Name:</label> 
       <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter First Name" name="firstname"value="<?php echo set_value('firstname'); ?>" > 
       </div> 
      </div> 
      </div> 




     <div class="form-group"> 
      <div class="row"> 
       <label class="col-sm-2 control-label">Course:</label> 
       <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter your Course" name="course" value="<?php echo set_value('course'); ?>"> 
       </div> 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="row"> 
       <label class="col-sm-2 control-label">Email:</label> 
       <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter email address" name="email" value="<?php echo set_value('email'); ?>"> 
       </div> 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-10"> 
       <button type="submit" class="btn btn-success">Register</button> 
       <a href="<?php echo base_url().'user/home_register';?>" type="button" class="btn btn-primary">Back</a> 
      </div> 
     </div> 


     </form> 
     </div> 
     </div> 
    </div> 

<div class="footer"> 
     <img src = "<?php echo base_url() . '/images/tf_footer.png' ?>"/> 
    </div> 

    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> 
    </body> 
    </html> 

控制器:

public function addDelegate(){ 
       $this->form_validation->set_rules('surname', 'surname', 'required'); 
       $this->form_validation->set_rules('firstname', 'firstname', 'required'); 
       $this->form_validation->set_rules('age', 'Age', 'required|is_numeric'); 
       $this->form_validation->set_rules('course', 'Course', 'required'); 
       $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
       if ($this->form_validation->run() == TRUE) { 

        $reg_dat = array(
         'surname' => $this->input->post('surname'), 
         'name' => $this->input->post('firstname'), 
         'age' => $this->input->post('age'), 
         'course' => ($this->input->post('course')), 
         'email' => ($this->input->post('email')), 

        ); 

        $this->load->view('user/home_view'); 
       $this->user_model->add_user($reg_dat); 


     $this->load->view('user/individual_register'); 
       } else { 
        $this->load->view('user/individual_register'); 
       } 

      } 
+0

這看起來像工作對AJAX的,因爲你需要證明登記過程的結果,在模式,無需重新加載頁面。你在尋找AJAX解決方案嗎? –

回答

4

試試這個代碼

模式

class User_Model extends CI_Model 
{ 

    public function __construct() 
    { 

     $this->load->database(); 
    } 


    public function add_user($data) 
    { 
     if($this->db->insert('tablename', $data)) 
     { 
      return TRUE; 
     }else 
     { 
      return FALSE; 
    } 
    } 
} 

控制器

public function addDelegate(){ 
    $this->form_validation->set_rules('surname', 'surname', 'required'); 
    $this->form_validation->set_rules('firstname', 'firstname', 'required'); 
    $this->form_validation->set_rules('age', 'Age', 'required|is_numeric'); 
    $this->form_validation->set_rules('course', 'Course', 'required'); 
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
    if ($this->form_validation->run() === FALSE) { 
     $this->load->view(''); 
    }else 
    { 
    $reg_dat = array(
     'surname' => $this->input->post('surname'), 
     'name' => $this->input->post('firstname'), 
     'age' => $this->input->post('age'), 
     'course' => ($this->input->post('course')), 
     'email' => ($this->input->post('email')), 
    ); 
    //call method from model 
    if($this->user_model->add_user($reg_dat) === TRUE) 
    { 
     $data['message'] = 'Insert success'; 
     //load your view page 
     $this->load->view('user/home_view',$data); 
    }else 
    { 
     $data['message'] = 'insert failled'; 
     // load your view page 
     $this->load->view('user/home_view',$data); 
    } 

    } else { 
    // load your register page 
    $this->load->view('user/individual_register'); 
} 

} 

視圖

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <meta name="description" content=""> 
     <meta name="author" content=""> 
     <title>Register</title> 
     <?php echo link_tag('css/bootstrap.min.css'); ?> 
     <?php echo link_tag('jumbotron-narrow.css'); ?> 
     <?php echo link_tag('styles/menu.css'); ?> 
     <?php echo link_tag('styles/form.css'); ?> 
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> 
    </head> 
<body class='bg'> 
<div class='header'> 
    <img src = "<?php echo base_url() . '/images/tf_header.png' ?>"/> 
</div> 
<?php if (validation_errors()): ?> 
    <div class="alert alert-warning alert-dismissible" role="alert"> 
    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
    <?php echo validation_errors(); ?> 
</div> 
<?php endif; ?> 
<div class ="container center-block"> 
<br> 
    <div class ="panel panel-primary"> 
    <div class="panel-heading"> 
     <h3 class="panel-title">Registration</h3> 
     </div> 
     <div class="panel-body"> 
     <?php echo form_open(base_url('user/addDelegate'),['name' => 'addform', 'id' => 'addform', 'class' => 'form-horizontal', 'role' => 'form']) ?> 
     <div class="form-group"> 
     <div class="row"> 
      <label class="col-sm-2 control-label">Surname:</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter Surname" name="surname" value="<?php echo set_value('surname'); ?>"> 
      </div> 
      </div> 
     </div> 

     <div class="form-group"> 
     <div class="row"> 
      <label class="col-sm-2 control-label">First Name:</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter First Name" name="firstname"value="<?php echo set_value('firstname'); ?>" > 
      </div> 
     </div> 
     </div> 

     <div class="form-group"> 
     <div class="row"> 
      <label class="col-sm-2 control-label">Course:</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter your Course" name="course" value="<?php echo set_value('course'); ?>"> 
      </div> 
     </div> 
     </div> 

     <div class="form-group"> 
     <div class="row"> 
      <label class="col-sm-2 control-label">Email:</label> 
      <div class="col-sm-5"> 
       <input type="text" class="form-control" placeholder="Enter email address" name="email" value="<?php echo set_value('email'); ?>"> 
      </div> 
     </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-sm-offset-2 col-sm-10"> 
      <button type="submit" class="btn btn-success">Register</button> 
      <a href="<?php echo base_url().'user/home_register';?>" type="button" class="btn btn-primary">Back</a> 
      </div> 
     </div> 
    </form> 
    </div> 
    </div> 
</div> 

    <div class="footer"> 
    <img src = "<?php echo base_url('images/tf_footer.png')?>"/> 
    </div> 
</body> 
</html> 
相關問題