Parsing unchecked checkboxes into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brett James
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2002 1:13 am
Location: Brisbane, Australia

Parsing unchecked checkboxes into array

Post by Brett James »

Something that i have been working on for a while:

I have a number of checkboxes that I want the results recorded into an array. The problem is that I want the unchecked results to be recorded as well. What I have at the moment is the following:
It only reads in checked boxes:

Code to display & read in checkboxes:

WHILE ($Get_Roles_Row = mssql_fetch_array($Get_Roles_Result)) {

echo "<input type=checkbox name=Role_Checkbox[]> $Get_Roles_Row[Role_ID]<br>";
}


Code to insert results into database:
for ($i=0;$i < count($Role_Checkbox); $i++)
{
//Insert into database statement here;
echo "$Role_Checkbox[$i], $i, <br>,"; // testing to confirm insert

}

What I am trying to do is create a page for server operators to describe what roles their server serves.
ie
role table is (where the information is gathered from:
role_id role
1 mail server
2 file server
etc....

Then I want to insert these results into a Server_Roles table, which takes the form of:

server_id role_id
1 1
1 2
2 5
2 8
etc.....

Any ideas? Should I be doing this another way?

Thanks for any help..............
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You might want to consider changing this,

Code: Select all

echo "<input type=checkbox name=Role_Checkbox&#1111;]> $Get_Roles_Row&#1111;Role_ID]<br>";
to this

Code: Select all

echo '<input type="checkbox" name="Role_Checkbox&#1111;'.$Role_ID.']" />'.$Get_Roles_Row&#1111;'Role_ID'].'<br />';
that way you can check the $_POST['Role_Checkbox'] array for the role ID's and if they aren't there then they weren't chosen.

Mac
Brett James
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2002 1:13 am
Location: Brisbane, Australia

Post by Brett James »

Thanks for that, but still no joy.
With this, it is still only placing the "on" checkboxes to the beginning of the array.

I used the above code you suggested, and used the following code to test the results:

WHILE ($Get_Roles_Row = mssql_fetch_array($Get_Roles_Result)){

if ($Role_Checkbox[$Get_Roles_Row[Role_ID]])
{echo "this is on: $Get_Roles_Row[Role_ID]<br>";
}

else
{

echo "this is off: $Get_Roles_Row[Role_ID]<br>";
}

Out of the 8 checkboxes (at the moment - this is variable), I ticked the middle 4. This was the result:

this is on: 1
this is on: 2
this is on: 3
this is on: 4
this is off: 5
this is off: 6
this is off: 7
this is off: 8


Is there another way to do this?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Why not just set the value of the checkbox to the ID of the thing they are choosing, as opposed to setting the name of the thing they are choosing?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

For example:

Code: Select all

<input type="checkbox" name="element&#1111;]" value="1" /><br>
<input type="checkbox" name="element&#1111;]" value="2" /><br>
<input type="checkbox" name="element&#1111;]" value="3" /><br>
<input type="checkbox" name="element&#1111;]" value="4" /><br>
I mean, that is what you are looking for, isn't it?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

And that's what I meant to say and obviously completely messed up. Doh...

Mac
Post Reply