0

I am developing an application with codeigniter. My issue is iam not able to post value from second check box(checkbox in else condition).

View page

<div class="form-group">             
    <?php if($pro[0]['p_featured']==1){ ?>
     <label class="checkbox-inline">
          <input type="checkbox" value="1" name="featured" checked>featured 
     Product</label><?php } else {?>
    <label class="checkbox-inline">
     <input type="checkbox" value="2" name="featured" >featured 
      Product</label> 
   <?php } ?>
  </div>

My controller

echo $this->input->post('featured'); exit;

I am able to post value from first condition. But not able to POST value from second condition

Yadhu Babu
  • 1,503
  • 2
  • 13
  • 25

3 Answers3

0

try

isset($this->input->post('featured'))?1:2;
lochawala
  • 96
  • 4
  • 12
0

Form elements of <input type='checkbox'> will not post a value if not checked. So, if the checkbox is in second condition and it remains unchecked when the form is submitted, then $this->input->post('featured'); will return NULL.

If you were thinking to use the value property to determine which checkbox was output to browser that won't work because value won't be posted if the checkbox is not checked.

Consider this solution

<div class="form-group">             
    <label class="checkbox-inline">
        <input type="checkbox" name="featured" <?php echo $pro[0]['p_featured'] == 1 ? "checked" : NULL; ?>>featured 
        Product</label>
</div>

This produces a checkbox that is checked when $pro[0]['p_featured'] == 1.

Notice the value attribute has been removed. That attribute is not needed in this situation.

When processing the form submit you confirm the state of the checkbox like this

$feature_checked = NULL !== $this->input->post('featured');

The value of $feature_checked will be TRUE if the box is checked, else it will be FALSE.

DFriend
  • 8,869
  • 1
  • 13
  • 26
0
<input type="checkbox" id="wifi" name="wi_fi" value="1" <?php if ($post['wi_fi']==1) :?>checked<?php endif;?>/><label for="wifi" >Wi-Fi</label>