Welcome Guest, Not a member yet? Register   Sign In
How to save multiple radio boxes ???
#1

[eluser]Bainzy[/eluser]
Hey everyone,

Being doing some work on Dove Forum and im currently adding in a ACL system based on a PHP5 ACL system that was posted on nettuts a while ago.

Anyway I have converted it for use in a CI library file and changed some functions to suit my needs.

I am working on the admin panel side of it and currently working on the roles page. Basically the ACL system provides me with all the permissions for a role based on the $roleID. I have a form with lots of radio buttons on it here is the HTML dump :

Code:
<table border="0" cellpadding="0" cellspacing="0" class="table">
<thead>
<tr>
<th></th><th>Allow</th><th>Deny</th><th>Ignore</th></tr>

</thead>
<tbody>
<tr>
<td class="alt">Access Admin System</td><td class="alt">&lt;input type="radio" name="perm_2" value="1" checked="checked" id="perm_2_1"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_2" value="0" id="perm_2_0"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_2" value="x" id="perm_2_x"  /&gt;&lt;/td></tr>
<tr>
<td>Access Premium Content</td><td>&lt;input type="radio" name="perm_7" value="1" checked="checked" id="perm_7_1"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_7" value="0" id="perm_7_0"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_7" value="x" id="perm_7_x"  /&gt;&lt;/td></tr>
<tr>
<td class="alt">Access Site</td><td class="alt">&lt;input type="radio" name="perm_1" value="1" checked="checked" id="perm_1_1"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_1" value="0" id="perm_1_0"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_1" value="x" id="perm_1_x"  /&gt;&lt;/td></tr>
<tr>
<td>Install Modules</td><td>&lt;input type="radio" name="perm_5" value="1" checked="checked" id="perm_5_1"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_5" value="0" id="perm_5_0"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_5" value="x" id="perm_5_x"  /&gt;&lt;/td></tr>
<tr>
<td class="alt">Limited Admin</td><td class="alt">&lt;input type="radio" name="perm_8" value="1" checked="checked" id="perm_8_1"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_8" value="0" id="perm_8_0"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_8" value="x" id="perm_8_x"  /&gt;&lt;/td></tr>

<tr>
<td>Post Comments</td><td>&lt;input type="radio" name="perm_6" value="1" checked="checked" id="perm_6_1"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_6" value="0" id="perm_6_0"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_6" value="x" id="perm_6_x"  /&gt;&lt;/td></tr>
<tr>
<td class="alt">Publish Articles</td><td class="alt">&lt;input type="radio" name="perm_3" value="1" checked="checked" id="perm_3_1"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_3" value="0" id="perm_3_0"  /&gt;&lt;/td><td class="alt">&lt;input type="radio" name="perm_3" value="x" id="perm_3_x"  /&gt;&lt;/td></tr>
<tr>
<td>Publish Events</td><td>&lt;input type="radio" name="perm_4" value="1" checked="checked" id="perm_4_1"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_4" value="0" id="perm_4_0"  /&gt;&lt;/td><td>&lt;input type="radio" name="perm_4" value="x" id="perm_4_x"  /&gt;&lt;/td></tr>
</tbody>
</table>

Now on to my question.

How when i submit this form can i get the value of each radio input, check its value and then update my 'permissions' table where changes have being made.

I am really stuck with this I just cant seem to think of a way to build it into my 'updateRole' function.

Chris
#2

[eluser]seeraw[/eluser]
Hi,
For this you have to give name for each radio box as array see below,

&lt;input type="radio" name="radio_name[]" /&gt;
&lt;input type="radio" name="radio_name[]" /&gt;
&lt;input type="radio" name="radio_name[]" /&gt;
&lt;input type="radio" name="radio_name[]" /&gt;
&lt;input type="radio" name="radio_name[]" /&gt;

Like this way so that when you post your form you will get an array which contains values of the check boxes you checked,


Thanks,
Swapnil
#3

[eluser]Derek Allard[/eluser]
Actually, seeraw's suggestion probably won't work with radio buttons, since they only allow one possible answer (would work with checkbox though).

All your radio buttons in your example have different names, so literally you're just going to have to grab each one of them separately:

Code:
$pref1 = $this->input->post('pref1');
$pref2 = $this->input->post('pref2');
$pref3 = $this->input->post('pref3');
...
$pref8 = $this->input->post('pref8');
#4

[eluser]Bainzy[/eluser]
I have managed to come up with a solution that works ( or seems to ) I will put up the code for others :

Code:
foreach( $_POST as $k => $v)
{
     if(substr($k, 0, 5) == "perm_")
     {
    $permID = str_replace("perm_", "", $k);    
    if($v == 'x')
    {
        $data = array(
        'roleID' => $roleID,
        'permID' => $permID,
        );
                            
        $this->db->delete('role_perms', $data);
        continue;
    }
                        
    $data = array(
        'roleID' => $roleID,
        'permID' => $permID,
        'value' => $v,
        'addDate' => date("Y-m-d H:i:s"),
    );
                        
    $this->db->set($data);
    $this->db->replace('role_perms');
     }
}
#5

[eluser]seeraw[/eluser]
I am extremely sorry Derek Allard
Yes it wont work for radio buttons It is valid only for checkboxes.
I will look into this issue and get back to you soon.

Thanks,
Swapnil




Theme © iAndrew 2016 - Forum software by © MyBB