how to extract checkbox information and store it in database with codeigniter
I have this code in the php file. it lists all the interests as checkboxes and allows to input a text field "other". The user should select several of these interests and save. Now, when the user clicks on the submit button it should take it to the controller and controller should extract them and store it in the database. I have one file thats coded to do that, but its no good and too confusing.
Here is the form in the normal php file
<form method="post" action="<?php echo site_url("userProfile/update_bio");?>" name="editForm" id="edit-form">
<div> <!-- description start -->
<div> <p <p class="special-p">Name:</p> <hr> </div>
<p class="used-p"> <input type="text" name="name" value="<?php echo $this->dx_auth->get_username(); ?>"></textarea> </p> <!-- should be real name -->
</div> <!-- description end -->
<div> <!-- description start -->
<div> <p <p class="special-p">Description:</p> <hr> </div>
<p class="used-p"> <textarea name="description"></textarea> </p>
</div> <!-- description end -->
<div> <!-- interests start -->
<div> <p class="special-p">Interests:</p> <开发者_C百科hr class="special-hr2"> </div>
<p class="used-p">I am interested in:</p>
<ul>
<?php
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox" name="checks1[]" value="'.$row->id.'"/> '.$row->name.'</li>';
}
?>
<li><input type="checkbox" name="checks1[]" value="other"/> Other: <input type="text" name="otherText"/></li>
</ul>
</div> <!-- interests start -->
<div>
<input id="editbutton" type="submit" value="Save" name="editBioButton" class="small green button"/>
</div>
</form>
The controller function is empty. The problem is I don't know how will it receive the checkboxes.
Thanx in advance
foreach($this->input->post(checks1) as $check)
{
echo $check;
}
But I believe that if the checkbox isn't checked, it won't appear in the $_POST
array. To combat this, give each checkbox a specific index:
<?php
$count=0;
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox"
name="checks1[' . $count . ']" value="'.$row->id.'"/> '.$row->name.'</li>';
$count++;
}
may do what you require. Your controller would be:
foreach($this->input->post(checks1) as $key => $value)
{
echo $key . ' - ' . $value;
}
精彩评论