0

I have a (MySQL) database table that looks like:

CREATE TABLE `dm_webcategory` (
  `cat_id` int(10) DEFAULT NULL,
  `PUB_CATEGORY` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  `web_only` tinyint(1) DEFAULT '0',
  UNIQUE KEY `dm_webcategory_pk` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I want to display and save ALL rows in this table as checkboxes If checked, will mark web_only as 1, and unchecked will mark web_only as 0.

Displaying them is simple:

<form method="post" action="">
<?php foreach($categories as $category): ?>
    <input type="checkbox" name="category[]" value="<?php echo $category->cat_id;?>" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
    <input type="submit" class="btn btn-primary" value="Save" />
    <br>
</form>

But how should I go about saving this list of checkboxes?

I can access all of them using $_POST['category'], but this will only $_POST the checked boxes?

To answer my own question, I could update the whole table web_only field to 0, and then foreach the posted result to update individually to 1, assuming all those not included were unchecked.

SQL: UPDATE dm_webcategory SET web_only = 0

Then PHP:

foreach($this->input->post('category') as $category)
{
    $this->db->update('dm_webcategory', array('web_only' => '1'), array('cat_id' => $category));
}

The above doesn't feel very 'safe' though, is there a better way? What about the scenario when I wasn't displaying / saving every row in the table?

Jonathan Clark
  • 1,180
  • 1
  • 8
  • 26
  • nothing to do with php. unchecked checkboxes are NOT submitted with the rest of the html. that's standard html/browser behavior, no matter what language you're using on the server. – Marc B Mar 22 '16 at 16:37
  • @MarcB Good point, have edited the title so it's more generic, a PHP - based answer would be preferable, but agreed, it's more the alternate idea (if there is one) that I'm after. – Jonathan Clark Mar 22 '16 at 16:40
  • You can do a trick with adding a hidden input with value before your checkbox and instead the id use 1 as the checkbox value. Use the same name tag with the use of the id `name="category[cat_id;?>]"` so you're gonna know if it's checked or not. And with a simple foreach it should work. – Nergal Mar 22 '16 at 16:46
  • @JonathanClark please try the answer as example below – Heemanshu Bhalla Mar 22 '16 at 16:51
  • Possible duplicate of [Submit an HTML form with empty checkboxes](http://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes) – Andy Hoffner Mar 22 '16 at 18:06

3 Answers3

1

Try this.

HTML:

<form method="post" action="">
<?php foreach($categories as $category): ?>
    <input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0"/> 
    <input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
    <input type="submit" class="btn btn-primary" value="Save" />
    <br>
</form>

PHP:

foreach($this->input->post('category') as $id => $value)
{
    $this->db->update('dm_webcategory', array('web_only' => $value), array('cat_id' => $id));
}
Nergal
  • 985
  • 1
  • 14
  • 29
  • Wow, that is excellent, I did not know you could put a value inside the square brackets of name="category[ ]". Also, I guess it's a tried and tested "trick" to have a hidden input above the checkbox that the checkbox will override if it is checked. I tested and having the hidden input underneath doesn't work. – Jonathan Clark Mar 22 '16 at 17:07
  • 1
    The trick here that the latest value in the DOM overrides the previous and the checkbox only gives value when checked. – Nergal Mar 23 '16 at 09:19
1

Try this form:

<form method="post" action="">
<?php foreach($categories as $category): ?>
    <input type="hidden" name="category[<?php echo $category->cat_id;?>]" value="0" />
    <input type="checkbox" name="category[<?php echo $category->cat_id;?>]" value="1" <?php if($category->web_only) echo "checked"; ?> /> <?php echo $category->PUB_CATEGORY;?>
<?php endforeach; ?>
    <input type="submit" class="btn btn-primary" value="Save" />
    <br>
</form>

and then:

foreach($this->input->post('category') as $cat_id => $web_only)
{
    $this->db->update('dm_webcategory', array('web_only' => $web_only), array('cat_id' => $cat_id));
}
David Aman
  • 281
  • 1
  • 8
  • Thanks, you were beaten to the answer I'm afraid, but still +1 for the `as $cat_id => $web_only`. I will also use that part, since it's more readable code :) – Jonathan Clark Mar 22 '16 at 17:13
0

You try to use following code

controller Code

$array = $this->input->post('s2');
$data['subject'] = implode(',',$array);
$this->student_model->saveForm($data);

view Code

<form method="post" action="">
Categories :
<?php foreach($categories as $category): ?>
<input type="checkbox"  name="s2[]" value="<?php echo $category->cat_id;?>">English
   <?php endforeach; ?>
<input type="submit" class="btn btn-primary" value="Save" />
<br>
</form>

model

  $this->db->insert('Tablename', $data);
Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53