Drop down menu load with specific value selected - php

I have a form which allows a user to edit a record. One of the items in the form is a drop down menu populated with values from a database. I was wondering if there were a way to set the default value of the drop down to the value held in the database for that record instead of automatically just going to the first item in the list (I am hoping this makes sense).
<td><label for ="genre">Image Genre: </label></td>
<td><select name="genre">
<?php echo'<option value="'. $row['genreID'] .'">'. $row['genreName'] .'</option>'; ?>
<?php while($genre_item = mysqli_fetch_array($genre_sql)){
echo'<option value="'. $genre_item['id'] .'">'. $genre_item['genreName'] .'</option>';
}
?>
</select></td>
As you can see I have put an option in to populate the first value with the value held in the database. The problem with this is that the genreName then appears twice.
Are there any solutions to this?

<td><label for ="genre">Image Genre: </label></td>
<td><select name="genre">
<?php $genre_item = mysqli_fetch_array($genre_sql); ?>
<?php foreach($genre_item as $item){
echo'<option value="'. $item['id'] .'">'. $item['genreName'] .'</option>';
}
?>
</select>
</td>
if i understood you right ....

Bearing in mind this question is more than two years old, it doesn't have an accepted answer...
Use the HTML tag <option> attribute selected:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the element is the descendant of a element whose multiple attribute is not set, only one single of this element may have the selected attribute.1
Integrating this with the PHP code:
<?php while($genre_item = mysqli_fetch_array($genre_sql)){
$selected = '';
//modify this condition per your needs -
if ($genre_item['id'] == $row['genreID') {
$selected = 'selected';
}
echo'<option value="'. $genre_item['id'] .'" '.$selected.'>'. $genre_item['genreName'] .'</option>';
When the selected option is found, the output should appear like below:
<label for ="genre">Image Genre: </label><br />
<select name="genre">
<option value="ind">Indie</option>
<option value="rock" selected>Rock</option>
<option value="srock">Soft Rock</option>
<option value="tech">Techno</option>
</select>
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

Related

how to display all user selected "options" in a list

I need the user to select one, two, three or four items from a drop down list and then display them in the "ul". I have the number of the selected item displayed and I need the value to be displayed.
my code
<select name="courses" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
</select>
<?php
if(!isset($_POST["courses"])) {
echo 'error';
} else {
echo '<ul><li>' . implode('</li><li>', $_POST["courses"]) . '</li></ul>';
}...
do not judge strictly, I am new to php thanks in advance
Change name="courses" attribute to name="courses[]", then it will be passed to $_POST and treated as an array, not as single value string.
<select name="courses[]" size="4" multiple>
<option>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
<option>Volvo</option>
Making the name attribute "courses" an array would solve the problem

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

php-Not getting values while i am selecting values from drop down?

I am trying to get a values from the drop down displayed from table .
i have tried in diff ways but i don't know where i have gone wrong.
Can any one help me on this..
Below is my code.
<tr class="form-field" id="appid">
<div>
<th valign="top" scope="row" >
<label for="country"><?php _e('country', 'custom_table_example')?></label>
</th>
<td>
<select id="country" name="country" class="code" >;
<option value="">select country</option>
<?php
global $wpdb;
$coun_name = $wpdb->get_col("select country_name FROM countries") ;
//print_r($coun_name);
foreach($coun_name as $a)
{
echo '<option value="'. strtolower($a) .'" />' . "$a </option>";
}
?>
</td>
</div>
</tr>
The above code is displaying values into drop down.
now the problem is i need to get the selected values.
echo '<option value="'. strtolower($a) .'"<?php echo $item['country']==".$a."?'selected="selected"':'' ?> />' . "$a </option>";
$item is the variable where i am storing all data.
country =name attribute.
Have you set $item from $_POST? Are you using POST as your form action? Do something like this:
<form name='countryTest' method='POST' action='<?/*where your action is going to*/?>'>
<select name='country'>
<?foreach($coun_name as $c){
?><option value='<?echo$c;?>'<?if($_POST['country']==$c)echo' selected="selected"';?><?
}?>
</select>
</form>
OR! If you want to be really cool (yea ok not so cool but nerdy)! Make a function or class function to do all this for you!
class formHelper{
public function select_form($name,$options=array([0]=>'Please select'),$selected=array(),$multiple=false){//name of select, options, selected options, multiple select
if(!is_array($selected))$selected=array($selected);
$sel='<select name="'.(($multiple===true)?$name.'[]':$name).'"';
if($multiple===true)$sel.=' multiple';
$sel.='>';
foreach($options as $value=>$shown){
$sel.='<option value="'.$value.'" '.((in_array($value,$selected))?'selected="selected"':'').'>'.$shown.'</option>';
}
return$sel.='</select>';
}
}
Now to use it just do this
$coun_name=array(merge(array('Please select a country'),$coun_name));
formHelper::select_form('country',$coun_name,$_POST['country']);
EDIT
your error is you've set your value to lower but when you're comparing it's not lowered. See strtolower. What you want to do is compare both as lower as $item will be lower. I'd recommend using an integer when comparing like this:
array(
[1]=>'England',
[2]=>'Wales',
[3]=>'Scotland'
);
So that your values will be
<option value='1'>England</option>
<option value='2'>Wales</option>
<option value='3'>Scotland</option>
But ye your issue is $item['country']==$a. Needs to be $item['country']==strtolower($a). And remove the string quotes with the full stops. "england" does not equal ".England.". The reason it's "england" already is because you've already set the string to lower. Unless $item is not $_POST['county']'

Select list array - prepopulate the choice to the top of the array

I am printing a simple select list out by looping through an associative array which is populated elsewhere.
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This select list gives the options available from the array. However, I am trying to pre-populate the list with the selected value, for example if a user is updating their user information, then I would like to set that choice at the top of the list (ideally without the choice appearing further down the list).
I have access to this data in a variable which I have queried:
$this->selected_choice //value
$this->selected_id //key
Now I have been testing some solutions
<select id="choice" name="choice">
<option name="<?php echo $this->selected_choice; ?>" value="<?php echo $this->selected_id;?>">"<?php echo $this->selected_choice; ?></option>
<?php
foreach ($this->choice as $key => $value) {
echo "<option name=".$value." value=".$key.">" .$value. "</option>" ;
}
?>
</select>
This solution does display the users choice on top of the list, but the foreach loop will also display this option as there is no indication that it shouldn't.
This is my question to you guys, how can I do this so the the selected_choice appears at the top of the list, but will then not appear in the foreach array.
I am hoping there is some simple solution to this problem, as it is not so important, but something I come across quite a lot.
Option have no "name". Name is attribute for SELECT tag.
If option is selected, you need to pass "selected" attribute in body of option
Ex: <option value="1" selected>Tramtada</option>
Do it this way:
<select id="choice" name="choice">
<?php
foreach ($this->choice as $key => $value) {
echo "<option <?php if($key == $this->selected_id) {?> selected <?php } ?> value=".$key.">" .$value. "</option>" ;
}
?>
</select>

Why Can't I Select Dynamically (PHP) Generated Options?

I am having an issue with a table where each cell contains an id'd input or select field. The cell contents are dynamically generated from a MySQL query. Everything works fine except for my dynamically generated SELECT drop downs. I am using the following code in a switch statement:
case 'crew_1':
case 'crew_2':
echo '<td><select class="'.classExt($types[$key]).' '. $key .'_c" id="' . $key . '--' . $idx .'">';
echo '<option value="" selected="selected"></option>';
while ($rowtech = mysqli_fetch_assoc($rtech)){
echo '<option value="' . $rowtech['name'] . '">' . $rowtech['name'] . '</option>';
} mysqli_data_seek($rtech,0);
echo '<option value="TEST">TEST</option>';
echo '</select></td>'; break;
This creates the following HTML:
<td>
<select class="varchar crew_1_c" id="crew_1--1">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
</td>
From this I can select the blank or 'TEST' option and it returns the correct value in a console.log, but if I choose one of the dynamically generated names, the box returns back to the default blank value. I get the same responses on Chrome and IE.
Where is my disconnect?
Included for the person requesting the script generating to output to console:
$('tbody select, tbody input').change(function(){
console.log($(this).val());
});
OP, you're missing the name attribute.
This works fine for me:
<form method="post" action="">
<select class="varchar crew_1_c" id="crew_1:1" name="choice">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
<input type="submit" value="Submit" name="submit" />
</form>
<?php
var_dump($_REQUEST);
?>
Found my issue. The JS request earlier had me dig back through my code to discover that I was setting my values toUpperCase() for submission for consistency, but that was breaking my value match on the drop down. I set my values to uppercase in the select options also and TA-DA!
Thanks for the help, guys. Sometimes it takes a fresh pair of eyes to point out the obvious.

Categories