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.
Related
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[]"/>
I have been working on a contact form for a website and I got this problem:
I have 4 checkboxgroups, every each of them have at least 3 checkboxes that are available to check. We don't want them to be required to send the email. So the code is this:
$CheckboxGroup1 = array();
if(isset($_POST['submit'])){
$name = $_POST['name'];
$attending = $_POST['attending'];
$CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] : 'Nothing checked';
HTML:
<h4>What kind of set-up would you like?</h4>
<p>Additional fees may apply for living room/specialty set-ups.</p>
<p>
<div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Living Room">
Living Room
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Conference Room">
Conference Room
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Other">
Other (please specify at the end of the form)
</label>
<br>
</div>
</p>
When none of the checkboxes is selected I get the message "Nothing checked" and when one of them is selected I get the value of it. The problem is when I select more than one, I get this in my email:
What kind of set-up will you like?: Array (not the name of those selected).
I do not know what I have to change to make it work the right way.
Any help would be very much appreciated.
You cannot print array with echo. Echoing array will give you string Array. Simple way in your case to get array values is to use implode:
echo implode(', ', $yourArray);
you have to change below line
Your code
$CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] : 'Nothing checked';
Change it to
if( isset($_POST['CheckboxGroup1']))
{
$CheckboxGroup1 =implode(", ",$_POST['CheckboxGroup1']);
}
else{
$CheckboxGroup1="Nothing checked";
}
Hello I am using various check boxes with different values and than storing the vales inside DataBase as all the checkboxes values are store inside Array
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<input type="checkbox" value="Spa" id="details_11" name="utilities[]">
and than store in the database like: Gym, Spa.
Later I am making MySQL query inside edit form where I will be able to edit those and other parameters. For the other inputs I am using:
<?php if($row['data'] == 'Sample Data'){echo 'selected="selected"';}?>
In this case I would like to use something like the following:
<input type="checkbox" value="Gym" id="details_10" name="utilities[]"<?php if($row['utilities'] == 'Gym'){echo 'checked"';}?>>
Any help to achieve this result will be very welcome.
I've always used this in these cases.
<?php
if($row['utilities'] == 'Gym')
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]" checked="checked">
<?php
}
else
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<?php
}
?>
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
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"':'') ?> />