I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>
Related
<select class="form-control default-select2 " id="group" data-size="10" data-live-search="true" data-style="btn-white" multiple="multiple">
<option value="" disabled="disabled" selected="selected">Select Group</option>
<!-- <option value="1">Admin</option>
<option value="4">User</option> -->
<?php $groups = $objUser->GetGroups(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php echo $group['id'] ?>"<?php echo ($data[0]['group_id'] == $group['id'] ? "selected='selected'" : "") ?>><?php echo $group['primary_name'] ?></option> <? } ?>
</select>
Now I want user to select multiple option at a time. i searched the whole internet but i couldn't find the answer to fix this issue. i dont know what i am missing.
Your code can actually manage multiple selections. Just hold Ctrl key and click on different options.
Exemple of multiple select : here
When you submit the form, you will be able to get all the selected options into an array.
I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.
For the 'wachtwoord' input field, the value is the current database record. But how can I show the current record for the 'Jaar' select field? It's currently showing one, but it has to be two.. Anyone know how to solve this?
<label for="password">Wachtwoord</label>
<input type="password" id="password" name="password" value="<?php echo $t['gids_password'] ?>"/>
<br/>
<label for="jaar">Jaar</label>
<select name="jaar" id="jaar">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
You would need to compare the result from the database with the value of the options. If it matches, you need to echo 'selected'; Like so:
<option value="1" <?php if($t['gids_jaar']==1){ echo 'selected'; } ?> >1</option>
<option value="2" <?php if($t['gids_jaar']==2){ echo 'selected'; } ?> >2</option>
<option value="3" <?php if($t['gids_jaar']==3){ echo 'selected'; } ?> >3</option>
NOTE:
$t['gids_jaar'] would be the value of the database. Dont forget to put space between the closing php tag and the end of the option open tag. So this part has to have a space:
?> >3</option>
there has to be a space between the ?> and >
The answer of Daan works as well but I just wanted to add some additional information.
You need to add a selected to one of your option tags, most easy is with a ternary operator:
<option value="1" <?php echo ($t['myvalfromdb'] == 1) ? 'selected' : ''; ?> >1</option>
<option value="2" <?php echo ($t['myvalfromdb'] == 2) ? 'selected' : ''; ?> >1</option>
<option value="3" <?php echo ($t['myvalfromdb'] == 3) ? 'selected' : ''; ?> >1</option>
my goal is get current value from database, selected in the dropdown list.
I tried that code but it shows always "Ndone" in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option selected="selected" value=""></option>
<option selected="selected" value="DONE">DONE</option>
<option selected="selected" value="NDONE">NDONE</option>
</select>
Also this one and nothing just the first row that selected in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option value=""></option>
<option value="DONE">DONE</option>
<option value="NDONE">NDONE</option>
</select>
I don't know what to do, any help please
When i use a textfield it works, so there's no problem with the variable $work
<input type="text" name="work" value="<?php echo $work; ?>"/>
<select> does not have a "value" Attribute. You must add the selected attribute to one option.
<?php
$options = array(
'',
'DONE',
'NDONE',
);
?>
<select name="work" id="work">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>"<?php echo $option === $work ? ' selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>
As per your example you could do this:
You just need to check each option value against the $work variable and if it matches add the selected attribute.
<select name="work" id="work">
<option value=""></option>
<option value="DONE" <?php echo($work == 'DONE'?'selected="selected"':''); ?>>DONE</option>
<option value="NDONE" <?php echo($work == 'NDONE'?'selected="selected"':''); ?>>NDONE</option>
</select>
This is a simple one, i know it is i just cant think of a good logical way to do this.
I have the following code:
<select name="Title[]" class="form-control">
<option value="">Select title...</option>
<option value="Mr">Mr</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Prof">Prof</option>
<option value="Doctor">Doctor</option>
</select>
What i want to do is on my edit client page, have their previously selected option displayed. So for example, if Doctor was selected at sign up, it would be selected by default on the edit page. I know i could do this like:
<option value="Doctor" <?php if($client_title == 'Doctor'){ echo 'selected'; } ?>>Doctor</option>
But it seems like a bit of a redundant way to do it. Can this be done easily with a do while statement?
Sorry for the simple request, having a bit of a slow day today! haha
<select name="title[]" class="form-control">
<?php
$titles = array('Mr', 'Miss', 'Mrs', 'Ms', 'Prof', 'Doctor');
foreach ($titles as $title) {
$selected = $client_title == $title ? ' selected="selected"' : null;
?>
<option value="<?php echo $title; ?>"<?php echo $selected; ?>><?php echo $title; ?></option>
<?php
}
?>
</select>