how to select a value from a listbox? - php

I am having a list box like this ,the list box is populated from the database
<td bgcolor="#FFFFCC">
<select name="listbox" id="FriendmailId" size="3" >
<option value="0">Select User From List</option>
<? foreach($searchfriend as $row)
{?>
<option value=""><?=$row['dEmailID'];?></option>
<? } ?>
</select>
</td>
The values are listed in the list box ....but the problem is when i select a item it is highted but not really selected why it is so

You need to add selected="selected" for the option value you want selected:
<option value="" selected="selected"><?=$row['dEmailID'];?></option>
In a loop, this is usually done when a certain condition is met for an option to be selected (of course only one option can be selected at a time)
<? foreach($searchfriend as $row)
if (condition to select a specific option value) // when true
{
{?>
<option value="" selected="selected"><?=$row['dEmailID'];?></option>
<? } else { ?>
<option value=""><?=$row['dEmailID'];?></option>
<? }} ?>
Note: If you don't specify selected="selected" for an option, by default, first option value is selected.

Related

how to make the drop down option selected with database value using php

I'm having a select option in which values are populating from the database.
During updation I need to get that value selected.How can I do that?Please help.
Here is my code for select tag
<select name="supplier_id" class="form-control form-control-sm" id="colFormLabelSm">
<option value="0" selected="selected" disabled="disabled">
Select supplier
</option>
<?php
$supp_select = "SELECT supplier_id,supplier_name FROM supplier_details";
$supp_query = mysqli_query($con, $supp_select);
while ($supp_data = mysqli_fetch_assoc($supp_query)) {
?>
<option value="<?php echo $supp_data['supplier_id'] ?>">
<?php echo $supp_data['supplier_name']; ?>
</option>
<?php
}
?>
</select>
Put selected attribute on the option which is to be selected..Using if statement in option tag, check for which value is matching according to the value passed
E.g
<option <?php if(... ) echo "selected"; ?> value="value of db">

How to set the value for dropdown box?

i want to display the value for all the textbox with the help of this code
value="<?php `if(!empty($result) && !empty($result['part'])){ echo $result['part']; } ?>">
Where part is the name of the database field...same thing i have to display for dropdown box how can i do this..In dropdown box the options are displayed with the help of another table
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ): ?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?>
</option>
<?php endforeach ?>
</select>
where accName is the name stored in another table..i want to display the name here..but it had to be stored as code(predefined integer value) in my database field...How can i did this..??
Value for drop down is set different way than that of text box or text area.
Drop down has options and one of its options (multiple in case it is a multiple dropdown) is set to be selected one.
So, <option> syntax:
<option value="value">Text</option>
If we want to make an option selected, use selected' attribute of`
<option value="value" selected="selected">Text</option>
So, in your case, solution would be:
1) Take value from database.
2) In the options loop, compare database value with current iteration <option> value.
3) If any of options' values are matching, use selected attribute of <option>
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ):
$selected = ($dbValueForDropDown == $row['id']) ? 'selected="selected"' : '';
?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?> <?php echo $selected;?>><?=$row['accName']?></option>
<?php endforeach ?>
</select>
you should bring id and name of the accounts in the same results set by join in the query you send to the database and then run on the results and put the id in the value and the name in the display
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName" required placeholder="select account name">
<option value="" disabled selected>Select Account Name</option>
<?php foreach ($accName as $row ):
$selected = '';
if($row['id'] == $result['code']){
$selected = 'selected = selected';
}
?>
<option <?php echo $selected; ?> value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?></option>
<?php endforeach ?>
</select>

Select the data that has been saved in the db in a Select dropdown [PHP]

Each row has a select dropdown, so if in the 1st row dropdown, a name has been selected and saved to the database, next time the 1st row's dropdown will show respective name based on db record.
For the 2nd, 3rd and 4th row's dropdown will show a select dropdown without any name being selected
Now i can only work out how to do it by hardcoding.
But as i said the no of student name is unknown as it can be added or deleted.
Is there anyway i can select the student name that has been saved in the db previously
<td><select class="input" name="name[]" >
<option value="select" <?php if ($row['name'] == 'select') echo
'selected="selected"'; ?> > select</option>
<option value="<?php echo $_SESSION['name'] ?>" <?php if ($row['name'] ==
$_SESSION['name']) echo 'selected="selected"'; ?> > <?php echo
$_SESSION['name'] ?> </option>
</select></td>
Your while loop is going to create a new list for each row returned. Place it inside the select tag as such
<select name="course">
<option value="0">Pick student</option>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
?>
</select>
Having multiple options selected is just going to select the first option anyway. By default, the most recent addition should be the last row in the table.

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>

dropdown list with 2 values

I need to create a dropdownlist which has two values "OK" and "NOK" one should be selected by default based of an value of $Status. The user should than be able to change the selection or let the default one. How can i manage this? What i have tried:
<select name="Status">
<option value="OK">OK selected="selected"</option>
<option value="NOK">NOK</option>
</select>
<select>
<option value="OK" <?php if($Status == "OK") echo 'selected="selected"';?>>OK</option>
<option value="NOK" <?php if($Status == "NOK") echo 'selected="selected"';?>>NOK</option>
</select>
You can add an id and/or name attribute to the select as you like.

Categories