简体   繁体   English

如何在CodeIgniter中控制按钮单击上的过帐表单数据

[英]How to control posting form data on button click in CodeIgniter

Previously I posted an answer myself to this problem, believing that I've found a solution. 以前,我自己为这个问题发布了答案,并认为自己找到了解决方案。 However, just this morning, I found out that I was wrong. 但是,就在今天早上,我发现自己错了。 It's only partially solved. 只是部分解决了。 So, I've decided to remove that answer and edit this post altogether. 因此,我决定删除该答案并完全编辑此帖子。

In my CodeIgniter application, I have the following view page - meal_add.php , and there is a form there. 在我的CodeIgniter应用程序中,我具有以下视图页面meal_add.php ,并且那里有一个表单。 There's a submit button below that form. 该表格下方有一个提交按钮。 The form method is supposed to be "post" . 表单方法应该是"post" If I click on the submit button, data is saved in database. 如果单击提交按钮,数据将保存在数据库中。 The code is below: 代码如下:

<form action="javascript:checkTime();" method="post">
            <fieldset>
                <legend id="add_employee_legend">Add Meal Information</legend>
                <div>
                    <label id= "emp_id_add_label">Employee ID:</label>
                    <input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID" required="1"/>
                </div>
                <br>
                <div>
                    <label id= "is_guest_add_label">Guests?</label>
                    <input type="checkbox" name ="is_guest_checkbox" class ="guestcheck" checked="checked" value="1" onchange="valueChanged()"/>
                </div>
                <br>
                <div id = "guestnum">
                    <label id= "num_of_guest_add_label">No. of Guests:</label>
                    <input type="text" name="num_of_guest" id = "num_of_guest_add" placeholder='0'/>
                </div>
                <div>
                    <label id= "remarks_add_label">Remarks:</label>
                    <textarea rows="1" cols="20" style="margin-left: 35px"></textarea>
                </div>
                <input type="submit" name="submit" id = "meal_info_submit" value="Save Meal Information"/>
                <button id = "cancel_button" onclick="location.href='<?php echo base_url();?>index.php/admin_logins/meal'">Cancel</button>
            </fieldset>    
        </form>

Now, I want to do a certain condition checking on click of submit button. 现在,我想对“提交”按钮的单击进行某种条件检查。 Based on the checking, I'll allow the submission of data and redirection of the page. 基于检查,我将允许提交数据和页面重定向。 So my JavaScript function goes as follows: 所以我的JavaScript函数如下:

    <script>
    function checkTime()
    {
        var today = new Date();
        var hour = today.getHours();
        if(hour < '12')
        {
            location.href='<?php echo base_url();?>index.php/meals/insert_meal_db';
        }
        else
        {
            alert("You can't place an order after 12 pm.");
            location.href='<?php echo base_url();?>index.php/admin_logins/meal';
        }
    }
    </script>

This bit works fine - Whenever I try to submit data after 12 pm, an alert message says I can't place an order after 12 pm, halts my data submission and redirects to another page. 该位正常工作-每当我尝试在晚上12点之后提交数据时,都会收到一条警告消息,提示我无法在晚上12点之后下订单,暂停我的数据提交并重定向到另一个页面。 However, when it's BEFORE 12 pm, data is supposed to be submitted on click, ie form method should be post. 但是,如果在下午12点之前,则应在单击时提交数据,即应张贴表单方法。 But it seems data is not submitted on click, the form method "post" is not working at all. 但是似乎没有在点击时提交数据,表单方法"post"根本不起作用。 All I get is just plain 0s in my database. 我得到的只是我数据库中的纯0。

My controller insert method - 我的控制器插入方法-

public function insert_meal_db()
{
    date_default_timezone_set('Asia/Dacca');
    $mdata['emp_id'] = $this->input->post('emp_id');
    $mdata['is_guest'] = $this->input->post('is_guest');
    $mdata['num_of_guest'] = $this->input->post('num_of_guest');
    $mdata['remarks'] = $this->input->post('remarks');
    if($mdata['num_of_guest'] == 0)
    {
        $mdata['bill'] = 35.00;
    }

    else
    {
        $mdata['bill'] = 35.00 + (35.00 * $mdata['num_of_guest']);

    }
    $res = $this->meal_model->insert_meal($mdata);
    if($res)
    {   
        $this->session->set_flashdata('message','Meal information added successfully');
        header('location:'.base_url()."index.php/meals/".$this->index());

    }
}

Note that $mdata['bill'] is not coming from $this->input->post() , it's coming from a calculation. 注意, $mdata['bill']不是来自$this->input->post() ,而是来自计算。 So I get data for $mdata['bill']` in my database based on the calculation, instead of a 0. But for other columns, I get plain 0s. 因此,基于计算,我在数据库中获得了$ mdata ['bill']`的数据,而不是0。但是对于其他列,我得到的是纯0。

My model insert method- 我的模型插入方法-

  //To add a new meal to the database
  public function insert_meal($data)
  {
      return $this->db->insert('meal', $data);
  }   

My table name is meal , database is MySQL and here is the table structure: 我的表名称是meal ,数据库是MySQL,这是表结构:

       Column Name                       Data Type
          id                               int PK 
        emp_id                      varchar(15) NOT NULL
       is_guest                        int NOT NULL
      num_of_guest                         int 
        bill                           int NOT NULL
       remarks                             text

The funny thing is - although my emp_id column is a VARCHAR NOT NULL column, the data that is being saved there is 0. 有趣的是-尽管我的emp_id列是VARCHAR NOT NULL列,但是那里保存的数据为0。

In order to check validation before submission of form, you need to do these tasks: 为了在提交表单之前检查验证,您需要执行以下任务:

<form id="form" onSubmit="return checkSubmit();" action="#">

And javascript form validation using: 和javascript表单验证使用:

  function checkSubmit(){       
    if (validationCorrect()) {
         $('#form').submit();
    }
}  

Hope you got the concept. 希望你有这个概念。

Seems like you need to stop submitting the form if the time greater 如果时间较长,您似乎需要停止提交表单

<input type="submit" name="submit" id = "meal_info_submit" onclick="return checkTime()" value="Save Meal Information"/>

And the Javascript function would be something like Javascript函数将类似于

<script>
function checkTime()
{
    var today = new Date();
    var hour = today.getHours();
    if(hour > '12')
    {
        alert("You can't place an order after 12 pm.");
        location.href='<?php echo base_url();?>index.php/admin_logins/meal';
    } else {
       return true;
    }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM