Auto select value of dropdown - php

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

Related

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]

Change value of jQuery mobile flip switch using php

Does anyone know how to set the value of a jQuery mobile flip switch using just php? I have tried the below without any luck. Any ideas would be appreciated!
<?php
ifFilled = '1';
?>
<select name="flip-min" id="flip-min" data-role="slider">
<option value="1" <?php echo $isFilled == '1' ? ' selected="selected"' : '';?>>Filled</option>
<option value="0" <?php echo $isFilled == '0' ? ' selected="selected"' : '';?>>Un-Filled</option>
</select>
Do it with JavaScript:
Look at this fiddle first.
<label for="flip-a">Select slider:</label>
<select name="slider" id="flipMe" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<?php if ($ifFilled == true): ?>
var fts = $('#flipMe');
fts.val('on');
fts.slider('refresh');
<?php endif; ?>
opps $ifFilled does not match $isFilled lol dummy of the year award over here!!! the code works fine other than that....

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.

Dynamically mark as selected an option in a form using HTML and PHP

I am creating a form to update some entries in a sql table.
For one of the fields, I will have a Select Option that must be "selected" in the value that the field contains.
So ideally, I would have something like this:
<select id="source" name="source">
<option <?=$manual?>>MANUAL</option>
<option <?=$etsy?>>ETSY</option>
<option <?=$online?>>ONLINE</option>
</select>
But I want to make it dynamic so, if I have 100 options, I won't have to write 100 variables, but just load the corresponding variable name with the word "selected".
Whats the best way to achieve this?
Try something like this:
<select id="source" name="source">
<?php
$Attrib = 'selected="';
if ($order['source'] === 'value'){
$Attrib.= 'selected"';
}
else{
$Attrib .= 'false"';
}
?>
<option name="" <?=$Attrib;?>>NAMEHERE</option>
Not to mention, you are assigning ($order['source']) to value, so this will always equal to "value"
May be you meant to do something like
<option <?= ($order['source']==='optionname')?'selected':'' ?> > optionname </option>
And it would be nicer to push the options to some array.
Oh, that's what u mean.
That's how i would have done it
<?php
$checkedoption = 'flytothemoon'; //the checked option
$options = array('opt1', 'opt2', 'opt3', 'flytothemoon');
foreach($options as $option):?>
<option <?= ($order['source']===$checkedoption)?'selected':'' ?> > $option </option>
<?php endforeach;?>

passing parameters to html select from php

I would like to pass php parameters to an html select options tag. it works fine for an input tag using the value attribute as follows:
<input type="text" name="to" value= "<?php echo $parameter ?>" size=10>
I tried to do the same for the select tag, but some reason its not working. Below is the select tag:
<select name="clinic">
<option value="<?php echo $clinicName; ?>" >Baylor</option>
<option value="<?php echo $clinicName; ?>" >IDCC</option>
</select>
Please note that the parameter would need to passed to an sql statement below these above statements as follows:
$clinicName = $_POST['clinic'];
$sql = "select * from patients where clinic = '$clinicName'";
Should do the trick
<select name="clinic">
<option <?php echo ($clinicName=="Baylor" ? "selected" : ""; ?> value="Baylor">Baylor</option>
<option <?php echo ($clinicName=="IDCC" ? "selected" : ""; ?> value="IDCC">IDCC</option>
</select>
Also, as someone above me mentioned in the comments, you need to read up on SQLi otherwise your site is just one more to the list of already-hacked.
Are you trying to set the current selected value on the <select>? If so, you need to use if statements to check which one to mark as selected:
<option value="Baylor"<?php if($clinicName == 'Baylor') echo ' "selected"'?>>Baylor</option>
<option value="IDCC"<?php if($clinicName == 'IDCC') echo ' "selected"'?>>IDCC</option>
Instead of the if, you could also use the ternary operator:
<option value="Baylor"<?php echo $clinicName == 'Baylor' ? ' "selected"' : ''?>>Baylor</option>
<option value="IDCC"<?php echo $clinicName == 'IDCC') ? ' "selected"' : ''?>>IDCC</option>
First of all, please sanitize your SQL query. Right now it is more exploitable than Lindsay Lohan. If you use mysql_* functions, then write it as:
$sql = "select * from patients where clinic = '".mysql_real_escape_string($clinicName)."'";
Secondly, in your HTML the $clinicName is the same on both cases. So the $_POST['clinic'] will always be the same no matter what. If you want to pre-select a value, then you need to write it as:
<select name="clinic">
<option value="Baylor" <?php if($clinicName=='Baylor'){ echo 'selected'; } ?>>Baylor</option>
<option value="IDCC" <?php if($clinicName=='IDCC'){ echo 'selected'; } ?>>IDCC</option>
</select>
Other than that, I remain a bit confused about your question.
Copy this in your HTML tag
<select name="clinic">
<option value="baylor" >Baylor</option>
<option value="IDCC" >IDCC</option></select>
And this before your HTML tag
<?php
echo $clinicName = $_POST['clinic'];
?>

Categories