Page 1 of 1

Parsing unchecked checkboxes into array

Posted: Wed Jun 26, 2002 1:13 am
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..............

Posted: Wed Jun 26, 2002 2:06 am
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

Posted: Wed Jun 26, 2002 7:44 pm
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?

Posted: Wed Jun 26, 2002 8:21 pm
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?

Posted: Wed Jun 26, 2002 8:22 pm
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?

Posted: Thu Jun 27, 2002 1:51 am
by twigletmac
And that's what I meant to say and obviously completely messed up. Doh...

Mac