![]() |
Properly handling Group of Checkboxes - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Properly handling Group of Checkboxes (/showthread.php?tid=51016) |
Properly handling Group of Checkboxes - El Forum - 04-17-2012 [eluser]Unknown[/eluser] I have a simple form that I would like to use checkboxes on for clarity and versatility but have wasted far too much time trying to do it the codeigniter way, which I still can not find a complete example of. To keep it even more simple, because if I can figure out handling one I can figure out the rest, here is a situation I seriously need to know the best way to handle using the infrastructure of codeigniter: Create,Update, Delete, display list of Venues. For now, I'll just go with Name, Phone, Phone_type. Very green at using codeigniter, but here is what I have (all fields varchar): CONTROLLER: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Tvenue extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->database(); $this->load->helper('form'); $this->load->helper('url'); $this->load->model('tvenue_model'); } function index() { $this->form_validation->set_rules('venue_name', 'Venue Name', 'required|trim|xss_clean|max_length[100]'); $this->form_validation->set_rules('phone_1', 'Phone 1', 'trim|is_numeric|max_length[10]'); $this->form_validation->set_rules('phone_1_type', 'Phone 1 Type', 'trim'); $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); if ($this->form_validation->run() == FALSE) { $this->load->view('tvenue_view'); } else { $form_data = array( 'venue_name' => set_value('venue_name'), 'phone_1' => set_value('phone_1'), 'phone_1_type' => set_value('phone_1_type'), ); if ($this->tvenue_model->SaveForm($form_data) == TRUE) { redirect('tvenue/success'); } else { echo 'An error occurred saving your information. Please try again later'; } } function success() { echo 'Success'; } } } ?> VIEW <?php echo form_open('tvenue'); ?> <p> <label for="venue_name">Venue Name <span class="required">*</span></label> <?php echo form_error('venue_name'); ?> <br /> <input id="venue_name" type="text" name="venue_name" maxlength="100" value="<?php echo set_value('venue_name'); ?>" /> </p> <p> <label for="phone_1">Phone 1</label> <?php echo form_error('phone_1'); ?> <br /> <input id="phone_1" type="text" name="phone_1" maxlength="100" value="<?php echo set_value('phone_1'); ?>" /> </p> <p> <?php echo form_error('phone_1_type'); ?> <br /> <input type="checkbox" id="phone_1_type" name="phone_1_type" value="LIPS" class="" <?php echo set_checkbox('phone_1_type', 'LIPS'); ?> /> <label for="phone_1_type">LIPS</label> </p> <p> <?php echo form_submit( 'submit', 'Submit'); ?> </p> <?php echo form_close(); ?> MODEL <?php class Tvenue_model extends CI_Model { function __construct() { parent::__construct(); } function SaveForm($form_data) { $this->db->insert('tvenue', $form_data); if ($this->db->affected_rows() == '1') { return TRUE; } return FALSE; } } ?> I would soooo appreciate it if someone could show me how to properly make the phone type a group of 3 check boxes instead of the one: <input type="checkbox" id="phone_1_type" name="phone_1_type" value="LIPS" class="" <?php echo set_checkbox('phone_1_type', 'LIPS'); ?> /> <label for="phone_1_type">LIPS</label> <input type="checkbox" id="phone_1_type" name="phone_1_type" value="Bottle" class="" <?php echo set_checkbox('phone_1_type', 'Bottle'); ?> /> <label for="phone_1_type">Bottle</label> <input type="checkbox" id="phone_1_type" name="phone_1_type" value="Dinner" class="" <?php echo set_checkbox('phone_1_type', 'Dinner'); ?> /> <label for="phone_1_type">Dinner</label> I look forward to your guidance. Thanks - Randy |