0

Suppose I have a form like this, where checkboxes are repeating fields:

<form action="" method="post">
    <?php for($i=0; $i<3; $i++) { ?>
       <input type="checkbox" name="ch[]" value="1">
    <?php } ?>
    <button type="submit" name="submit">submit</button>
</form>

I'm on WordPress and using custom meta boxes for dealing with it. So I declared the form within the callback function of the metabox, and receiving the values in another save function that's hooked with save_post and new_to_publish action hooks.

So what's happening: when I click on the button, the metabox callback submitted the form, and the hooked function receives it. (Can be visible at add_meta_box() WordPress Codex) Suppose my save function contains:

<?php
if( isset($_POST['submit']) ) {
    $chb = $_POST['ch'];
    $result = array();
    foreach ($chb as $cb) {
        $result[] = array( 'isactive' => $cb );
    }
    var_dump($result);
}
?>

It's showing that, checkboxes are not returning any value when unchecked. I considered all the server-side solutions mentioned here: Post the checkboxes that are unchecked

PROBLEM is, whenever the form is submitted, it's taking the checkboxes' values to an array(), so I can't check the array values like:

if( !isset( $_POST['ch'] ) || empty( $_POST['ch'] ) ) {
    $chb = 0;
} else {
    $chb = 1;
}

I also tried hidden field with 0 value, accompanied with array_unique(), but nothing seems work for me.

How can I deal with unchecked checkboxes in an array so that they can't be null, and my foreach can loop through all of 'em and store data accordingly, and correct?

I want to avoid JavaScripts solutions.

Community
  • 1
  • 1
Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
  • @Fred-ii- let it be. it's not the issue. it's submitting the form and I'm having the value. I just made the form to minimize the issue to core. – Mayeenul Islam Apr 09 '15 at 01:53
  • @Marty I already mentioned *I also tried hidden field with `0` value, accompanied with `array_unique()`, but nothing seems work for me.* with two fields with same name it'll occur six fields of same name, and the `array_unique()` takes a 1-1-1 combination and make an output of 1-0-1. :( – Mayeenul Islam Apr 09 '15 at 01:54
  • @Fred-ii- please don't take it otherwise. I's having the value even with the missing `type`. I checked with or without. – Mayeenul Islam Apr 09 '15 at 01:59
  • @MayeenulIslam Yep, my bad :) You could use that solution if you output the array indexes in the name e.g. `name="ch[0]"`. – Marty Apr 09 '15 at 02:04

3 Answers3

1

If you name the checkboxes with an index in them, like so:

<input type="checkbox" name="chk_<?php echo $i ?>" value="1">

Then you could loop through them like so:

<?php
   $chkBoxes = array();
   foreach ($_POST as $k => $v) {
      if (strpos("chk_",$k) === 0) {
          $cbIndex = str_replace('chk_', '', $k);
          $chkBoxes[$cbIndex] = $v;
      }
   }

Then to test if a checkbox was checked and sent to the server, you could use:

<?php 
    if (isset($chkBoxes[$cbIndex])) 

Remember - the value of the checkbox is only sent if it was checked: Does <input type="checkbox" /> only post data if it's checked?

Community
  • 1
  • 1
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • We actually solved the issue like this, but I's not satisfied with it. That's why I asked the question. Anyways, it seems it's the only solution. :\ – Mayeenul Islam Apr 09 '15 at 03:45
0

Add a hidden field in the form with the number of checkboxes, and use the index $i for the array ch[]:

<form action="" method="post">
    <input type="hidden" name="num" value="<?= $num = 3 ?>">
    <?php for($i=0; $i<$num; $i++) { ?>
       <input type="checkbox" name="ch[<?= $i ?>]" value="1">
    <?php } ?>
    <button type="submit" name="submit">submit</button>
</form>

Then:

<?php
if( isset($_POST['submit']) ) {
    $chb = $_POST['ch'];
    $num = $_POST['num'];
    $result = array();
    for($i=0; $i<$num; $i++) {
        $result[$i]['isactive'] = isset($chb[$i]) ? 1 : 0;
    }
    var_dump($result);
}
?>
d79
  • 3,699
  • 1
  • 22
  • 36
0

first of all i copied ideas from many people inside this forum! i only synthesized the way!

  1. my function to get data from my base located inside the $anoigmata class:
    function getall() { $data = $this->_db->get('<table_name>', array('ID', '>=', 0)); $results = $data->results(); $rows = $data->count(); return array($results, $rows); }
  2. code inside the create function to save all the secondary options that are not presented on the site.

    $fields_Κ = array('history' => serialize(array( 'checkboxes' => Input::get('checkboxes') )), ); $data = $this->_db->insert('ΚΕΛΥΦΟΣ', $fields_Κ); return true;

  3. the php-html code that shows the result

<form method="post"> <div class="form-group"> <label for="checkboxes[]">checkboxes title</label><br>

  `<?php 
  for ($i=0; $i<$anoigmata->getall()[1]; $i++) {
   echo "
   <input type='hidden' name='checkboxes[$i]' value='0' />
   <input type='checkbox' id='checkboxes[$i]' name='checkboxes[$i]' value='".$anoigmata->getall()[0][$i]->ΕΜΒΑΔΟΝ."' />".$anoigmata->getall()[0][$i]->ΠΕΡΙΓΡΑΦΗ."<br>";}?>`

`</div><button type="submit" class="btn btn-success">Submit</button></form>`

***ΕΜΒΑΔΟΝ and ΠΕΡΙΓΡΑΦΗ are the row names from my table that i'm saving!!!

i hope i helped a little..!