keep assoc array checkbox checked - php

I'm trying to keep a list of checkboxes checked until post is valid and no errors. Below is the code I'm using. I would be thankful for any help.
<?php foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks[]' name='drinks[]' value='<?php echo $option;?>' <?php if(!empty($_POST['drinks'])){if($_POST['drinks']==$option){ echo "checked='checked'" ; }}?> /><?php echo $options;?><br />
<?php } ?>
I can successfully display the checked checkboxes using implode however I need help for the above..

$_POST['drinks'] is an array. Also, the id value does not need to be 'drinks[]'
Try something like this:
$drinksIndex = 0;
$drinksPost = $_POST['drinks'];
foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks<?php echo $drinksIndex; ?>' name='drinks[<?php echo $drinksIndex; ?>]' value='<?php echo $option;?>'<?php
if( !empty($drinksPost[$drinksIndex]) ) echo " checked='checked'";
$drinksIndex++;
?> /><?php
echo $options;?><br />
<?php
} ?>
Amended the !empty($drinksPost[$drinksIndex]) part and changed to associative.
If this doesn't work, can you include $drinks_checkbox

Related

Problems when using nested foreach on checkboxes loaded dynamically

I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>

$_POST checkbox array only returns single value

The foreach loop at the end with $_POST['clients'] only returns one value. Whats wrong?
<?php $clients_to_display = Client::find_all(); ?>
<p><?php foreach ($clients_to_display as $key) {
echo $key->name; ?>:<input type='checkbox' name='clients[]' value=<?php $key->name; ?></><br/>
<?php } ?></p>
if(isset($_POST['submit'])){
$job->name = $_POST["job_name"];
$job->description = $_POST["job_description"];
$job->type = $_POST["job_type"];
$job->age = $_POST["job_age"];
foreach ($_POST['clients'] as $key) {
echo $key;
}
}
This code does nothing:
<?php $key->name; ?>
I think you want:
<?php echo($key->name); ?>
Also, it has to be enclosed in quotes:
value="<?php echo($key->name); ?>"
Also the markup is invalid. So, the full line should be:
echo($key->name); ?>:<input type="checkbox" name="clients[]" value="<?php echo($key->name); ?>" /><br/>

How to show checkboxes as checked when values are set in the database in codeigniter

I use foreach to show data came from database.
This is for Plan_data
<?php foreach ($veddingPlanData as $row) { ?>
<input box with value containing "echo row->value_name" >
<?php } ?>
This is for task data as check box [here I want to show the checkbox list in which the task is given to plan_id they appear as cheeked checkbox and remaining list with not checked status)
<?php foreach ($veddingPlanTaskMappingData as $row) { ?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked><?php echo $row->task_name?><br>
<?php } ?>
Here I show the whole task list in check box.
<?php foreach ($allVedingTasks as $row) { ?>
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name?><br>
<?php } ?>
I want to foreach the task_name list with selected some task as there mapped plan_id.
Finally I found It
<?php
$arrSelVedTask = array();
foreach ($veddingPlanTaskMappingData as $row) {
$arrSelVedTask[$row->task_id] = '';
}
?>
<div class="form-group">
<lable for="task_id" class="control-label col-sm-12">Plan Task LIST:</lable>
<div class="col-sm-10">
<div class="checkbox" style="margin-left:40px;">
<?php foreach ($allVedingTasks as $row) {
if(isset($arrSelVedTask[$row->task_id])) {
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked ><?php echo $row->task_name; ?><br><?php
}
else{
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name; ?><br><?php
}
}
?>
Simply add a check:
<?php
foreach ($veddingPlanTaskMappingData as $row) {
$checked = ($row->task_id == YOUR_CONDITION) ? 'checked="checked"' : '';
?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" <?php echo $checked?>><?php echo $row->task_name?><br>
<?php } ?>
Use set_checkbox Of CI
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" <?php echo set_checkbox('task_id', $row->task_id); ?> ><?php echo $row->task_name?>
at first in your model fetch the value of checkbox which is in your database then call that function in controller and pass the value to view page. then use if statement with every checkbox item to check whether the value is same or not. and write echo "checked";command if the condition match.
First of all we will take all the categories than we are taking an Id and using it for showing the checked list from the database. First we select the table values based on Id than we will fetch the record. We are having the data in the database column using , comma like 1,2,3,5,9. Than we save it in array and at last we just show that array to the page.
$table="select * from categorytable";
$this->result=mysqli_query($this->con,$table);
$this->count=mysqli_num_rows($this->result);
if($this->count < 1)
{
return "<div class='form-group'>".$messages->getResponseMessage("No category is added.", "Warning")."</div>";
}
else
{
$alert='';
$select="select Category from table where Id='$id'";
$result= mysqli_query($this->con, $select);
$amrow= mysqli_fetch_array($result);
$getAminity=$amrow["Category"];
$getArray= explode(",",$getAminity);
while($row= mysqli_fetch_array($this->result))
{
if(in_array($row[Id],$getArray)){
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' checked name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
else
{
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
}

form checkbox value php

I have a form created by a while loop in php like this :
<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
$i++;
echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
echo $line->title;
echo'<br>';
$result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>
Then later on the code I'd like to store the values of the checked checkboxes only in a new array :
if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
foreach($result_array as $result)
{
if($result == $_REQUEST[$j]) <<<< ERROR
{
$final_array[$j] = $result;
}
}
}
Surprisingly, this code does not work at all.
The error message is "Notice : Undefined offset: 0", then offset 1 then 2 etc ...
The line where the message says theres an error is the marked one.
I really have no idea how to do this. Someone ? =)
Don't try to do it this way, this just makes it hard to process, just use a grouping name array: name="checkboxes[<?php echo $i; ?>]", then on the submission, all values that are checked should simply go to $_POST['checkboxes']. Here's the idea:
<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
<input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />
PHP that will process the form:
if(isset($_POST['submit'], $_POST['nid'])) {
$ids = $_POST['nid']; // all the selected ids are in here
// the rest of stuff that you want to do with it
}

how to loop and compare two arrays at the same time(PHP)

I have two array. What I want to do is compare the key ['user_id'] of two arrays, if they are the same, pass the ['user_id'] and ['ref'] in hidden form. I tried to put them into two foreach, but system seems into a dead lock.
<?php foreach($_SESSION['printing_set'] as $data) { ?>
<?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
<input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];} ?>" />
<?php } ?>
<?php } ?>
What is the right way to do that?
What you are doing is printing again and again the part of '<input type="hidden" name="...'. here is what you should do
<?php
echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="'; //olny one time.
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
if($userlist['user_id'] == $data['user_id']){
echo $userlist['ref']; //only if condition is true
}
}
}
echo '" />'; //only one time
?>
You've got some funky formatting going on, so it's hard to tell where the error might be. Try it like this:
<?php
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
$value = "";
if($userlist['user_id'] == $data['user_id'])
$value = $userlist['ref'];
echo "<input type='hidden' name='reference$user_id' value='$value' /> \n";
}
}
?>

Categories