Codeigniter - Edit Form with Checkbox - php

I'm using codeigniter and have a problem with my edit form in checkbox. The checkbox have 2 values 1 and 0, if checked then the value is 1 and if unchecked then the value is 0.
here is my controller:
function updatepost($id=0){
$data = $_POST;
$this->db->where('id_post',$id);
$outp = $this->db->update('post',$data);
}
And here is my view
sport <input type="checkbox" name="sport" value="1" <?php if($data['sport'] == '1'){echo 'checked';}?> />
tekno <input type="checkbox" name="tekno" value="1" <?php if($data['tekno'] == '1'){echo 'checked';}?>/>
game <input type="checkbox" name="game" value="1" <?php if($data['game'] == '1'){echo 'checked';}?>/>
if i unchecked the checkbox, value should be '0';
My question is how to get value if the checkbox is uncheked?
Many thanks for the answer..

Checkboxes are posted only if they are checked.
In your controller, check if they are posted,
if posted, value is 1 else 0.
Example Code:
$sport = 0;
if (! empty($_POST['sport']) {
$sport = 1;
}
If you want to use ternary operators, use:
$sport = (! empty($_POST['sport']) ? 1 : 0;
So, final code:
function updatepost($id=0){
// Assign default values in view that they are not getting
// posted if they are not checked.
// If they are posted, they will be overriden in $data.
$data['sport'] = $data['tekno'] = $data['tekno'] = 0;
$data = $_POST;
$this->db->where('id_post',$id);
$outp = $this->db->update('post',$data);
}

In your controller file replace the $data = $_POST; line by the following code
$data['sport'] = (array_key_exists('sport',$_POST)) ? $_POST['sport'] : 0;
$data['tekno'] = (array_key_exists('tekno',$_POST)) ? $_POST['tekno'] : 0;
$data['game'] = (array_key_exists('game',$_POST)) ? $_POST['game'] : 0;

Related

How to check which radio button is clicked by PHP $_POST?

I am trying to know which radio button is clicked by PHP in a form with $_POST['...'].
This is my form:
<form action="" method="post" enctype="multipart/form-data">
<div class="input-group">
<span class="input-group-addon">Add:</span>
<input type='radio' name='add1' id='program' value='program' onchange="hideCollege()" checked> Program Intern</input>
<input type='radio' name='add1' id='department_coop' value='department_coop' onchange="showCollegelist()"> Department Cooperation</input>
<input type='radio' name='add1' id='foreigner' value='foreigner' onchange="hideCollege()"> Foreigner Area</input>
</div>
</form>
And I am trying to receive the values of the three radio buttons by:
if(isset($_POST['program'])){
$program = 1;
}
else if(isset($_POST['foreigner'])){
$foreigner = 1;
}
else if(isset($_POST['department_coop'])){
$coop = 1;
$college = $_POST['college'];
$department = $_POST['department'];
}
But it seems that no if statements are true, and turns out that no variables are assigned value. Does anyone know how to get to what I aim to do? Thanks a lot in advance.
Try this below section . Post request value should processed on form name attributes. So must check the conditional form post name value.
if(isset($_POST['add1']) && $_POST['add1']=='program' ){
$program = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='foreigner' ){
$foreigner = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='department_coop' ){
$coop = 1;
$college = $_POST['college'];
$department = $_POST['department'];
}
You should check the values of the radio button on the basis of name attribute.
if(isset($_POST['add1']) && $_POST['add1']=='program'){
$program = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='foreigner'){
$foreigner = 1;
}
else if(isset($_POST['add1']) && $_POST['add1']=='department_coop'){
$coop = 1;
$college = $_POST['college'];
$department = $_POST['department'];
}
Then you will get the checked value.
If you access $_POST['add1'] check for the value as it should either be program, department_coop or foreigner (in this case)

simple yes or no checkbox for forms

I have inserted a checkbox in my form.
My code:
<input type="checkbox" id="checkbox" name="checkbox" value="1"/>
if($checkbox = ($_POST['checkbox']) == '1')
{
$checkbox = "si";
}
else
{
$checkbox = "no";
}
I would like that if the checkbox is checked i receive "yes" otherwise "no".
Thanks.
You've written wrong if condition here, You cannot use assignment in conditions.
Also there is no need to assign value to any variable in checking condition, You can directly use $_POST['checkbox']. Like this,
if($_POST['checkbox'] == '1') {
$checkbox = "si";
} else {
$checkbox = "no";
}
Update:
A better option is to use isset() which determine if a variable is set and is not NULL. Like this,
if(isset($_POST['checkbox'])) {
$checkbox = "si";
} else {
$checkbox = "no";
}
Program will go in if condition only when user has checked the checkbox. In above case value attribute for <input> is not required. So your HTML will look something like this,
<input type="checkbox" id="checkbox" name="checkbox"/>
$request['checkbox_name'] = $request['checkbox_name'] == null ? 'N' : 'Y'; //for default value

Is there a way of updating a large form with checkboxes effectively?

Having an issue updating a large form with checkboxes effectively to database.
Just for illustration:
<form action="save.php" method="post">
<?php
for {$i=0;$i<1000;$i++) {
echo '<input type="checkbox" name="product-' . $i . '">';
}
<input type="submit">
</form>
<?php
$posted_values = $_POST;
foreach($posted_values as $key=>$p) {
$chkbox = $posted_values[$p];
$update = 0;
if ($chkbox == 'on') {
$update = 1;
}
//Do some "expensive" checking for each posted value
$save_dbarray[$key] = $update;
}
//Do the actual updating to databased based on array `save_dbarray`
Is there any way of just adding changed checkboxes to the save_dbarray? (Only checked boxes would be posted to $_POST, but I want unchecked values to be a part of the update as well if they have changed) I have to do some expensive checking for each posted value, therefore
UPDATE
I dont want to have loop through all 1000 checkboxes. I just want to loop through the changed (from checked to unchecked or from unchecked to checked) checkboxes, but in above case $posted_values would only return checkboxes that has checked values (from unchecked to checked)
<?php
//I DONT want to have to do like this:
for {$i=0;$i<1000;$i++) {
$prodnr = 'product-' . $i;
$chkbox = $_POST[$prodnr];
$update = 0;
if ($chkbox == 'on') {
$update = 1;
}
//Do some "expensive" checking for every value
$save_dbarray[$key] = $update;
}
//Do the actual updating to databased based on array `save_dbarray`
You can use HTML array inputs and PHP to do the same.
A sample code will be like below.
<form action="save.php" method="post">
<?php
for ($i=0;$i<1000;$i++) {
echo '<input type="checkbox" name="products[]" value="' . $i . '"> '. $i .'<br>';
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['products']); // Will contain your desired output
foreach($_POST['products'] as $i) {
$save_dbarray[$i] = 'on'; // 'on' or whatever value if you need.
// Actually you just need $_POST['products'], no need for this loop.
}
print_r($save_dbarray);
?>
EDIT
You need to loop through $_POST['products'] to find the new checked ones and you need to loop through $already_selected to find the unchecked ones.
<?php
// Select from db or something
$already_selected = array(2,3);
foreach($_POST['products'] as $i) {
if(!in_array($i,$already_selected)){
$save_dbarray[$i] = 'checked_update';
}
}
foreach($already_selected as $j) {
if(!in_array($j,$_POST['products'])){
$save_dbarray[$j] = 'unchecked_update';
}
}
print_r($save_dbarray);
// Do db update and select again and update $already_selected to display the checked ones
?>
<form action="save.php" method="post">
<?php
for ($i=1;$i<10;$i++) {
$checked = in_array($i, $already_selected) ? 'checked' : '';
echo '<input type="checkbox" name="products[]" value="' . $i . '" ' . $checked . '> '. $i .'<br>';
}
?>
<input type="submit">
</form>

post array keep only 1 value

PHP FORM CODE :
echo '<form name="planeSeat" action="koltuk-sec-process.php" method="post" >';
...
echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork" value="'.$rightplace.'" />';
...
echo '<input type="submit" name="formSubmit" value="Üye girişi yapmadan devam et"/>';
PHP PROCESS CODE :
$rest1 = array();
$rest2 = array();
$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest2[] = htmlspecialchars($_POST["artwork2"]);
if($rest1 != null){
print_r ($rest1);
}
if($rest2 != null){
print_r ($rest2);
}
When I send artwork, if I selected 2 checkboxes, $rest1 only keeps the last value. How can I send my all values?
You probably wanted to code
$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest1[] = htmlspecialchars($_POST["artwork2"]);
instead of
$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest2[] = htmlspecialchars($_POST["artwork2"]);
For one result, break should do the trick:
$foobar = array('uno', 'dos', 'tres');
foreach ($foobar as $fb) {
echo '<p>'.$fb.'</p>';
break;
}
the reason is simple : in case of multiple checkbox checked with the same name, you have to specified an array for the checkbox name, this way you will be able to traverse the posted values
echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork[]" value="'.$rightplace.'" />';
echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork[]" value="'.$rightplace.'" />'; // second checkbox
print_r ( $_POST['artwork'] ) // will display the artwork array
all of your checkbox with similar content should have the same name !!!!!

HTML multiple checkboxes with identical name= into PHP $_POST

So I have a 3rd party survey tool that generates html surveys, and for multi-select checkboxes in a given question, it will name them all the same:
<input type="checkbox" value="1" id="V25_1" name="V25">
<input type="checkbox" value="2" id="V25_2" name="V25">
Is there a way that all the selected values can be retrieved in PHP?
$_POST by default seems to simply store only the last selected value.
Your code should be approximately the following:
<select multiple="multiple">
<input type="checkbox" value="1" id="V25_1" name="V25[]">
<input type="checkbox" value="2" id="V25_2" name="V25[]">
</select>
V25[] means that you can get the value from an array. e.g. $_GET['V25'][0]
You could also specify an index if needed:
V25[1] or V25[a]
You could run something like this before the form submit:
$(":checkbox").each(function(){
$(this).attr("name",$(this).attr("id"));
});
or
$(":checkbox").each(function(){
$(this).attr("name",$(this).attr("name")+"[]");
});
It is possible to retrieve all variables when you have multiple elements with identical 'name' parameters.
After much scouring the internet for a solution, I wrote the following php script which grabs the raw post data, and parses it:
<?php
function RawPostToArray() {
$rawPostData = file_get_contents("php://input");
if($rawPostData) {
$rawPostData = explode('&', $rawPostData);
if($rawPostData && is_array($rawPostData) && count($rawPostData)) {
$result = array();
foreach($rawPostData as $entry) {
$thisArray = array();
$split = explode('=', urldecode($entry), 2);
$value = (count($split) == 2) ? $split[1] : '';
if(array_key_exists($split[0], $result)) {
if(is_array($result[$split[0]])) {
$result[$split[0]][] = $value;
}
else {
$thisArray[] = $result[$split[0]];
$thisArray[] = $value;
$result[$split[0]] = $thisArray;
}
}
else {
$result[$split[0]] = $split[1];
}
}
return $result;
}
else return array();
}
else return array();
}
?>
Any duplicate 'names' are bumped down into an array within the result, much the way $_POST does, when the HTML is coded correctly.
There is probably plenty of room for improvement here, and I'm sure not all situations are accounted for, but it works well for my application.
Suggestions are appreciated where improvements can be made.

Categories