I have the following code in my form to submit multiple checkbox
application/views/form.php
<form action="<?php echo base_url().'admin/addMessage/'.$pd_name; ?>" method= "POST">
<div class="form-group">
<label><strong>Message Type *</strong></label>
<input type="text" class="form-control" name="msg_type" id="msg_type" placeholder="Enter Message Type" required>
</div>
<label><strong>Message Bit *</strong></label>
<div class="form-group">
<div class="checkbox">
<table class="table">
<tbody>
<tr>
<td><label> P-1 </label><input type="checkbox" checked disabled readonly /></td>
<td><label id="P17"> P-17 </label><input name="bitmsg" type="checkbox" value="P17"/></td>
<td><label id="P33"> P-33 </label><input name="bitmsg" type="checkbox" value="P33" /></td>
<td><label id="P49"> P-49 </label><input name="bitmsg" type="checkbox" value="P49"/></td>
<td><label id="S65"> S-65 </label><input name="bitmsg" type="checkbox" value="S65" /></td>
<td><label id="S81"> S-81 </label><input name="bitmsg" type="checkbox" value="S81" /></td>
<td><label id="S97"> S-97 </label><input name="bitmsg" type="checkbox" value="S97" /></td>
<td><label id="S113"> S-113 </label><input name="bitmsg" type="checkbox" value="S113"/></td>
</tr>
</tbody>
</table>
</div>
</div>
<button type="button" class="btn btn-default" id="reset" data-dismiss="modal">Cancel</button>
<button type="submit" value="submit" class="btn btn-primary"> Add New Message Type</button>
</form>
application/controller/Admin.php
public function addMessage($pdID)
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('msg_type', 'MessageType', 'required');
if ($this->form_validation->run() == FALSE)
{
echo '<script>alert("Please Select Valid Message Type"); window.history.back();</script>';
}
else
{
$this->model_general->set_message($pdID);
}
}
application/models/Model_general.php
function set_message()
{
$this->load->helper('url');
$this->load->helper('form');
//set default values
$dbEntry = array(
'P17' => 0,'P33' => 0,'P49' => 0,'S65' => 0,'S81' => 0,'S97' => 0,'S113' => 0
);
$postData = $this->input->post();
foreach ($postData as $key => &$v) {
if (array_key_exists($key, $dbEntry)) {
$dbEntry[$key] = 1;
}
elseif ($key == 'bitmsg') {
$dbEntry[$v] = 1;
}
else {
$dbEntry[$key] = $v;
}
}
$this->db->insert('msg_format', $dbEntry);
}
I did select all checkbox.. but $postData only return the last checkbox that i checked. E.g:
$postData return "MsgType1,0,0,0,0,0,0,1" <<= if select all
$postData return "MsgType2,0,0,0,0,0,1,0" <<= if i select all except S-113
Something missing and I couldn't find it.. :/
Aucun commentaire:
Enregistrer un commentaire