I have this drop-down and user can select multiple options ,how can i keep selected value on form after submit button, if error comes on form
<select onclick="document.getElementById('cand_qual4').style.display='none'; " name="oca[]" id="oca" multiple="multiple">
<?php
$odrop = array('B COM','M COM','BBA','MBA','LLB','LLM','CPA','CIMA','MS FINANCE','DISA','CISA','OTHER');
foreach ($odrop as $odrop1)
{
echo '<option value="' . $odrop1 . '"' . (isset($_POST['oca']) && in_array($odrop1,$_POST['oca']) ? ' selected' : '') . '>' . $odrop1 . '</option>';
}
?>
</select>
instead of
$_POST['oca'] == $odrop1
condition as $_POST['oca'] would be an array, try
in_array($odrop1,$_POST['oca'])
TRY THIS-
echo '<option value="' . $odrop1 . '"' . (is_array($_POST['oca']) && in_array($odrop1,$_POST['oca'] ) ? ' selected' : '') . '>' . $odrop1 . '</option>';
Related
I have stuck ;/ I have activeradiolist and works ok, but I need to create another list bud with dropdowns item's
my code with $model for activeradioList
echo Html::activeradioList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
Now i try to convert this to dropdownList like ->
echo CHtml::dropDownList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
and have htmlspecialchars() expects parameter 1 to be string, object given
1) The CHtml is class from old Yii 1.x framework. Yii2 doesn't use the C prefix for its class names.
2) You are pairing the form field with your model instance so you should use the activeDropDownList() method instead of dropDownList().
3) In your radio button options you have item callback that is used to generate the html code for radio button. The drop down list doesn't have anything like that so you should remove it from its options. You can completly omit the fourth parameter because the item callback is the only option there.
So the code for drop down list should look like this:
echo Html::activeDropDownList($add, 'type_contact', $items);
I'm trying to return html, to my ajax request.
The ajax request requests a controller method to create a view according to what is clicked.
It's a simple form. But it should have a <select> tag in it.
The <select> tag should display different roles, with one role already selected. which can be assigned to different users.
So I tried creating an object as so:
$roles = $this->database->GetAllRoles();
$selectedRole = $this->database->GetAllRoles()->where('id', '=', $user->roles_id)->first();
foreach($roles as $role){
$selectObject =
'
<option selected value="' . $selectedRole['id'] . '">"' . $selectedRole['name'] . '"</option>
<option value="' . $roles->id . '">"' . $roles->name . '"</option>
';
}
$htmlFinal =
'
<form>
<!-- Form labels and inputs ... -->
<select class="form-control" type="text">
"' . $selectObject . '"
';
This is what it should look like, with a selected option in the form select tag, and more options when opened.
Obviously the above code doesn't work ofcourse, I tried alot of different approaches but I'm kind of stuck right now.
You are using = on the $selectObject variable inside the loop, so, the variable is overriden each iteration. You could use .= to append the HTML in the variable. Then, you shouldn't write the selected option inside the loop, because you will add it several times. Also, you use $roles but in your foreach, the current element is $role.
$selectObject = '<option selected value="' . $selectedRole['id'] . '">"' . $selectedRole['name'] . '"</option>';
foreach ($roles as $role) {
$selectObject .= '<option value="' . $role->id . '">"' . $role->name . '"</option>';
}
Finally, to avoid a duplicate of the selected role, you could add a if to add only other roles.
$selectObject = '<option selected value="' . $selectedRole['id'] . '">"' . $selectedRole['name'] . '"</option>';
foreach ($roles as $role) {
if ($role->id != $selectedRole['id']) {
$selectObject .= '<option value="' . $role->id . '">"' . $role->name . '"</option>';
}
}
Make your foreach loop looklike this:
if(array_key_exists('id',$selectedRole) && $selectedRole['id']==$roles->id)
<option value="' . $roles->id . '" selected >"' . $roles->name . '"</option>
else
<option value="' . $roles->id . '">"' . $roles->name . '"</option>
I have a select (dropdown) box. On each button click, I'd like it to load previously saved position. My code is:
<select name="myVariables" style="width:150px">
<?php
foreach ( $variables as $var ) {
echo "<option value=\"" . $var . "\"". $var . " </option>";}
?>
</select>
I have a variable $previouslySelected, and every time the button is clicked, this variable can change. I'd like to also change the currently selected option in the select box to the same value. I've tried with:
echo "<option value=\"" . $var . "\" <?=$previouslySelected==$var ? ' selected=\"selected\"' : '';?\>>". $var . " </option>";}
but this doesn't seem to work.
Also, I've tried this but it only works the first time.
How do you submit the form. With GET or POST?
With GET, than you must take the submitted value in this way:
$previouslySelected = isset($_GET['myVariables']) ? $_GET['myVariables'] : '';
Otherwise with POST:
$previouslySelected = isset($_POST['myVariables']) ? $_POST['myVariables'] : '';
In PHP 7 you can also do:
$previouslySelected = $_POST['myVariables'] ?? '';
EDIT: Write your echo more readable.
$selected = ($previouslySelected === $var) ? ' selected="selected"' : '';
echo '<option value="' . $var . '" ' . $selected . '>' . $var . '</option>';
I know this should be simple but I just can't seem to get my head around it.
I have a list of continents in a sql database that I get back using PHP DBO and display in a drop down list. What I then want to do is get the users preferred continent from the sql database and select that one in the list. E.g if the list contains World, Africa, Europe, N America and S America but the users favorite is 'Europe' I want that one selected. $getContinent is the users preference.
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
if ($getContinent != ''){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
I would be most grateful if someone could set me straight as I have found some examples on the internet but have been unable to get them to work :)
Your code should be like this
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
//just check if the option id is equal to the chosen value
if ($getContinent != '' && $getContinent==$row['CONTINENT_ID'] ){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
Its simple, as you guessed :D
You would use something like this:
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] . '">' . $row['CONTINENT_NAME'] . '</option>';
}
I hope that helps!
--al
You can use ternary operator
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] .
($getContinent == $row['CONTINENT_NAME']) ? '" selected="selected"' : '"' . '>' .
$row['CONTINENT_NAME'] . '</option>';
}
I have this nested array and i want to convert it into dropdown ,but at output it is just showing me combo-box with options as (array ,array,array)
<select name="pcity" id="pcity" multiple="multiple">
<?php
$pcitylist = array('Andaman and Nicobar' => array('North and Middle Andaman',
'South Andaman', 'Nicobar'), 'Andhra Pradesh' => array('Adilabad', 'Anantapur',
'Chittoor', 'East Godavari', 'Guntur', 'Hyderabad', 'Kadapa', 'Karimnagar',
'Khammam', 'Krishna', 'Kurnool', 'Mahbubnagar', 'Medak', 'Nalgonda', 'Nellore',
'Nizamabad', 'Prakasam', 'Rangareddi', 'Srikakulam', 'Vishakhapatnam',
'Vizianagaram', 'Warangal', 'West Godavari'), 'Arunachal Pradesh' => array('Anjaw',
'Changlang', 'East Kameng', 'Lohit', 'Lower Subansiri', 'Papum Pare', 'Tirap',
'Dibang Valley', 'Upper Subansiri', 'West Kameng'));
foreach ($pcitylist as $pcitylist1) {
echo '<option value="' . $pcitylist1 . '"' . (isset($_POST['pcity']) && $_POST['pcity'] ==
$pcitylist1 ? ' selected' : '') . '>' . $pcitylist1 . '</option>';
}
?>
</select>
i want it to display like this
<select>
<optgroup>Andaman and Nicobar</optgroup>
<option>North and Middle Andaman</option>
<option>South Andaman</option>.....
</select>
and so on...
foreach ($pcitylist as $key => $pcitylist1)
{
echo '<optgroup label="'.$key.'">';
foreach ($pcitylist1 as $finalCity) {
echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>' . $finalCity . '</option>';
}
echo '</optgroup>';
}
The $key holds the optgroup label. This will work with your array.
It is an multidimensional array... use one more for loop inside ur for loop and u will get the ouput..
Try the following..
foreach ($pcitylist as $key => $pcitylist1)
{
foreach ($pcitylist1 as $finalCity) {
echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>'.$key . $finalCity . '</option>';
}
}