0

I have 3 rows and each row has a checkbox, when I uncheck all boxes and click save I receive the error in my else statement. If I uncheck on box at a time and click save, when I get to the last checkbox and click save I also receive the error in the else statement.

Here is my question - How do I allow all checkboxes to be unchecked without receiving an error?

Here is my PHP code:

//Update Social Preferences with new value for display on homepage
$settingsArray = array('myfacebook', 'mytwitter', 'mygoogle');
if(isset($_POST['btn-save']))
{          
      if(isset( $_POST['mysocial']))
      {
          $values = array();
      foreach($_POST['mysocial'] as $selection )
      {  if(in_array($selection, $settingsArray))
         {  $values[ $selection ] = 1; }
         else
         {  $values[ $selection ] = 0; }
      } // end of foreach.

      $user_id = $_SESSION['user']['id'];

      try // save user selection to the database
      {

        $stmt = $db->prepare("UPDATE social_preferences SET my_facebook = :myfacebook, my_twitter = :mytwitter, my_google = :mygoogle WHERE user_id = :userID");
        $stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
        $stmt->bindParam(':myfacebook', $values['myfacebook']);
        $stmt->bindParam(':mytwitter', $values['mytwitter']);
        $stmt->bindParam(':mygoogle', $values['mygoogle']);
        $stmt->execute();

        header("Location: admin-social-test.php"); 
        die("Redirecting to admin-social-test.php"); 
       }  catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
  }
  else
  {  $noCheckbox = 'No checkbox selection made...'; }     
} // End of, if statement from the button check 

Here is my HTML Code:

<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
iamthestreets
  • 733
  • 1
  • 15
  • 38
  • 1
    What is your actual question? – Mike Oct 02 '14 at 15:34
  • 1
    Checkboxes not checked don't get sent in the POST. In other words you never see an unchecked checkbox in the POST array unless you do something specifically to fix that. – Jay Blanchard Oct 02 '14 at 15:38
  • 1
    possible duplicate of [How come checkbox state is not always passed along to PHP script?](http://stackoverflow.com/questions/2520952/how-come-checkbox-state-is-not-always-passed-along-to-php-script) – Jay Blanchard Oct 02 '14 at 15:39
  • @JayBlanchard I'm not sure I understand. When I uncheck 1 box it will send it to POST and update the database. It's only when I get to the last checkbox or try to uncheck all at once, it does not work. – iamthestreets Oct 02 '14 at 15:52
  • The linked question does not work for me. I would have to re-write code for what looks like a work around to me. Are there any other solutions? – iamthestreets Oct 03 '14 at 14:30
  • I stand correct I made a few changes based on the answer to the linked question and everything seems to be working. – iamthestreets Oct 03 '14 at 16:08

1 Answers1

0

I added a hidden input for each one.

So I changes this:

<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />

To this:

<input type="hidden" name="mysocial[]" value="myfacebook1" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mytwitter1" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mygoogle1" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />

This seems to have got it working.

iamthestreets
  • 733
  • 1
  • 15
  • 38