select combo box based on get value - php

I'm using search with combo box
Here is my combo box source code
<select name="salary" class="styled">
<option selected="selected" value=''>Any</option>
<option value="10000">10,000</option>
<option value="20000">20,000</option>
<option value="30000">30,000</option>
<option value="40000">40,000</option>
<option value="50000">50,000</option>
<option value="60000">60,000</option>
<option value="70000">70,000</option>
<option value="80000">80,000</option>
<option value="90000">90,000</option>
<option value="100000">100,000</option>
<option value="110000">110,000</option>
<option value="120000">120,000</option>
<option value="130000">130,000</option>
<option value="140000">140,000</option>
<option value="150000">150,000</option>
</select>
I would like to select value based on $salary=$_GET['salary']; if $salary empty I need to select first one as selected

To set a default value for an HTML select element, you need to give the appropriate <option> element the selected attribute. It would look like this:
<option value='50000' selected='selected'>50,000</option>
In your PHP code, you would add this by setting a string for each option, to either "selected='selected'" or blank, depending on whether that option is the one you want to have selected.
This is obviously far easier to put into a loop rather than writing the same code twenty times, so you would need to rewrite your output to create a loop for creating your options.
Hope that helps.

You can do something like this on creation...[not tested]
<?php
$options = array(
'10000' => '10,000',
...
'150000' => '150,000'
); //From MySQL
array_unshift($options, '' => 'Any');
$salary = isset($_GET['salary'])?$_GET['salary']:'';
?>
<select name="salary" class="styled">
<?php foreach ($options as $value=>$text){ ?>
<option <?php if($value == $salary){echo 'selected="selected"'; }?> value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php } ?>
</select>

If I understand your question correctly you want that if the person doesn't select any thing you want 10000 to be automatically selected for him. And I also assume that the options in the select list are static. So..
<?php
if($_GET['salary']=='')
$salary = 10000;
else
$salary = $_GET['salary'];
?>

Related

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

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

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;?>

HTML define what selection option is active

I have a select box with the following (it has been shortened):
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="Germany">Germany</option>
</select>
If the user logs into the control panel and wants to change his country, he gets presented with this form. My problem is that everytime USA is the default and I can not change this depending on the users country. For example, the user lives in Australia. He wants to change his country to USA and goes to this page. I want the country that is displayed to be Australia. Let me know if this makes sense.
To preselect a value, just add the selected attribute to the desired option.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
This will preselect Australia for example.
You need to add a selected attribute to the option you want to select, as described in the spec.
<select id="viewSelector" name="when"">
<option value="USA">USA</option>
<option value="Australia" selected="selected">Australia</option>
<option value="Germany">Germany</option>
</select>
In your script, you need to emit this attribute for whatever default you want to display.
Use the selected attribute of the option tag.
<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option selected value="Germany">Germany</option>
</select>
Use selected on the <option> tag that names the user's current country:
<option value="Australia" selected="selected">Australia</option>
So in PHP:
<select>
<?php
$countries = array('USA', 'Australia', 'Germany');
$current_country = 'Australia';
foreach($countries as $country) {
if($country == $current_country) {
echo '<option selected="selected">'.$country.'</option>';
} else {
echo '<option>'.$country.'</option>';
}
}
?>
</select>
The html looks like:
<option selected value="Australia">Australia</option>
In the php loop that builds this html, compare the current option name with the user's current country (retrieved from a database or where-ever). When the two match, add selected
A bit shorter:
foreach($paises as $key=>$value)
{
if($key == "PE"){$activo = ' selected="selected" '; }else{$activo = "";}
echo "<option $activo value=\"$key\">$value</option>";
}

Categories