Welcome Guest, Not a member yet? Register   Sign In
working with checkboxes
#1

[eluser]black.Blank[/eluser]
I have let say for example 5 checkboxes for group 1 and 4 checkboxes for group 2. Now, I have to check if the user has selected at least one option for each group. When the user clicks the submit button and no option has been selected for groups 1 and 2, then an error message will be displayed (i guess an alert message--javascript..)

Thanks in advance Smile
#2

[eluser]Petsoukos[/eluser]
You will create your form the usual way. Create the checkboxes and setup their name attributes as array. e.g. 'name' => 'group1[ ]', See the example bellow.
Code:
echo form_open('the_link_to_your_controller');

// Group One
$data = array(
'name' => 'group1[]',
'value' => 'something',
'checked' => false
);
echo form_checkbox($data);

$data = array(
'name' => 'group1[]',
'value' => 'something_else',
'checked' => false
);
echo form_checkbox($data);

// Group Two
$data = array(
'name' => 'group2[]',
'value' => 'something',
'checked' => false
);
echo form_checkbox($data);

$data = array(
'name' => 'group2[]',
'value' => 'something_else',
'checked' => false
);
echo form_checkbox($data);

echo form_submit('submit', 'Submit');

echo form_close();
Ideally you should create a loop to construct those checkboxes dynamically.

Then in your controller grab those posted values and make a simple IF statement:
Code:
function process() {
        
        $groupA = $this->input->post('group1');
        $groupB = $this->input->post('group2');
        
        if(!$groupA || !$groupB) {
            echo 'Error!';
        } else {
            // your process goes here
        }
    }
Ideally the process should be made in the model.

This will guarantee that your user checked at least 1 checkbox on each group.

This is how I would do it and I've tested it and it works. I suggest you wait for other responses also. Maybe there is another more efficient way to do the same thing.
#3

[eluser]black.Blank[/eluser]
thanks Smile
will try if it works Smile
#4

[eluser]black.Blank[/eluser]
is there any other way how can i enable/disable radio buttons and checkboxes besides this..

for example..

Code:
if(document.forms[0].elements["sample"].checked) {
document.forms[0].elements["option1"].enabled = true;
document.forms[0].elements["option2"].enabled = true;
}
#5

[eluser]Petsoukos[/eluser]
[quote author="black.Blank" date="1295814795"]is there any other way how can i enable/disable radio buttons and checkboxes besides this..

for example..

Code:
if(document.forms[0].elements["sample"].checked) {
document.forms[0].elements["option1"].enabled = true;
document.forms[0].elements["option2"].enabled = true;
}
[/quote]

Enable/Disable via? Buttons? Mass select checkboxes?

Sorry but I didn't get the question.




Theme © iAndrew 2016 - Forum software by © MyBB