javascript - How to control posting form data on button click in CodeIgniter -
javascript - How to control posting form data on button click in CodeIgniter -
previously posted reply myself problem, believing i've found solution. however, morning, found out wrong. it's partially solved. so, i've decided remove reply , edit post altogether.
in codeigniter application, have next view page - meal_add.php
, , there form there. there's submit button below form. form method supposed "post"
. if click on submit button, info saved in database. code 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, want status checking on click of submit button. based on checking, i'll allow submission of info , redirection of page. javascript function goes follows:
<script> function checktime() { var today = new date(); var hr = today.gethours(); if(hour < '12') { location.href='<?php echo base_url();?>index.php/meals/insert_meal_db'; } else { alert("you can't place order after 12 pm."); location.href='<?php echo base_url();?>index.php/admin_logins/meal'; } } </script>
this bit works fine - whenever seek submit info after 12 pm, alert message says can't place order after 12 pm, halts info submission , redirects page. however, when it's before 12 pm, info supposed submitted on click, i.e. form method should post. seems info not submitted on click, form method "post"
not working @ all. plain 0s in database.
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 info added successfully'); header('location:'.base_url()."index.php/meals/".$this->index()); } }
note $mdata['bill']
not coming $this->input->post()
, it's coming calculation. info $mdata['bill']` in database based on calculation, instead of 0. other columns, plain 0s.
my model insert method-
//to add together new meal database public function insert_meal($data) { homecoming $this->db->insert('meal', $data); }
my table name meal
, database mysql , here table structure:
column name info 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 - although emp_id
column varchar not null
column, info beingness saved there 0.
in order check validation before submission of form, need these tasks:
<form id="form" onsubmit="return checksubmit();" action="#">
and javascript form validation using:
function checksubmit(){ if (validationcorrect()) { $('#form').submit(); } }
hope got concept.
javascript php html forms codeigniter
Comments
Post a Comment