I have a form here: http://kees.een-site-bouwen.nl/evenementform
as you can see I have a button with a +
to add a second date to the event. All is working when I fill in everything (so both date field) when I leave the optional one blank I'm getting an error because it's empty.
I tried something to use those optional fields only if they're filled in, but I could not figure out how.
I think I have to use a foreach loop.
My code to insert the fields into the database:
public function addevenement($image_data = array())
{
$data = array(
'titel' => $this->input->post('titel'),
'waar' => $this->input->post('waar'),
'entree' => $this->input->post('entree'),
'organisatie' => $this->input->post('organisatie'),
'omschrijving' => $this->input->post('omschrijving'),
'www' => $this->input->post('www'),
'tel' => $this->input->post('tel'),
'email' => $this->input->post('email'),
'afbeelding' => $image_data['file_name']
);
$this->db->insert('agenda', $data);
$id = $this->db->insert_id();
$data2 = array(
'idagenda' => $id,
'datum' => $this->input->post('datum'),
'van' => $this->input->post('van'),
'tot' => $this->input->post('tot')
);
$this->db->join('agenda', 'agendadatum.idagenda = agenda.idagenda');
$this->db->where('agendadatum.idagenda', $id);
$post = $this->input->post();
for ($i = 0; $i < count($post['datum']); $i++) {
$this->db->insert('agendadatum', array('idagenda' => $id, 'datum' => $post['datum'][$i], 'van' => $post['van'][$i], 'tot' => $post['tot'][$i]));
};
redirect('agenda');
}
How can I make the second date field optional so it skips them when they're empty?
Try something like this:
for ($i = 0; $i < count($post['datum']); $i++) {
if( $post['datum'][$i] != "" ){
$this->db->insert('agendadatum', array('idagenda' => $id, 'datum' => $post['datum'][$i], 'van' => $post['van'][$i], 'tot' => $post['tot'][$i]));
}
};
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.