Page 1 of 1

problems with checkboxes

Posted: Thu Nov 26, 2009 9:35 am
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?

Re: problems with checkboxes

Posted: Thu Nov 26, 2009 9:59 am
by pickle
Checkboxes are only POSTed or GETed if they are checked. If unchecked, they don't show up.

Re: problems with checkboxes

Posted: Thu Nov 26, 2009 10:02 am
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 )

Re: problems with checkboxes

Posted: Thu Nov 26, 2009 10:53 am
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....