problems with checkboxes

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

problems with checkboxes

Post by IGGt »

Hi Guys,

I'm having an issue with some checkboxes, which don't seem to be passing the state across to the new file.

the script I have thus far is

file1.html

Code: Select all

 
<form action="file2.php" method="post">
Slave 1 <INPUT TYPE="checkbox" NAME="slave[]"><br/>
Slave 2 <INPUT TYPE="checkbox" NAME="slave[]"><br/>
Slave 3 <INPUT TYPE="checkbox" NAME="slave[]"><br/>
Slave 4 <INPUT TYPE="checkbox" NAME="slave[]"><br/>
<br/>
<input type="submit" />
</form>
 
file2.php

Code: Select all

 
<?php
 
If (isset($_POST['Slave[1]']))
{
echo "Slave 1" ;
}
ELSE
{
echo "not selected (1)";
}
//////////////////////////////////
If (isset($_POST['Slave[2]']))
{
echo "Slave 2" ;
}
ELSE
{
echo "not selected (2)";
}
/////////////////////////////////
If (isset($_POST['Slave[3]']))
{
echo "Slave 3" ;
}
ELSE
{
echo "not selected (3)";
}
////////////////////////////////
If (isset($_POST['Slave[4]']))
{
echo "Slave 4" ;
}
ELSE
{
echo "not selected (4)";
}
?>
 

But all I get when I tick the boxes (some or all or none) and click Submit is:

SLAVE 1

not selected (1)not selected (2)not selected (3)not selected (4)


Can anybody see what I am missing please?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: problems with checkboxes

Post by pickle »

Checkboxes are only POSTed or GETed if they are checked. If unchecked, they don't show up.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: problems with checkboxes

Post by iankent »

Your $_POST bits are wrong. The [1], [2] etc don't become part of the $_POST variable name, the $_POST variable is itself an array.

I.e.:

Code: Select all

 <input type="text" name="test[]" value="First" />
<input type="text" name="test[]" value="Second" />
<input type="text" name="test[]" value="Third" />
<input type="text" name="test[]" value="Fourth" /> 
would return values in this structure:

Code: Select all

 $_POST['test'][0] = 'First';
$_POST['test'][1] = 'Second';
$_POST['test'][2] = 'Third';
$_POST['test'][3] = 'Fourth'; 
and not:

Code: Select all

 $_POST['test[0]'] = 'First';
$_POST['test[1]'] = 'Second';
$_POST['test[2]'] = 'Third';
$_POST['test[3]'] = 'Fourth';
edit: i'm starting to doubt myself lol, but try it and see what happens :banghead:

edit: I was right, this code:

Code: Select all

<?php
if(isset($_POST['test'])) {
    print_r($_POST['test']);
}  
?>
<form action="?" method="post">
<input type="hidden" name="test[]" value="first" />
<input type="hidden" name="test[]" value="second" />
<input type="hidden" name="test[]" value="third" />
<input type="hidden" name="test[]" value="fourth" />
<input type="submit" />
</form>
outputs this:

Code: Select all

Array ( [0] => first [1] => second [2] => third [3] => fourth )
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: problems with checkboxes

Post by IGGt »

I have figured it out:

file1.html

Code: Select all

 
<form action="MySQLSlaveStatus3.php" method="post">
                Query: <input type="text" name="query" Size="100"/><br/>
                <br/>
                Slave 1 <INPUT TYPE="checkbox" NAME="slave1" VALUE="Slave1" CHECKED><br/>
                Slave 2 <INPUT TYPE="checkbox" NAME="slave2" VALUE="Slave2" CHECKED><br/>
                Slave 3 <INPUT TYPE="checkbox" NAME="slave3" VALUE="Slave3" CHECKED><br/>
                Slave 4 <INPUT TYPE="checkbox" NAME="slave4" VALUE="Slave4" CHECKED><br/>
                <br/>
                <input type="submit" />
            </form>
 
file2.php

Code: Select all

 
 
if (isset($_POST['slave1'])) {
    $slave1 = $_POST['slave1'];
 
    echo $slave1, "\n";
 
repeat....
Post Reply