PHP - selected HTML form value based on PDO value - php

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.

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>

PHP: selected not being written out to HTML

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"

Read value from MySQL and compare which option is selected from drop-down list

<select name="title">
<selected value="<?php echo $title; ?>"><?php echo $title; ?></selected>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Mr">Mr.</option>
<option value="Dr">Dr.</option>
</select>
I am trying to read a value from a column Title in a MySQL database, which is suppose to read the value, whether it be Mr., Ms., Mrs., then compare it with the values in the drop-down list. It then lets the user select another title to then update the one stored in MySQL database.
I am creating a user profile. So when the user logs in and navigates to the view to edit a profile, he/she should be presented with a drop-down list containing the title he/she selected when registered. Then if he/she wants to they can change the title in the drop-down list and press the update button and it should now update to the new title in the database.
Change your <select> dropdown list in the following way,
<select name="title">
<option value="Mrs"<?php if($title == "Mrs"){ echo " selected='selected'"; } ?>>Mrs.</option>
<option value="Ms"<?php if($title == "Ms"){ echo " selected='selected'"; } ?>>Ms.</option>
<option value="Mr"<?php if($title == "Mr"){ echo " selected='selected'"; } ?>>Mr.</option>
<option value="Dr"<?php if($title == "Dr"){ echo " selected='selected'"; } ?>>Dr.</option>
</select>
There is no selected HTML tag. Use the selected attribute of the option tag:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a <select> element whose multiple attribute is not set, only one single <option> of this <select> element may have the selected attribute.1
So consider this example from the Examples section of the Mozilla Developer Network page for <select>:
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
Your example code can be updated similarly:
<select name="title">
<option value="Mrs" <?php if($title=="Mrs"){ echo "selected"; } ?>>Mrs.</option>
<option value="Ms" <?php if($title=="Ms"){ echo "selected"; } ?>>Ms.</option>
<option value="Mr" <?php if($title=="Mr"){ echo "selected"; } ?>>Mr.</option>
<option value="Dr" <?php if($title =="Dr"){ echo "selected"; } ?>>Dr.</option>
</select>
A simpler way to do this would be to process the names first, using array_reduce():
<?php
$title = 'Dr';
$names = array('Mrs','Ms','Mr','Dr');
$options = array_reduce($names,function($carry,$name) use ($title) {
return $carry .= '<option value="'.$name.'"'.($title == $name?' selected':'').'>'.$name.'.</option>';
});
?>
<select name="title">
<?php echo $options;?>
</select>
See it in action in this playground example.
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

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]

Auto select value of dropdown

How can I auto select a field in dropdown.
Say if someone goes to www.xyx/form/?abc
Some value gets selected in the dropdown,
Or if someone goes to www.xyx/form/?def
Some other value gets selected in the dropdown.
I am comfortable with JS and php.
assuming example.com/?sel=xxx
<?php
$sel = $_GET['sel'];
?>
<select ...>
<option val="xxx" <?php if($sel==='xxx') echo 'selected="selected"';?>>Option XXX</option>
<option val="yyy" <?php if($sel==='yyy') echo 'selected="selected"';?>>Option YYY</option>
</select>
No Javascript needed.
PHP
<select name="select">
<option value="abc"<?php ($_GET['select'] == 'abc'? echo 'selected="selected"' : ''); ?>>ABC</option>
<option value="def"<?php ($_GET['select'] == 'def'? echo 'selected="selected"' : ''); ?>>DEF</option>
</select>
<option value="abc" <?php echo isset($_GET['abc']) ? 'selected="selected"' : ''; ?>>abc</option>
hmmm, so what will you do when you have 100s of items in the Options list? The other ideas wont look so great then.
Then you will need to just simply write 1 line of code at the end of select tag:
<?php if(isset($_POST['env_foil_color'])) echo "<script>document.getElementById('env_foil_color').value='{$_POST['env_foil_color']}';</script>"; ?>
where, 'env_foil_color' is the select tag's ID and Name both

Categories