Welcome Guest, Not a member yet? Register   Sign In
Datepicker and inserting to DB
#1

[eluser]Skoobi[/eluser]
Hi im a complete amateur at this but am learning slowly...
Ive been trying to create a online booking form and have come stuck when inserting the date from a jquery datepicker to the mysql database. Im not sure whats going on or how to format it.

Heres my code if anyone could shed some light on it for me. Many thanks

Controller
Code:
// Booking Form Template
    //********************//
    private function _bookings(){

        $this->data['recent_news'] = $this->article_m->get_recent();

        $this->load->library('form_validation');
        $this->load->helper('form');  
        $this->load->model('booking_m');  
        $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');      
        $this->form_validation->set_rules('phone', 'Phone Number', 'required|trim|xss_clean');      
        $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|valid_email');        
        $this->form_validation->set_rules('fromDate', 'From', 'required|trim|xss_clean');
        $this->form_validation->set_rules('toDate', 'To', 'required|trim|xss_clean');
        $this->form_validation->set_rules('extra', 'Extras','trim|xss_clean');
    
        if ($this->form_validation->run() == FALSE) // validation hasn't been passed
        {
            $this->data['recent_news'] = $this->article_m->get_recent();    
        }
        
        else // passed validation proceed to post success logic
        {      
            $this->booking_m->create_booking();
            $this->data['status'] = '<p><strong>Request Sent!!!</strong> We will be in touch shortly.</p>';    
        }
    } // End Booking Form


Model:
Code:
// Frontend Create Booking
public function create_booking()
{
  $data = array(
   'name' => $this->input->post('name'),
   'phone' => $this->input->post('phone'),
   'email' => $this->input->post('email'),
   'fromDate' => $this->input->post('fromDate'),
   'toDate' => $this->input->post('toDate'),
   'extra' => $this->input->post('extra'),
  );
  return $this->db->insert('booking', $data);
}

View:
Script in head:
Code:
[removed]
       $(function() {
               $('#datepicker').datepicker({dateFormat : 'ISO 8601 yy-mm-dd'});
       });
   [removed]

&lt;form view&gt;
Code:
&lt;?php // Change the css classes to suit your needs    

        $attributes = array('class' => 'remove-bottom', 'id' => 'contactForm');
        echo form_open('bookings', $attributes); ?&gt;

        
          <label for="name">Your Name</label> &lt;?php echo form_error('name'); ?&gt;
          &lt;input type="text" name="name" id="name" value="&lt;?php echo set_value('name'); ?&gt;" placeholder="Enter name"&gt;
          
          
          <label for="phone">Your Phone Number</label> &lt;?php echo form_error('phone'); ?&gt;
          &lt;input type="text" name="phone" id="phone" value="&lt;?php echo set_value('phone'); ?&gt;" placeholder="Enter Phone Number"&gt;
          
          
          <label for="email">Email Address</label> &lt;?php echo form_error('email'); ?&gt;
          &lt;input type="text" name="email" id="email" placeholder="Enter email" value="&lt;?php echo set_value('email'); ?&gt;"&gt;
          
          <label for="from">From</label>
          &lt;input type="text" id="fromDate" name="fromDate" placeholder="Choose From Date" value="&lt;?php echo set_value('fromDate'); ?&gt;"/&gt;

          <label for="to">to</label>
          &lt;input type="text" id="toDate" name="toDate" placeholder="Enter To Date" value="&lt;?php echo set_value('toDate'); ?&gt;" /&gt;

          <label for="extras">Extras</label>
          &lt;?php echo form_error('extras'); ?&gt;
          &lt;textarea  rows="5" id="extra" name="extra"&gt;&lt;?php echo set_value('extra'); ?&gt;&lt;/textarea&gt;

          <button class="btn btn-success" value="submit">Request Availability!</button>
        
        

      &lt;?php echo form_close(); ?&gt;
#2

[eluser]jonez[/eluser]
If you're getting an error on the insert (incorrect date format), format the date to match the field in your database. If it's a datetime field, do this:

