将模式内的表单数据传递到表单外的控制器按钮,而不使用javascript

时间:2017-07-11 03:23:11

标签: javascript php forms codeigniter

我正在使用Codeigniter Framework,现在,我有一个带有表单的模态。现在在我的模态页脚中,我为它提交了提交表单。它已经超出了模态。现在为什么它不在我的控制器中提交?我在我的控制器中放了var_dump,以便在提交时回显,我通过没有javascript发送它。

这是我的形式:

<div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Category</h4>
      </div>
      <div class="modal-body">
            <div class="container-fluid">
            <form id="frm_category" class="form-horizontal" method="POST" action="Admin_controls/insertCategory">  
                <div class="form-group">
                    <label for="cat_name">Category: </label>
                    <input type="text" class="form-control" name="cat_name">
                </div>
                <div class="form-group">
                    <label for="cat_desc">Description: </label>
                    <input type="text" class="form-control" name="cat_desc">
                </div>
            </form>    
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-success" value="submit" form="frm_category">Add</button>
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>
    </div>

这是我的控制器:

public function insertCategory() {

        $data = array (
            'Category_name' => $this->input->post('cat_name'),
            'Category_desc' => $this->input->post('cat_desc')
        );
        var_dump($data);
    }

3 个答案:

答案 0 :(得分:1)

您应该将submit button放在</form>之前。另外,您需要javascript提交表单

您可以使用以下代码

    <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Add Category</h4>
          </div>
          <div class="modal-body">
                <div class="container-fluid">
                <form id="frm_category" class="form-horizontal" method="POST" action="Admin_controls/insertCategory">  
                    <div class="form-group">
                        <label for="cat_name">Category: </label>
                        <input type="text" class="form-control" name="cat_name">
                    </div>
                    <div class="form-group">
                        <label for="cat_desc">Description: </label>
                        <input type="text" class="form-control" name="cat_desc">
                    </div>
                </form>    
                </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-success" value="submit" onclick="myFunction()">Add</button>
            <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
          </div>
        </div>
<script>
function myFunction() {
    document.getElementById("frm_category").submit();
}
</script>

答案 1 :(得分:1)

  • 您应该更改要提交的按钮类型。所以它可以提交表格。
  • 提交按钮应位于表单标记
  • 我修改了您的模态代码。我希望它会起作用

                  ×       添加类别                                                    类别:                                                描述:                                                             加       关        

答案 2 :(得分:0)

如果要插入数据,则应执行以下步骤:

  1. POSTGET,您应该在<form>标记旁边添加该操作属性。
  2. 现在,对于post,您需要将控制器的地址提供给<form action = "<?= base_url('your_controller/your_function') ?>" method="post">
  3. 现在在控制器端:

    public function your_function(){
        // By using this you can get all posted data at once
        $your_post_data = $this->input->post();
        // By using this you get the post data seperatedly
        $your_post_data = $this->input->post('user_name'); // your form input name
    
       //Now for sending data to the model for database querys
    
        $this->load->model ('your_model');
        $data = $this->your_model->your_model_function($your_post_data);
    }
    
  4. 如果您不想每次都加载模型,那么您可以将模型添加到__construct

    public function __construct() {
        parent::__construct ();
    
        $this->load->model ('your_model');
        // So now on you just need to call the model name in your function.
    
    }
    
    public function your_function(){
      // By using this you can get all posted data at once
      $your_post_data = $this->input->post();
        // By using this you get the post data seperatedly
      $your_post_data = $this->input->post('user_name'); // your form input name
    
     $data = $this->your_model->your_model_function($your_post_data); 
    
    }
    
  5. 现在在模型中:(我正在为插入提供示例)

    public function your_model_function($your_post_data ){
    
        $this->db->insert_batch('your_table_name', $your_post_data );
    
        //If you want to understing of query execuation
        // for thie you need to enable the log from config.php form config folde.
        //Remove comment from log_message after emeble the log message
        //log_message('debug', 'Last Insert query: ' . print_r($this->db->last_query(), TRUE).' Inserted Data: '.print_r($your_post_data, TRUE));
    
        if($this->db->affected_rows() > 0){
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
  6. 有关查询的模式详细信息,请访问此CI documentation

    您的准则应该是:

        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Add Category</h4>
          </div>
          <form id="frm_category" class="form-horizontal" method="POST" action="<?= base_utl('Admin_controls/insertCategory') ?>"> 
              <div class="modal-body">
                    <div class="container-fluid">
    
                        <div class="form-group">
                            <label for="cat_name">Category: </label>
                            <input type="text" class="form-control" name="cat_name">
                        </div>
                        <div class="form-group">
                            <label for="cat_desc">Description: </label>
                            <input type="text" class="form-control" name="cat_desc">
                        </div>                  
                    </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-success" value="submit" form="frm_category">Add</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
              </div>
          </form>
        </div>
    

    你的控制器应该是:

    public function insertCategory() {
    
        // You should have to validate befor insert
    
        $this->load->model ('your_model');
        $your_post_data = $this->input->post();
        $insert_status = $this->your_model->your_model_function($your_post_data); 
    
        if($insert_status == TRUE){
            //return to your view or load the view
    
            $this->load->view ('your_view_directory');
        } else {
            // Send Error Message
    }
    

    验证请访问此CI documentation