I am having problems with a booking form in Codeigniter; when a visitor makes a booking, the details are supposed to be inserted into two tables in a database ('reservation', and 'period', there is an FK from 'reservation' to 'period').
When I try to make the booking, nothing appears in the database, and I can't tell if it a problem with the form, or between the controller and model (there are no errors thrown - just nothing in the db). I tried to follow the logic from this example .
here is my form (gite-booking-form.php):
<html>
<head>
<title>Gite booking form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('GiteCtl/giteBookingForm'); ?>
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA', 'giteB', 'giteC', 'giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'], $gitenames) ? $_POST['gitename'] : 'giteA';
echo '<select name="gitename">';
foreach ($gitenames as $option) {
echo '<option value="' . $option . '"' . (strcmp($option, $gitename) == 0 ? ' selected="selected"' : '') . '>' . $option . '</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" value="<?php echo set_value('start_date'); ?>"/>
<h5>End date</h5>
<input type="date" value="<?php echo set_value('end_date'); ?>"/>
<div><input type="submit" value="Submit"/></div>
</form>
</body>
</html>
my model (Gite.php):
<?php
class Gite extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function reservationInsert($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
}
and the controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class GiteCtl extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Gite');
$this->load->library('calendar');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('Visitors');
}
public function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
if (!isset($isLoggedIn) || $isLoggedIn != TRUE) {
echo 'In order to book a gite, you must first login';
echo 'button: visitor-login.php'; // I will add this later
} else {
$this->giteBookingForm();
}
}
public function giteBookingForm()
{
$this->form_validation->set_rules('gite_selection', 'gite_select', 'required');
$this->form_validation->set_rules('start_date', 'startDate', 'required', array('required' => 'You must choose a %s.'));
$this->form_validation->set_rules('end_date', 'endDate', 'required', array('required' => 'You must provide an %s.')
);
if ($this->form_validation->run() == FALSE) {
$this->load->view('gite-booking-form');
} else {
$start_date = $this->input->post('start_date');
$end_date = $this->input->post('end_date');
$isHauteSaison = 1;
$isWeekend = 1;
$period_data = array(
'start_date' => $start_date,
'end_date' => $end_date,
'isHauteSaison' => $isHauteSaison,
'isWeekend' => $isWeekend
);
$gite_selection = $this->input->post('gite_selection');
$visitor_email = $this->session['visitorName'];
$price = 100.00;
$period_id = $this->Gite->reservationInsert('period', $period_data);
$reservation_data = array(
'gite_selection' => $gite_selection,
'visitor_email' => $visitor_email,
'price' => $price,
'period' => $period_id
);
$insert = $this->Gite->reservationInsert('reservation', $reservation_data);
}
}
}
Thanks, E.
In the end it was the form; the php for retrieving data from the form was more complicated than I thought:
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA','giteB','giteC','giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'],$gitenames)?$_POST['gitename']:'giteA';
echo '<select name="gitename">';
foreach($gitenames as $option) {
echo '<option value="'.$option.'"'.(strcmp($option,$gitename)==0?' selected="selected"':'').'>'.$option.'</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" class="form-control" name="start_date" value="<?php echo isset($start_date) ? set_value('start_date', date('Y-m-d', strtotime($start_date))) : set_value('start_date'); ?>">
<h5>End date</h5>
<input type="date" class="form-control" name="end_date" value="<?php echo isset($end_date) ? set_value('end_date', date('Y-m-d', strtotime($end_date))) : set_value('end_date'); ?>">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.