Jump to content

sticky checkbox array


rbragg

Recommended Posts

I still get [cbUnit] => Array.  :-X All other values that are not arrays are passed successfully. I have another array from a checkbox group that is doing the same thing.

 

Could this not be caused by the foreach statements in my form?

Link to comment
Share on other sites

Just noticed this:

 

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

 

I would think that would throw a syntax error, if that was not fixed I would try fixing it to this:

 

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

 

That and this does not make any sense....

 

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

 

Try this instead

 

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

 

Only run through the code if it isset, do not set it to an empty array if it is not. You can also throw in an array check like so:

 

  if (isset($_POST['cbUnit']) && is_array($_POST['cbUnit']) )  {
    foreach($_POST['cbUnit'] as $index => $checkedUnit)
    {
      echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));;
    }
    echo ">" . $unitName . "<br>";
  }else {
     echo 'Did not enter array'; // just for testing purposes.
}

 

Too avoid some errors.

Link to comment
Share on other sites

Thank you for your help. I used your last example and still have the same problem.  :'(

 

The empty array idea was suggested by a helper earlier in this thread. Also, the extra single quote was only mistyped here and is not represented in my code.

 

Hmmm... I'm running out of things to change. LOL!

Link to comment
Share on other sites

Sure! The only form element I'm posting is the cbUnit array to keep you from scrolling to China.  :P

 

Page 1 (user enters info):

 

<?php 
session_start();

if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) )
{

foreach ($_POST as $key => $value)
  {
    if ($key != "confirm")
    {
      $_SESSION[$key] = strip_tags($value);
    }
  }
  include 'shared/validate.php';
}
?>	

<form name="application" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

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

foreach($cbUnitName as $key => $unitName)
{
  echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'";
  
  if (isset($_POST['cbUnit']) && is_array($_POST['cbUnit']) )
  { 
    foreach($_POST['cbUnit'] as $index => $checkedUnit)
    {
      echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));;
    }

    echo ">" . $unitName . "<br>";
  }
  else 
  {
    echo 'Did not enter array'; # just for testing purposes.
  }
}?>

<input type="submit" name="confirm" id="confirm" value="confirm" class="style1">
</form>

 

Page 2 (info is echoed):

 

<?php 
session_start();

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

$cbUnit = $_SESSION['cbUnit'];

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

 

The problem is that the array values are not displayed when echoing. Only [cbUnit] => Array and the Warning: Invalid argument supplied for foreach() error.

 

Please don't find something very simple and make me feel like an idiot. I'm very sensitive today.  8)

Link to comment
Share on other sites

<?php 
session_start();

if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) )
{

foreach ($_POST as $key => $value)
  {
    if ($key != "confirm")
    {
       if (is_array($value)) {
            $_SESSION[$key] = $value;
        }else {
            $_SESSION[$key] = strip_tags($value); // this might be the problem try
        }
    }
  }
  include 'shared/validate.php';
}

 

Strip_tags should only take a string, not an array, which in return when it did the array part it took it literaly as if you just printed it. Anytime you print an array it should display Array.

 

Hope that makes sense but give that a shot.

Link to comment
Share on other sites

It made good sense. This was successful in echoing the contents of the array. However, on the form it would not display $unitName (the name next to the checkbox) unless I did as that previous helper suggested and add:

 

$_POST['cbUnit'] = array();

 

Also, since the array is no longer stored as a string I am no longer able to implode it for db insertion"

 

$cbUnitString = implode(", ",$unit);

 

Is there a way around this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.