PHP: selected not being written out to HTML - php

I have the following
<?php echo $d[17];?>
<select class="form-control" name="text_06" id="text_06_<?=$this->instanceID?>" class="form-control">
<option value="--" <?php echo ($d[17]=='--' ? 'selected':'');?>>-</option>
<option value="Full" <?php echo ($d[17] == 'Full' ? 'selected':'');?>>The full review period</option>
<option value="Half" <?php echo ($d[17] == 'Half' ? 'selected':'');?>>50% of the review period</option>
<option value="None" <?php echo ($d[17] == 'None' ? 'selected':'');?>>None of the above</option>
</select>
It outputs "Full" as expected but the option is not being selected i.e. the selected text is not being written. If I move the ternary operator to anywhere outside the select menu, even to the text of that option, it displays "selected"
I have also tried using selected="selected" to no avail
Any idea? Chrome v70 being used. var_dump($d[17]) of the variable gives:
string(4) "Full"

Related

PHP: How to display select options if no value from database

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>

select multiple options at a time

<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.

Selected on an option not working

I'm loading the selected via a PHP shorthand if statement. Inspecting the options shows the correct item has the selected attribute but it's not actually selected. I've also used selected="selected" but it didn't work either. I even tried hardcoding the attribute and it still didn't work:
<select id="popUpType" name="popUpType" class="widefat">
<option value="none" <?php echo ($popUpType == 'none') ? "selected" : ""; ?>>None</option>
<option value="entry" <?php echo ($popUpType == 'entry') ? "selected" : ""; ?>>Entry</option>
<option value="exit" <?php echo ($popUpType == 'exit') ? "selected" : ""; ?>>Exit</option>
<option value="event-based" <?php echo ($popUpType == 'event-based') ? "selected" : ""; ?>>Event-Based</option>
<option value="timed" <?php echo ($popUpType == 'timed') ? "selected" : ""; ?>>Timed</option>
</select>
I'm building this in WordPress and testing in the latest version of Chrome. I've done this before and never had an issue.
This is the output after a selection is made and submitted:
<select id="popUpType" name="popUpType" class="widefat">
<option value="none">None</option>
<option value="entry" selected>Entry</option>
<option value="exit">Exit</option>
<option value="event-based">Event-Based</option>
<option value="timed">Timed</option>
</select>
I run your html and it gives right output. Please check css and javascript inherited. Please see the link -
[https://jsfiddle.net/4yoy3L0v/][1]

Show current database record

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>

PHP - selected HTML form value based on PDO value

I have an HTML form and I have a select menu. I would like that according to the value chosen by the user this would be the same value that is selected on HTML form. For example if the user chooses HIDE - the value in the select will be HIDE, if the user chooses SHOW - the value would be SHOW.
I have managed to change the value in the SQL table using PDO but I haven't managed to display the selected option based on the SQL table. Regardless of what is being saved in the table - the value being displayed in the HTML form is always show.
<select id="address_privacy" name="address_privacy" tabindex="auto">
<option "<?php if($result['address_privacy'] == 'SHOW') { echo 'selected="selected"';} ?>" value="SHOW">Show Physical Location</option>
<option "<?php if($result['address_privacy'] == 'HIDE') { echo 'selected="selected"';} ?>" value="HIDE">Hide Physical Location</option>
</select>
The above is waht I tried till now.
Try this
<select id="address_privacy" name="address_privacy" tabindex="auto">
<option <?php echo $result['address_privacy'] == 'SHOW' ? 'selected="selected"' : ''?>
value="SHOW">Show Physical Location
</option>
<option <?php echo $result['address_privacy'] == 'HIDE' ? 'selected="selected"' : '' ?>
value="HIDE">Hide Physical Location
</option>
</select>
You could do something like this:
<select id="address_privacy" name="address_privacy" tabindex="auto">
<?php if($result['address_privacy'] == 'SHOW'):?>
<option <?php { echo 'selected="selected"';} ?> value="SHOW">Show Physical Location</option>
<?php else : ?>
<option { echo 'selected="selected"';} ?> value="HIDE">Hide Physical Location</option>
<?php endif;?>
</select>
Regardless of HOW you accomplish this, you need to use == for the comparison, not =. And make sure you remove the " around the <?php ?> sections of your code.

Categories