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
}
?>
Related
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' : ''); ?>
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/>
I have a form which has a list of checkboxes to be filled in on insert to database as well as when editing. When editing I am trying to populate the fields with a list of fields from the database which is in the $groups variable and the checked value being checked against the group_id from groups with the group_id from the $user. It is working except it only fills out one checkbox and some users belong to multiple groups. Any ideas, or more efficient ways to do this.
Heres my code so far
<?php foreach($groups as $group) : ?>
<?php foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
} else {
$checked = '';
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
This problem is that you are searching all the $users['group'] array and not stopping when you find a match, so unless last $uG['group_id'] matches $group['id'] you continue and therefore clear $checked after potentially setting it.
So just add a break when you find a match.
Also, officially the correct way of setting the checked status is checked="checked" although most modern browsers are not that pedantic, its possibly better to stick to the HTML spec
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = 'checked="checked"';
break;
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
you could also use a ternary operator and in_array() to do this all in one simple statement
<?php
foreach($groups as $group) :
$checked = in_array( $group['id'], $user['groups'] ) ? 'checked="checked"' : '';
?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
Define $checked = ''; first then loop and change the value if match found
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
I have user titles, which are stored in database in this way. 1,2,6,10 and etc.
I want to check if the user already has this titles and if he does, to check the check box.
<?php
$user_titles = explode(',', $user['titles']);
//foreach($user_titles as $uTitles){
//echo $uTitles;
//}
?>
<input type="checkbox" name="title[]" value="1">Test1<br/>
<input type="checkbox" name="title[]" value="2">Test2<br/>
<input type="checkbox" name="title[]" value="3">Test3<br/>
You need to add the checked attribute to the checked ones.
<input type="checkbox" name="title[]" value="1" <?php if(in_array(1, $user_titles) echo 'checked="checked"'; ?>>Test1<br/>
Move the titles to new array and iterate trough them. Inside the loop, check if the value is in the $user_titles array and add 'checked' to the input tag.
<?php
$titles = [
1 => "Test1",
2 => "Test2",
3 => "Test3",
];
foreach ($titles as $value => $title) {
$checked = in_array($value, $user_titles) ? 'checked' : '';
echo '<input type="checkbox" name="title[]" value="' . $value . '" ' . $checked . '>' . $title . '<br/>';
}
?>
If i understand correctly you have the values stored as a string delimited by a comma.
Use an iteration method, for example:
<?php $checked = ''; ?>
<?php for($i = 1; $i <= count($possible_checkbox); $i++ ) {
if(in_array($i, $user_titles ) ) { $checked = 'checked'; } else {$checked = '' }
?>
<input type="checkbox" value="<?php echo $i; ?>" name="title[]" <?php echo $checked; ?> />
<?php } ?>
Hope it helps.
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"]);