Friday, March 8, 2013

PHP form submission with check box, radio button and dropdown menu



1. For PHP checkbox form submission,  we still can only need one column in MySQL using PHP bitwise function &. (note we can have multi-choice in checkbox). Example code :
<input type="checkbox" name="competency[]" value="4" <?php if(($row['competency']&4) == 4){ echo('checked="checked"'); } ?>  />  Hard skill &nbsp;
<input type="checkbox" name="competency[]" value="2" <?php if(($row['competency']&2) == 2){ echo('checked="checked"'); } ?>  />  Soft skill &nbsp;
<input type="checkbox" name="competency[]" value="1" <?php if(($row['competency']&1) == 1){ echo('checked="checked"'); } ?>  />  Others

In the action side:
      $comp    =0;
     foreach($_POST['competency'] as $pc) {
      $comp = $comp + intval($pc);
     }

and insert $comp in database as column competency, which  will be used in $row['competency']

2. For dropdown menu:
 <select name="competency" id="competency">
     <option value="">- select  -</option>
     <option value="3" <?php if($row['competency'] == 3){ echo('selected="selected"'); } ?>  >  Hard skill </option>
     <option value="2" <?php if($row['competency'] == 2){ echo('selected="selected"'); } ?>  > Soft skill </option>
     <option value="1" <?php if($row['competency'] == 1){ echo('selected="selected"'); } ?>  > Others </option>
     </select>

In the action side:, we can insert $_POST['competency'] as it is a unique  value.


3. For radio button:
    <input type="radio" id="c1" class="input-radio" name="competency" value="3" <?php if($row['competency'] == 3){ echo('checked="checked"'); } ?>  />
   <label for="c1">Yes, with hard skill</label>
  
   <input type="radio" id="c2" class="input-radio" name="competency" value="2" <?php if($row['competency'] == 2){ echo('checked="checked"'); } ?>  />
   <label for="c2">Yes, with soft skill</label>
 
   <input type="radio" id="c3" class="input-radio" name="competency" value="1" <?php if($row['competency'] == 1){ echo('checked="checked"'); } ?>  />
   <label for="c3">No</label> <br />

In the action side:, we can insert $_POST['competency'] as it is a unique  value.

No comments:

Post a Comment