Selected on an option not working - php

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]

Related

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"

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.

Select field drop down select state based on mysql field and php variable status

I'm creating a user form that requires the functionality for people to come back and finish the form at different times, this requires it to save its content intermittently. The issue is that if a user submits some information, come backs and submits some more, it erases the previous content. To combat this and improve usability I have it set up for the form to call back the content from the database and insert it into the form before the user edits the content. This works great for textareas but not so well with dropdowns etc. I've looked at other code that allows you to give a selected state to content that matches within the database. HOWEVER, unlike all the other examples that have the dropdown content in the database, mine is hard-coded into the site like so:
<div class="block-a">
<label>Likelihood</label>
<select class="large-input" name="questions['.$questions['id'].'][1]">
<option></option>
<option value="1">Negligible</option>
<option value="2">Low</option>
<option value="3">Medium</option>
<option value="4">High</option>
<option value="5">Very High</option>
<option value="N/A">Not Applicable</option>
</select>
</div>
So say at 1pm a user set the likelihood to "4" and then comes back at 3pm all it does now is go back to blank, I want it to communicate with the database and see that "4" is in the database and then set "High" to the selected state.
DOES anyone know how to do this? I've been trying to no success. Cheers
Just to answer your question - assuming the list above is hard-coded and you want to stick to that ...
<option <?php echo (isset($var_which_holds_value) && $var_which_holds_value == '') ? 'selected="selected"' : ''); ?>></option>
<option value="1" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 1) ? 'selected="selected"' : ''); ?>>Negligible</option>
<option value="2" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 2) ? 'selected="selected"' : ''); ?>>Low</option>
<option value="3" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 3) ? 'selected="selected"' : ''); ?>>Medium</option>
<option value="4" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 4) ? 'selected="selected"' : ''); ?>>High</option>
<option value="5" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == <?php echo (isset($var_which_holds_value) && $var_which_holds_value == '') ? 'selected="selected"' : ''); ?>) ? 'selected="selected"' : ''); ?>>Very High</option>
<option value="N/A" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 'N/A') ? 'selected="selected"' : ''); ?>>Not Applicable</option>
however there are better ways (in terms of code readability and management) to achieve that.

Show the option of the drop down list that is equal on the variable on the url

I have the following drop down list. How can I show the correct option based on the url e.g. www.domain.com/?car=Algema&city=Paris
What I did is add a value="echo $_REQUEST['car'];" because I used the same on my textboxes but it is not working.
I am using PHP.
<select name="car" value="echo $_REQUEST['car'];" id="car" class="select">
<option value="Algema">Algema</option>
<option value="Barkas">Barkas</option>
<option value="Cadillac">Cadillac</option>
</select>
<select name="car" id="car" class="select">
<option value="Algema" <?php echo $_REQUEST['car'] == 'Algema' ? 'selected="selected"' : '';?>>Algema</option>
<option value="Barkas" <?php echo $_REQUEST['car'] == 'Barkas' ? 'selected="selected"' : '';?>>Barkas</option>
<option value="Cadillac" <?php echo $_REQUEST['car'] == 'Cadillac' ? 'selected="selected"' : '';?>>Cadillac</option>
</select>
I can't answer with PHP specific code but you need to set the selected attribute of the option you would like to select to selected.
<select name="car" id="car" class="select">
<option value="Algema" <?=($_GET['car'] == 'Algema') ? 'selected="selected"' : NULL?>>Algema</option>
<option value="Barkas" <?=($_GET['car'] == 'Barkas') ? 'selected="selected"' : NULL?>>Barkas</option>
<option value="Cadillac" <?=($_GET['car'] == 'Cadillac') ? 'selected="selected"' : NULL?>>Cadillac</option>
</select>

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