-1

My framework is codeigniter. I developed a form to store user inputs in the database. One input is there to fill the date and time automatically inside input box. But when I try to save it in the database, date does not save. Other values in the form saves in the database This is codeigniter view page

<?php $attributes = array("name" => "addclients");echo form_open("home/ExpenseSave", $attributes);?>       
    <div class="row">
           <body onload="myFunction()">
           <input type="text" id="demo" class="form-control"/>
     </div>
     <script>
function myFunction() {

$(document).ready(function(){
  $('#demo').attr("placeholder", Date());
});

}
</script>

This is codeigniter controller page which saves data in database

 $data = array(
                'demo'                        => $this->input->post('demo'),
                'pt'                        => $this->input->post('pt'),
                'pn'                        => $this->input->post('pn'),
                'task'                        => $this->input->post('task'),
                'employee'                        => $this->input->post('employee'),
                'projectname' => $this->input->post('projectname'),
                'ExpenseName' => $this->input->post('ExpenseName'),
                'ExpenseAmount' => $this->input->post('ExpenseAmount'),

            );

            if ($this->db->insert('expenses', $data))

In the database , data type for "demo" is datetime. Database is mysql

Dushee
  • 255
  • 5
  • 17

1 Answers1

1

On your view:

<?php $attributes = array("name" => "addclients");echo form_open("home/ExpenseSave", $attributes);?>       
    <div class="row">
           <body onload="myFunction()">
           <input type="text" name="current_date" class="form-control" value="<?php echo date('Y-m-d H:i:s') ?>" />
           <input type="text" id="demo" class="form-control"/>
     </div>
     <script>
function myFunction() {

$(document).ready(function(){
  $('#demo').attr("placeholder", Date());
});

}
</script>

And in your model:

$data = array(
                'demo'                        => $this->input->post('demo'),
                'pt'                        => $this->input->post('pt'),
                'pn'                        => $this->input->post('pn'),
                'task'                        => $this->input->post('task'),
                'employee'                        => $this->input->post('employee'),
                'projectname' => $this->input->post('projectname'),
                'ExpenseName' => $this->input->post('ExpenseName'),
                'ExpenseAmount' => $this->input->post('ExpenseAmount'),
                'columnDate' => $this->input->post('current_date')

            );

            if ($this->db->insert('expenses', $data))

I haven't see any of your input date. So I add to your code. Please try.

Nere
  • 4,097
  • 5
  • 31
  • 71
  • Thank you Abu..now value saves in the database. But how can I change the time zone in to my country? – Dushee Aug 09 '17 at 03:23
  • See https://stackoverflow.com/questions/3792066/convert-utc-dates-to-local-time-in-php – Nere Aug 09 '17 at 03:24