Jump to content

How to 'remember' what's in checkbox?


CasperCaro

Recommended Posts

I am wondering how to keep a checkbox checked?

 

I have the folllowing code and it is outputting the correct info, but when i tick checkbox, and then click box "SAVE Days and Recipes Chosen" the checkbox doesn't get set.

 

<?php

for($i = 0; $i < $rows; $i++) {

?>

<tr>

<?php

    for($j = 0; $j < $columns; $j++) {

            echo "<TD><font color='8678E9'>" . $data[$i + ($j * $rows)] . "</font></TD>\n"; ?>

            <td><input type='checkbox' name=$R1 <?php  if($R1=="checked"){$R1=="checked"; echo $R1;}

                                                        elseif($R1=="unchecked"){$R1=="unchecked"; echo $R1;} ?>                                                                                               

            <td><?php echo"<TD><font color='3923D6'><font size=4>" . $data2[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data4[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data5[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

            <td align="centre"><input type="checkbox" name="ch1[]" method="POST" value=<?php echo $data3[$i + ($j * $rows)]?>,1 >M<br>         

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,2 method="POST">Tu<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,3 method="POST">W<br>

                <input type="checkbox" name="ch4[]" <?php if (!isset($ch4)){$Value='unchecked'; echo $Value;} elseif(isset($ch4)){$Value='checked'; echo $Value;}?> value=<?php echo $data3[$i + ($j * $rows)]?>,4 method="POST">Th<br></td>

            <td align="centre"><input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,5 method="POST">F<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,6 method="POST">Sa<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,7 method="POST">Su<br></td>     

                <?php echo"<TD><font color='3923D6'><font size=4>" . $data6[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

            <td style='font-weight: bold; text-align: left;'><font color='8678E9'>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</font></td>

                <?php

    }

    echo "</TR>\n";

}

echo "</TABLE>\n";

echo "<br>";

                     

 

It is the following part that i cuurently  need help with

 

            <td><input type='checkbox' name=$R1 <?php  if($R1=="checked"){$R1=="checked"; echo $R1;}

                                                        elseif($R1=="unchecked"){$R1=="unchecked"; echo $R1;} ?>         

 

I haven't looked at the other checkbox part of

 

            <td align="centre"><input type="checkbox" name="ch1[]" method="POST" value=<?php echo $data3[$i + ($j * $rows)]?>,1 >M<br>         

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,2 method="POST">Tu<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,3 method="POST">W<br>

                <input type="checkbox" name="ch4[]" <?php if (!isset($ch4)){$Value='unchecked'; echo $Value;} elseif(isset($ch4)){$Value='checked'; echo $Value;}?> value=<?php echo $data3[$i + ($j * $rows)]?>,4 method="POST">Th<br></td>

            <td align="centre"><input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,5 method="POST">F<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,6 method="POST">Sa<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,7 method="POST">Su<br></td>     

 

 

Appreciate any suggestions

 

 

Link to comment
https://forums.phpfreaks.com/topic/227952-how-to-remember-whats-in-checkbox/
Share on other sites

I have been mucking around for ages with this and still cant work out what im doing wrong! Please if anyone can see whats wrong, or may have suggestion id really appreciate it.

 

I changed code slightly to

 

<?php

for($i = 0; $i < $rows; $i++) {

?>

<tr>

<?php

    for($j = 0; $j < $columns; $j++) {

    //$R1[$i]='checked';

            echo "<TD><font color='8678E9'>" . $data[$i + ($j * $rows)] . "</font></TD>\n"; ?>

            <td><input type='checkbox' name="R1[]" method="POST" <?php  //if(isset($R1)){$R1='checked'; echo $R1;}

                                                        //elseif(!isset($R1)){$R1="unchecked"; echo $R1;}   

                                                        ////if($R1[$i]=="checked"){$R1[$i]=="checked"; echo $R1[$i];}

                                                        ////elseif($R1[$i]=="unchecked"){$R1[$i]="unchecked"; echo $R1[$i];}?>  <?php

                                                        //if(isset($R1[$i])){$R1[$i]="checked"; echo $R1[$i];}

                                                        //elseif(!isset($R1[$t])){$R1[$t]="unchecked"; echo $R1[$t];} $t++;?>  <?php

                                                       

                                                        if (!isset($R1)){$Value='unchecked'; echo $Value;}

                                                      elseif(isset($R1)){$Value='checked'; echo $Value;}?> method="POST"><br></td>

                                                                                                                                                 

            <td><?php echo"<TD><font color='3923D6'><font size=4>" . $data2[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data4[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data5[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

            <td align="centre"><input type="checkbox" name="ch1[]" method="POST" value=<?php echo $data3[$i + ($j * $rows)]?>,1 >M<br>         

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,2 method="POST">Tu<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,3 method="POST">W<br>

               

                <input type="checkbox" name="ch4[]" <?php if (!isset($ch4)){$Value='unchecked'; echo $Value;}

                                                      elseif(isset($ch4)){$Value='checked'; echo $Value;}?> value=<?php echo $data3[$i + ($j * $rows)]?>,4 method="POST">Th<br></td>

                                                     

            <td align="centre"><input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,5 method="POST">F<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,6 method="POST">Sa<br>

                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,7 method="POST">Su<br></td>     

                <?php echo"<TD><font color='3923D6'><font size=4>" . $data6[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>

            <td style='font-weight: bold; text-align: left;'><font color='8678E9'>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</font></td>

                <?php

    }

    echo "</TR>\n";

}

echo "</TABLE>\n";

echo "<br>";

 

if(isset($R1)){

echo "R1 chkbox set - havent gone into submitok check";

echo "<br>";

 

echo $R1;

echo "<br>";

//echo $value;

//echo "<br>";

 

echo $size=count($R1);

echo "<br>";

 

    $i = 0;

while ($i < $size) {

echo $R1[0];

echo "<br>";

++$i;

}

}

 

 

echo "<br>";

echo "<br>";

echo "<br>";

echo "<br>";

 

 

 

if (isset($ch4)){

echo "CHECKBOX SET";

echo "split the RN and DC - havent gone into submitok check";

echo "<br>";

 

echo $ch4;

echo "<br>";

 

echo $size=count($ch4);

echo "<br>";

 

    $i = 0;

while ($i < $size) {

echo $ch4[$i];

echo "<br>";

++$i;

}

//$ch4==checked;

}

echo "<br>"

 

and the output is

 

array(3) {

  ["R1"]=>

  array(1) {

    [0]=>

    string(2) "on"

  }

  ["ch4"]=>

  array(1) {

    [0]=>

    string(4) "56,4"

  }

  ["submitok"]=>

  string(28) "SAVE Days and Recipes Chosen"

}

 

 

 

R1 chkbox set - havent gone into submitok check

Array

1

on

 

 

 

 

CHECKBOX SETsplit the RN and DC - havent gone into submitok check

Array

1

56,4

 

 

im thinking theres something the matter with my $R1 array - or attempt at array

 

I thought R1 would be 'checked' NOT 'on' ???

 

also it is checking every checkbox, i thought it would only  REPORT  a  check on the checkbox that i entered a check against???

 

 

Would REALLY appreciate any help - thanks

OK sorry

 

<?php 
for($i = 0; $i < $rows; $i++) {
?>
<tr>
<?php
    for($j = 0; $j < $columns; $j++) {
    //$R1[$i]='checked';
            echo "<TD><font color='8678E9'>" . $data[$i + ($j * $rows)] . "</font></TD>\n"; ?>
            <td><input type='checkbox' name="R1[]" method="POST" <?php  //if(isset($R1)){$R1='checked'; echo $R1;}
                                                        //elseif(!isset($R1)){$R1="unchecked"; echo $R1;}     
                                                        ////if($R1[$i]=="checked"){$R1[$i]=="checked"; echo $R1[$i];} 
                                                        ////elseif($R1[$i]=="unchecked"){$R1[$i]="unchecked"; echo $R1[$i];}?>  <?php 
                                                        //if(isset($R1[$i])){$R1[$i]="checked"; echo $R1[$i];} 
                                                        //elseif(!isset($R1[$t])){$R1[$t]="unchecked"; echo $R1[$t];} $t++;?>  <?php
                                                        
                                                        if (!isset($R1)){$Value='unchecked'; echo $Value;} 
                                                       elseif(isset($R1)){$Value='checked'; echo $Value;}?> method="POST"><br></td> 
                                                                                                                                                  
            <td><?php echo"<TD><font color='3923D6'><font size=4>" . $data2[$i + ($j * $rows)] . "</TD>\n"; ?></font></td> 
                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data4[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>
                <?php echo"<TD align='center'><font color='3923D6'><font size=4>" . $data5[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>
            <td align="centre"><input type="checkbox" name="ch1[]" method="POST" value=<?php echo $data3[$i + ($j * $rows)]?>,1 >M<br>           
                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,2 method="POST">Tu<br>
                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,3 method="POST">W<br>
                
                <input type="checkbox" name="ch4[]" <?php if (!isset($ch4)){$Value='unchecked'; echo $Value;} 
                                                       elseif(isset($ch4)){$Value='checked'; echo $Value;}?> value=<?php echo $data3[$i + ($j * $rows)]?>,4 method="POST">Th<br></td>
                                                       
            <td align="centre"><input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,5 method="POST">F<br>
                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,6 method="POST">Sa<br>
                <input type="checkbox" name="ch1[]" value=<?php echo $data3[$i + ($j * $rows)]?>,7 method="POST">Su<br></td>      
                <?php echo"<TD><font color='3923D6'><font size=4>" . $data6[$i + ($j * $rows)] . "</TD>\n"; ?></font></td>
            <td style='font-weight: bold; text-align: left;'><font color='8678E9'>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</font></td>
                <?php
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";
echo "<br>";

if(isset($R1)){
echo "R1 chkbox set - havent gone into submitok check";
echo "<br>";

echo $R1;
echo "<br>";
//echo $value;
//echo "<br>";

echo $size=count($R1);
echo "<br>";

    $i = 0;
      while ($i < $size) {
      echo $R1[0];
      echo "<br>";
      ++$i;
         } 
}


      echo "<br>";
      echo "<br>";
      echo "<br>";
      echo "<br>";



if (isset($ch4)){
echo "CHECKBOX SET";
echo "split the RN and DC - havent gone into submitok check";
echo "<br>";

echo $ch4;
echo "<br>";

echo $size=count($ch4);
echo "<br>";

    $i = 0;
      while ($i < $size) {
      echo $ch4[$i];
      echo "<br>";
      ++$i;
         } 
//$ch4==checked;
}
echo "<br>"


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.