Welcome Guest, Not a member yet? Register   Sign In
Properly handling Group of Checkboxes
#1

[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';
}
}
}
?&gt;

VIEW

&lt;?php

echo form_open('tvenue'); ?&gt;

<p>
<label for="venue_name">Venue Name <span class="required">*</span></label>
&lt;?php echo form_error('venue_name'); ?&gt;
<br />
&lt;input id="venue_name" type="text" name="venue_name" maxlength="100" value="&lt;?php echo set_value('venue_name'); ?&gt;" /&gt;
</p>

<p>
<label for="phone_1">Phone 1</label>
&lt;?php echo form_error('phone_1'); ?&gt;
<br />
&lt;input id="phone_1" type="text" name="phone_1" maxlength="100" value="&lt;?php echo set_value('phone_1'); ?&gt;" /&gt;
</p>

<p>
&lt;?php echo form_error('phone_1_type'); ?&gt;
<br />
&lt;input type="checkbox" id="phone_1_type" name="phone_1_type" value="LIPS" class="" &lt;?php echo set_checkbox('phone_1_type', 'LIPS'); ?&gt; /&gt;
<label for="phone_1_type">LIPS</label>
</p>

<p>
&lt;?php echo form_submit( 'submit', 'Submit'); ?&gt;
</p>

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

MODEL
&lt;?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;
}
}
?&gt;

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:


&lt;input type="checkbox" id="phone_1_type" name="phone_1_type" value="LIPS" class="" &lt;?php echo set_checkbox('phone_1_type', 'LIPS'); ?&gt; /&gt;
<label for="phone_1_type">LIPS</label>


&lt;input type="checkbox" id="phone_1_type" name="phone_1_type" value="Bottle" class="" &lt;?php echo set_checkbox('phone_1_type', 'Bottle'); ?&gt; /&gt;
<label for="phone_1_type">Bottle</label>


&lt;input type="checkbox" id="phone_1_type" name="phone_1_type" value="Dinner" class="" &lt;?php echo set_checkbox('phone_1_type', 'Dinner'); ?&gt; /&gt;
<label for="phone_1_type">Dinner</label>


I look forward to your guidance.

Thanks - Randy




Theme © iAndrew 2016 - Forum software by © MyBB