Code:
'fromDate' => date( 'Y-m-d H:i:s', strtotime( $this->input->post('fromDate') ) ),

Or if it's just a date:

Code:
'fromDate' => date( 'Y-m-d', strtotime( $this->input->post('fromDate') ) ),

When you read the value back out it will be in the database format, so to turn it back into your field value use the same method and adapt the date parameters: http://php.net/manual/en/function.date.php
#3

[eluser]noideawhattotypehere[/eluser]
Code:
$( "#fromDate" ).datepicker({
                    dateFormat: "yy-mm-dd",
                    onClose: function(selectedDate) {
                        $("#toDate").datepicker("option", "minDate", selectedDate);
                    }
                });
                $( "#toDate" ).datepicker({
                    dateFormat: "yy-mm-dd",
                    onClose: function(selectedDate) {
                        $("#fromDate").datepicker("option", "maxDate", selectedDate);
                    }        
                });

Code:
echo form_open('controller_name_which_you_didnt_say_so_i_type_stupid_things/bookings', $attributes); ?&gt;

And your controller function which you submit to (bookings) cant be private, they are not accessible via url.
#4

[eluser]CroNiX[/eluser]
Code:
$('#datepicker').datepicker({dateFormat : 'ISO 8601 yy-mm-dd'});

It looks like you are using a 2 digit year in your datepicker. Mysql stores dates in yyyy-mm-dd format.
#5

[eluser]Skoobi[/eluser]
Many thanks i got it working eventually... with your suggestions.
The one thing im now stuck on is to make it to goto its own controller and then back with the status message. And also pass the data from the form to the database.


Heres my book.php controller which the form now points to

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Book extends CI_Controller {

public function index(){
  
    }


public function makeBooking(){
        $this->load->library('form_validation');
        $this->load->helper('form');  
        $this->load->model('booking_m');  
        $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');      
        $this->form_validation->set_rules('phone', 'Phone Number', 'required|trim|xss_clean');      
        $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|valid_email');
        $this->form_validation->set_rules('location', 'Location', 'required|trim|xss_clean');        
        $this->form_validation->set_rules('fromDate', 'From', 'required|trim|xss_clean');
        $this->form_validation->set_rules('toDate', 'To', 'required|trim|xss_clean');
        $this->form_validation->set_rules('extra', 'Extras','trim|xss_clean');

        $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
    
        $data = array(
            'name' => set_value('name'),
            'phone' => set_value('phone'),
            'email' => set_value('email'),
            'location' => set_value('location'),
            'formDate' => set_value('fromDate'),
            'toDate' => set_value('toDate'),
            'extra' => set_value('extra')
        );

        if ($this->form_validation->run() == FALSE) // validation hasn't been passed
        {
            $this->data['status'] = '<p><strong>Something went wrong!</p>';
            redirect('bookings');
        }
        
        else // passed validation proceed to post success logic
        {      
            $this->booking_m->create_booking($data);
            $this->data['status'] = '<p><strong>Request Sent!!!</strong> We will be in touch shortly.</p>';
            redirect('bookings');
        }
    }

}

heres the model booking_m.php
Code:
public function create_booking()
{
  $data = array(
   'name' => $this->input->post('name'),
   'phone' => $this->input->post('phone'),
   'email' => $this->input->post('email'),
   'location' => $this->input->post('location'),
   'fromDate' => $this->input->post('fromDate'),
   'toDate' => $this->input->post('toDate'),
   'extra' => $this->input->post('extra'),
  );
  return $this->db->insert('booking', $data);
}

and i want it to return to the page.php controller and to the method bookings

Code:
private function _bookings(){
        $this->load->library('form_validation');
        $this->load->helper('form');  
        $this->data['recent_news'] = $this->article_m->get_recent();
        
    }


This is probably a simple thing to do for you guys but hey im still learning!!! Im just going through the tuts tutorials now and thats helping a bit.




Theme © iAndrew 2016 - Forum software by © MyBB