It's a really silly problem.i don't know how i get this but i get.
i have a array and i wants to match array values with checkbox which came from while loop.So how is it possible.
//Array that i want to match with checkbox
$filter = explode(',', $getproduct->specification_filter);
<table class="table" style="background-color: white;">
<tbody>
<?php
while($fch = $allfilter->fetch_array()){
?>
<tr>
<th><?=$fch[2]?></th>
<?php
$sqlbv="SELECT * FROM product_filter where idd='$fch[4]'";
$resultbv=$conn->query($sqlbv);
while($rowbv = $resultbv->fetch_array()){
?>
<td>
<label class="ckbox ckbox-danger">
<input type="checkbox" value="<?=$rowbv[0]?>" name="pfilter[]">
<span> <?=$rowbv[3]?></span>
</label>
</td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
Now i wants to check Checkbox field if $filterarrays value match with checkbox value. So how can i do that ?
since your $filter is an array use in_array
<input type="checkbox" value="<?=$rowbv[0]?>" <?= in_array($rowbv[0], $filter)? 'checked':'' ?> name="pfilter[]">
for more details see in_array.
Just use in_array() to check this. If its true and marked checked.
<input type="checkbox" value="<?=$rowbv[0]?>" name="pfilter[]" <?php echo in_array($rowbv[0],$filter) ? 'checked="checked"' : '';?>>
Hope this could help you.
In input checkbox, you can check adding attribute 'checked'.
For that, you can write
< input type="checkbox" < ?=($rowbv[0] ? 'checked' : '')?> name="pfilter[]"/>
else
< input type="checkbox" < ?php echo $rowbv[0] ? 'checked' : '';?> name="pfilter[]"/>
Related
Instead of having 2 inputs with the full-time and the contractor box ticked, I have 4 inputs see below. How can I avoid the duplication of the inputs, and ends up with two inputs only that will have the boxes ticked for full-time and contractor?
Here is the code:
<?php
$jobEmploymentType = "FULL_TIME,CONTRACTOR";
$jobEmploymentTypeExplode = (explode(",",$jobEmploymentType));
//print_r($jobEmploymentTypeExplode);
foreach ($jobEmploymentTypeExplode as $jobType) : ?>
<span class="asterisk">*</span> <label for="jobEmploymentType">Employment Type</label><br>
<input type="checkbox" class="w3-check" id="fullTime" name="fullTime" value="FULL_TIME" <?= ($jobType == "FULL_TIME")? "checked":"";?>>
<label for="fullTime"> FULL-TIME</label><br>
<input type="checkbox" class="w3-check" id="contractor" name="contractor" value="CONTRACTOR" <?= ($jobType == "CONTRACTOR")? "checked":"";?>>
<label for="contractor"> CONTRACTOR</label><br>
<?php endforeach; ?>
Expecting result:
I've found a way that works for me using in_array:
PHP Code:
$jobTypeExplode = (explode(",",$jobEmploymentType));
if(in_array('FULL_TIME',$jobTypeExplode)) {$fulltime = 'FULL_TIME';}
if (in_array('CONTRACTOR',$jobTypeExplode)) {$contractor = 'CONTRACTOR';}
HTML code:
<input type="checkbox" class="w3-check" id="fullTime" name="fullTime" value="FULL_TIME" <?= ($fulltime == "FULL_TIME")? "checked":"";?>>
<label for="fullTime"> FULL-TIME</label><br>
<input type="checkbox" class="w3-check" id="contractor" name="contractor" value="CONTRACTOR" <?= ($contractor == "CONTRACTOR")? "checked":"";?>>
<label for="contractor"> CONTRACTOR</label><br>
$jobEmploymentType = "FULL_TIME,CONTRACTOR";
Your problem is in the above line. When you give two values to the variable $jobEmploymentType the program is going to spit out the program twice.
how to set checkbox value if form validation fails in codeigniter. I have multiple select checkbox whose values populated from other dropdown selection. I want if form validation fails it should give me selected values. In my case it disappers. Any help will be appreciated.
my code:
<tbody class="location_id">
<?
$checked='';
if(isset($data1['loc_id']))
{
if($data1['loc_id']!='')
{
$checked='checked';
}
}
if(isset($data1['loc_id'])){
?>
<tr><td><input type="checkbox" name="loc_id[]" value="<?php if(isset($data1['loc_id'])){ echo $data1['loc_id'];} ?>" <?php echo set_checkbox('loc_id[]', $data1['loc_id']); ?> <?php echo $checked;?>></td><td><?php if(isset($data1['loc_id'])){echo get_name('location_tbl','loc_id',$data1['loc_id'],'loc_name');}?></td></tr>
<?php
}
?>
</tbody>
hope you understand.
In my controller I have set
$this->form_validation->set_rules('loc_id', '', 'trim|xss_clean');
sorry for grammar.
<input type="checkbox" style= "position: initial;" name="hobby[]" value="Sport"<?php if(strpos($hobby, 'Sport') !== false) echo "checked='checked'"; ?> >Sport
model:-
$hobby =implode(",", $this->input->post('hobby'));
Hello knowledgeable people. I am having trouble retrieving checkbox data from form. I have a site in which user can add checkboxes themselves, so I am writing them out like this:
<table style="padding:10px;">
<?php
$query_boolean = $DB->prepare("SELECT * FROM moduls WHERE type='boolean'") or die(mysql_error());
$query_boolean->execute();
while (($row = $query_boolean->fetch()) != false)
{
?>
<tr>
<td>
<?php echo $row->name ?>:
</td>
<td>
<?php
$s = "";
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="yes">%s', $row->id, Yes);
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="no">%s', $row->id, No);
echo $s;
?>
</td>
</tr>
<?php
}
?>
</table>
Now I have an advanced search in which I have to chech through every checkbox to see what has been selected (ether none, Yes, No, or both). How can I get the info from every checkbox in variables? Thank you so much!
To get POST data from checkboxes they must have attribute
checked="checked"
EDIT:
If you have 2 checkbox as this..
<input type="checkbox" checked="checked" class="textbox" name="boolean_yes" value="yes">
<input type="checkbox" class="textbox" name="boolean_no" value="no">
When you submit your form the checkbox with attribute checked will be sent as POST and the one without checked attribute will not be sent..
if(isset($_POST['search'])) {
$all_checked = array();
foreach($_POST as $key=>$value){
if(strpos($key, "boolean_") > -1){
$all_checked[$key] = $value;
}
}
var_dump($all_checked);
}
This way you will get inside $all_checked array all marked boxes.. All others checboxes are not marked!
if you want to get checkbox value then use checkbox name as array
<input type="checkbox" name="email1[]" value="">
an get it on another page by
<?php
$var2 = $_POST['email1'];
$v=implode(",",$var2);
echo $v;
?>
try it
I'm creating a form that has two checkboxes yes and no. I have added a yes or no into a mysql field called Completed.
The problem I have is getting the tick boxes to show a tick if the field that has a Yes or a No.
So depending what is in the field Completed "Yes" or "No" the tick boxes should be ticked or not ticked.
My code is like this:
<input type="checkbox" name="Completed" Completed="Yes" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > Yes</td>
<input type="checkbox" name="Completed" Completed="No" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > No</td>
Check boxes don't strike me as an appropriate control for this type of information. Have you considered using radio buttons instead? In any case, the answer to your question depends on how you are storing the data in the database. If it is a simple "Yes" or "No", then try something like the following:
<input type="radio" name="Completed" value="Yes" <?php echo $stuff["Completed"] == 'Yes' ? 'checked="checked"' : ''; ?> />
<?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> will always return true because you check for 'Yes' and 'No'. This values are true for php. You have to set the values 0 or 1 in db or check do something like this:
<?php echo $stuff["Completed"] == 'Yes' ? 'checked=" checked"' : ''; ?>
...
<?php echo $stuff["Completed"] == 'No' ? 'checked=" checked"' : ''; ?>
For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />