Jump to content

sticky checkbox array


rbragg

Recommended Posts

I have a group of checkboxes that I would like to be sticky. I want the checkboxes that the user has checked to remain checked upon returning to the page from a validation error.

 

<?php
<input name="cbUnit[]" type="checkbox" value="Talon">Talon<br>
<input name="cbUnit[]" type="checkbox" value="Star">Star<br>
<input name="cbUnit[]" type="checkbox" value="Cater">Cater<br>
<input name="cbUnit[]" type="checkbox" value="Landrum">Landrum<br>
?>

 

I've tried this:

 

<?php
$sticky_cbUnit = ( (isset($_SESSION['cbUnit'])) && ($_SESSION['cbUnit'] == 'Talon') )?' checked="checked" ':' ';
echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />';
?>Talon<br>

 

I'm guessing the array [] is giving me the problem but it WILL be an array so I need it.  ???

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/
Share on other sites

<?php
<input name="cbUnit[]" type="checkbox" value="Talon">Talon<br>
<input name="cbUnit[]" type="checkbox" value="Star">Star<br>
<input name="cbUnit[]" type="checkbox" value="Cater">Cater<br>
<input name="cbUnit[]" type="checkbox" value="Landrum">Landrum<br>
?>

 


<?php
session_start();
$_SESSION['cbUnit']  = $_POST['cbUnit'];

$sticky_cbUnit = ( (in_array($_SESSION['cbUnit'], 'Talon') )?' checked="checked" ':' ';
//Note: the comparison is done in a case-sensitive manner.
echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />';
?>
Talon<br>

 

 

EDIT: Click Solved Bottom left

 

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247225
Share on other sites

Thanks for your help.

 

I have this:

 

<?php
$sticky_cbUnit = (in_array($_SESSION['cbUnit'],'Talon') )?' checked="checked" ':' ';
echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />';
?>Talon<br>

 

After receiving a parse error I removed an opening "(" in front of in_array. Now, I get this error:

 

Warning: in_array(): Wrong datatype for second argument in /food_test/ea_form.php on line 132

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247228
Share on other sites

I'd imagine the error is because the second parameter, 'Talon', is not seen as an array. I tried to store the values in an array:

 

$unitNames = array('Talon', 'Star', 'Cater', 'Landrum');

 

Then,

 

<?php
$sticky_cbUnit = (in_array($_SESSION['cbUnit'], $unitNames) )?' checked="checked" ':' ';
echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />';
?>Talon<br>

 

The datatype error goes away but I don't have sticky checkboxes.

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247285
Share on other sites

Hi boo_lolly!

 

I tried this:

 

<?php
$sticky_cbUnit = (in_array('cbUnit', $unitNames) )?' checked="checked" ':' ';
echo '<input type="checkbox" ' . $sticky_cbUnit . ' name="cbUnit[]" value="Talon" />';
?>Talon<br>

 

The boxes still aren't sticky. I also tried 'cbUnit[]'. Some background info - this page submits to itself and stores the values in the $_SESSION array.

 

<?php 
session_start();

if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) ) # only if form is submitted
{
  foreach ($_POST as $key => $value) # put the values into session variables
  {
    if ($key != "confirm")
    {
      $_SESSION[$key] = strip_tags($value);
    }
  }
  include 'shared/validate.php';
}
?>

 

If there is an error found at validate.php then it will return. My radiobuttons use the $_SESSION array and are successfully sticky:

 

<?php
$sticky_rbStart = ( (isset($_SESSION['rbStart'])) && ($_SESSION['rbStart'] == 'Spring') )?' checked="checked" ':' ';
echo '<input name="rbStart" id="rbStart" type="radio" value="Spring" ' . $sticky_rbStart . '/> '; ?>Spring

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247297
Share on other sites

i think your method is wrong. it should be something like this:

<?php
        $checkboxValues = array('Talon', 'Star', 'Cater', 'Landrum');

        foreach($checkboxValues as $key => $val){
                echo "<input type=\"checkbox\" name=\"cbUnit[]\" value=\"{$val}\"";
                foreach($_POST['cbUnit'] as $index => $checked){
                        echo (($val == $checked) ? (' checked="yes"') : (''));
                }
                echo ">{$val}\n";
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247372
Share on other sites

Ok thanks for sticking with me guys. I think I understand this method and now I have this:

 

<?php		
$cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum');

foreach($cbUnitName as $key => $unitName)
{
  echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'";
  echo "unit name: " . $unitName . "\n"; # added to see if $unitName is being set

  foreach($_POST['cbUnit'] as $index => $checkedUnit)
  {
    echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));
  }
  echo ">" . $unitName . "\n";
}
?>

 

For every item in the array, I get:

 

Warning: Invalid argument supplied for foreach() in /food_test/ea_form.php on line 139

>Talon

 

Line 139 is the second foreach construct. When I try to echo the $unitName I don't get anything ... not even the "unit name: ". So, I'm guessing that the first foreach is not functioning? I did also copy what you had here directly and had the same warning.

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247451
Share on other sites

try

<?php		
$cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum');

foreach($cbUnitName as $key => $unitName)
{
  echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'";
  echo "unit name: " . $unitName . "\n"; # added to see if $unitName is being set
  if (!isset($_POST['cbUnit'])) $_POST['cbUnit'] = array();
  foreach($_POST['cbUnit'] as $index => $checkedUnit)
  {
    echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));
  }
  echo ">" . $unitName . "\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247466
Share on other sites

i chek if is not set $_POST['cbUnit'] and if not exist (nothing is posted) i set $_POST['cbUnit'] to empty array

you can change this code in diferen way like this

if (isset($_POST['cbUnit'])) {
    foreach($_POST['cbUnit'] as $index => $checkedUnit)
    {
      echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));
    }
  }

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-247502
Share on other sites

I have another problem pertaining to this thread.

 

<?php		
$cbUnitName = array(''Talon', 'Star', 'Cater', 'Landrum');

foreach($cbUnitName as $key => $unitName)
{
  echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'";

  if ( !isset($_POST['cbUnit']) ) $_POST['cbUnit'] = array();
  {
    foreach($_POST['cbUnit'] as $index => $checkedUnit)
    {
      echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));;
    }
    echo ">" . $unitName . "<br>";
  }
}
?>

 

When I submit the form, cbUnit is stored in the Sessions array. However, I can't get the values (if there are any) to echo on the next page.

 

I move to that next page and print what is stored in the Sessions array. For cbUnit I get:

 

[cbUnit] => Array

 

In the space where I would like to echo the values I do this:

 

<?php
$cbUnit = $_SESSION['cbUnit'];

foreach($cbUnit as $key => $unit)
{
  echo $unit . "<br>";
}?>

 

I get the "Warning: Invalid argument supplied for foreach()" error.

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248108
Share on other sites

echo "<pre>". print_r($_SESSION['cbUnit'], true) ."</pre>\n";

 

That should print out the array of cbUnit. Another fun way to do it might be

 

<?php
if (is_array($_SESSION)) {
    foreach ($_SESSION as $key => $val) {
         if (is_array($val)) {
              print $key . " => " . print_r($val, true) . "<br />";
         }else {
              print $key . " => " . $val . "<br />";
         }
    }
}
?>

 

Hope that helps.

Link to comment
https://forums.phpfreaks.com/topic/50348-sticky-checkbox-array/#findComment-248295
Share on other sites

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.