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..............
Parsing unchecked checkboxes into array
Moderator: General Moderators
-
Brett James
- Forum Newbie
- Posts: 2
- Joined: Wed Jun 26, 2002 1:13 am
- Location: Brisbane, Australia
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You might want to consider changing this,
to this
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
Code: Select all
echo "<input type=checkbox name=Role_Checkboxї]> $Get_Roles_RowїRole_ID]<br>";Code: Select all
echo '<input type="checkbox" name="Role_Checkboxї'.$Role_ID.']" />'.$Get_Roles_Rowї'Role_ID'].'<br />';Mac
-
Brett James
- Forum Newbie
- Posts: 2
- Joined: Wed Jun 26, 2002 1:13 am
- Location: Brisbane, Australia
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?
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?
For example:
I mean, that is what you are looking for, isn't it?
Code: Select all
<input type="checkbox" name="elementї]" value="1" /><br>
<input type="checkbox" name="elementї]" value="2" /><br>
<input type="checkbox" name="elementї]" value="3" /><br>
<input type="checkbox" name="elementї]" value="4" /><br>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK