I have check box inside a foreach loop so that i want to checkbox checked.
<?php
$heads = TCG\Voyager\Models\Head::all();
$i=0;
foreach($heads as $heads){
$headvalues=explode(',',$test->reporting_head);
echo $headvalues[$i];
echo '<br>';
echo $heads->id;
?>
<input type="checkbox" name="check_list[]" value="{{$heads->id}}_{{$heads->email}}" <?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>><label>{{$heads->headname}}</label>
<?php
$i++;
}
?>
Here echo $headvalues[$i] are 1, 2 ,3 and echo $heads->id are 1,3,2.so i want to checked all this check boxes but now only one check box is checked.i think my logic has issue.I tried the following
<?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>
Please help me
Instead of
<?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>
use
<?php echo (in_array($heads->id, $headvalues)? 'checked' : ''); ?>
Related
I want to get checked the checkbox value if this is checked
Here is my code:
<?php
foreach ($dosage_form_list as $val) {
?>
<input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>">
<?php echo $val['dosage_form'];?>
<?php
}
?>
Just append checked=checked if you need it to be.. checked.
<input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>" checked="<?= $someConditional ? 'checked' : ''; ?>">
I got my solution:
<?php
$dosage_form_list_exist = preg_replace('/\s*,\s*/', ',',
$dosage_form_list_exist); // It removes spaces after the comma only not
between the values like "Oral Solutions" etc
// $dosage_form_list_exist = str_replace(' ', '', $dosage_form_list_exist);
// It removes all the spaces from the Variable
$dosage_list = explode(',',$dosage_form_list_exist);
foreach ($dosage_form_list as $val) {
?>
<input type="checkbox" name="dosage_form_input[]" value="<?php echo
$val['dosage_form']?>" <?php echo (in_array($val['dosage_form'],$dosage_list)
? 'checked' : ''); ?>>
<?php echo $val['dosage_form'];?>
<?php
}
?>
I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>
try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>
I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);
I am trying to echo checkbox names, however I am unable to echo the name of the checkbox as all that appears is a '1' if the checkbox or radio button has been checked.
Does anyone know how I can change my code so that if the user selects that they are a vegetarian and allergic to peanut, "Vegetarian, Peanut Allergy" is displayed on my page instead of the number "1" for each checked item.
I would also prefer the cost of sitting in the VIP area to be echoed as apposed to "Yes" or "No".
Here is my the relevant code for the checkboxes and radio buttons on my reservations page:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['vege'] = isset($_POST['vege']);
}
if (isset($_POST['submit'])) {
$_SESSION['vegan'] = isset($_POST['vegan']);
}
if (isset($_POST['submit'])) {
$_SESSION['peanut'] = isset($_POST['peanut']);
}
if (isset($_POST['submit'])) {
$_SESSION['gluten'] = isset($_POST['gluten']);
}
if (isset($_POST['submit'])) {
$_SESSION['vip'] = isset($_POST['vip']);
}
?>
...
<strong>Dietary Requirements:</strong>
Vegetarian <input type="checkbox" name="vege" value="Vegetarian" <?php if(isset($_POST['vege'])) echo "checked='checked'"; ?>>
Vegan <input type="checkbox" name="vegan" value="Vegan" <?php if(isset($_POST['vegan'])) echo "checked='checked'"; ?>>
Peanut Allergy <input type="checkbox" name="peanut" value="Peanut Allergy" <?php if(isset($_POST['peanut'])) echo "checked='checked'"; ?>>
Gluten Allergy <input type="checkbox" name="gluten" value="Gluten Allergy" <?php if(isset($_POST['gluten'])) echo "checked='checked'"; ?>>
<strong> VIP area* : </strong> <br><br>
Yes (+£5) <input type="radio" name="vip" <?php if (isset($vip) && $vip=="Yes") echo "checked";?> value="Yes">
<br><span id="vip" class="error"><?php echo $vipErr;?></span><br>
No <input type="radio" name="vip" <?php if (isset($vip) && $vip=="No") echo "checked";?> value="No">
Here is the php I am currently using which echos "1" when the user makes a selection on my thank you page:
<b>Dietary Requirements: </b><?php echo $_SESSION['vege'];?><?php echo $_SESSION['vegan'];?><?php echo $_SESSION['peanut'];?><?php echo $_SESSION['gluten'];?>
<b>VIP Area Costs: </b>£<?php echo $_SESSION['vip'];?>
You assigned not the value to the session array! Because you used isset() as a assignment! And then you assigned 1 because isset returned 1, because they are set!
So try this:
<?php
session_start();
if ( !empty($_POST['vege']) )
$_SESSION['vege'] = $_POST['vege'];
if ( !empty($_POST['vegan']))
$_SESSION['vegan'] = $_POST['vegan'];
if ( !empty($_POST['peanut']))
$_SESSION['peanut'] = $_POST['peanut'];
if ( !empty($_POST['gluten']))
$_SESSION['gluten'] = $_POST['gluten'];
if ( !empty($_POST['vip']))
$_SESSION['vip'] = $_POST['vip'];
?>
Edit:
If you echo session index check if they are set like this:
<b>Dietary Requirements: </b>
<?php
if(isset($_SESSION['vege']))
echo $_SESSION['vege'];
if(isset($_SESSION['vegan']))
echo $_SESSION['vegan'];
if(isset($_SESSION['peanut']))
echo $_SESSION['peanut'];
if(isset($_SESSION['gluten']))
echo $_SESSION['gluten'];
?>
<b>VIP Area Costs: </b>£<?php if(isset($_SESSION['vip'])) echo $_SESSION['vip'];?>
Basically your error is displaying boolean from isset() function.
if you want to echo value just use
$_SESSION['vegan'] = isset($_POST['vegan']) ? $_POST['vegan']:"";
what does the code mean?
$x = $trueOrFalse ? $valueIfTrue : $valueIfFalse;
of course perform some sanitizing:
$_SESSION['vegan'] = htmlspecialchars($_POST['vegan']);
NEVER EVER ECHO PURE USER INPUT! Always sanitize! By not sanitizing user input you allow custom external scripts to easily be run as any HTML tag can be injected into the page such as <script>
Why are you checking your POST['submit'] variable 5 times?
You only need to check it once.
if (isset($_POST['submit'])) {
$_SESSION['vege'] = isset($_POST['vege']) ? $_POST['vege'];
$_SESSION['vegan'] = isset($_POST['vegan']) ? $_POST['vegan'];
$_SESSION['peanut'] = isset($_POST['peanut']) ? $_POST['peanut'];
$_SESSION['gluten'] = isset($_POST['gluten']) ? $_POST['gluten'];
$_SESSION['vip'] = isset($_POST['vip']) ? $_POST['vip'];
}
How do I make the values stay in checkboxes?
my problem is when I submit the form the values do not stay (in the form).
Below is my code :
Mca<input type="checkbox" name="qual[]" id="Mca" value="Mca"
<?php if($qual == "Mca") { echo ' checked="checked"' ; } ?>>
Mtech<input type="checkbox" name="qual[]" id="Mtech" value="Mtech"
<?php if($qual == "Mtech") { echo "checked"; } ?>>
Btech<input type="checkbox" name="qual[]" id="Btech" value="Btech"
<?php if($qual == "Btech") { echo "checked"; } ?>>
Try this because $qual is an array.
/* Your $qual should be */ <?php $qual = $GET['qual']; ?>
<?php if($qual == "Mca")
must be changed to :
<?php if(in_array("Mca",$qual)
Try using better code for your form:
<?php
$checkboxes = array('Mca', 'Mtech', 'Btech');
foreach($checkboxes as $k => $v){
echo '<input '.($v==$qual[$k]? 'checked="checked" ': '').'type="checkbox" name="qual[]" id="'.$v.'" value="'.$v.'">';
}
?>