I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.
This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.
Example :
My database value is Pending. Then it will show Pending inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :
this is what I am looking for. And below is the code that I tried.
<div class="floatleft">
<select name="select2" class="select-form-standard" >
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
</div>
But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.
Add selected attribute for the option which must be selected. Assume $value = 0
<?php $value = 0; ?>
<select name="select2" class="select-form-standard" >
<?php foreach($options as $id => $option) { ?>
<option value="<?php echo $id ?>"<?php
if($id == $value) {
echo " selected";
} ?>><?php echo $option ?></option>
<?php } ?>
</select>
Try this:
$statusval is variable coming from your database
<option value="0" id="0" <?php if($statusval==0){echo "selected;"}?> name="status">Deleted</option>
<option value="1" id="1" <?php if($statusval==1){echo "selected;"}?> name="status">Active</option>
<option value="2" id="2" <?php if($statusval==2){echo "selected;"}?> name="status">Pending</option>
<option value="2" id="2" <?php if($statusval==3){echo "selected;"}?> name="status">Suspended</option>
<?php
$databaseValue = $your_database_value;
?>
<select name="select2" class="select-form-standard" >
<option value="0" <?php echo (($databaseValue==0)?"selected":"") ?> id="0">Deleted</option>
<option value="1" <?php echo (($databaseValue==1)?"selected":"") ?> id="1">Active</option>
<option value="2" <?php echo (($databaseValue==2)?"selected":"") ?> id="2">Pending</option>
<option value="3" <?php echo (($databaseValue==3)?"selected":"") ?> id="3">Suspended</option>
</select>
Try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name="status">
<option value style='display:none;'>-SELECT Status--</option>
$select_data=mysql_query("Select status,options from yourtable");
while($result=mysql_fetch_array($select_data)){
echo "<option value ='YourValue' ".selectBoxSelection('YourOptionValue', $result['status']).">".$resule['option']."</option>";
}
</select>
<select name="select2" class="select-form-standard" >
<option><?php echo $select2e; ?></option>
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
You need to add the attribute "selected" to the option you wish to have preselected:
<option value="2" id="2" name="status" selected>Pending</option>
or if you are using xhtml you will need to use
selected="selected"
To have this work off a value in database you will need to write an if statement to determine if the database value is the same as the option value, and if it is, then echo out the "selected" attribute within the option tag.
you can easily do it by a simple if statement. inside your loop try this
<option value="<?= $row['id']) ?> " <?= $row['id']==$categoryIDfromDatabase?'selected':''; ?> >
<?= $row['categoryName'] ?>
</option>
Related
I'm trying to retrieve data from a SQL DB when the user types a category ID in to search with. It should then autofill (populate) the html form data with the retrieved data from the row's columns (upon pressing the submit button).
Everything is working properly, but I don't know how to change a dropdown menu
For example:
<input name="prodname" type="text" value="<?php echo $pName; ?>"/>
Fills the form value successfully after I press the search(submit) button.
This following code does not change the drop down values at all (<select> vs <input>)
<select name="supplierID" value="<?php $suppID; ?>">
How can I change the drop down values of a while only using HTML/PHP?
Thank you.
To select a option in <select> tag you need to set selected attribute to that option. The following code may help you to understand.
<select name="supplierID">
<option value="1" <?php if($suppID == 1) echo 'selected'; ?> >1</option>
<option value="2" <?php if($suppID == 2) echo 'selected'; ?> >2</option>
<option value="3" <?php if($suppID == 3) echo 'selected'; ?> >3</option>
<option value="4" <?php if($suppID == 4) echo 'selected'; ?> >4</option>
</select>
Hope this helps.
To select the values in drop down, the selected attribute is required. You may check with the code below:
<select name="supplierID">
<option value="1" <?php if($id == 1) echo "selected"; ?>> A </option>
<option value="2" <?php if($id == 2) echo "selected"; ?>> B </option>
<option value="3" <?php if($id == 3) echo "selected"; ?>> C </option>
</select>
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">
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>
I have made some code to create a dropdown in a webpage and you can select among 2 currency values namely USD and SGD. I have been able to get the value for the currency field while entering the data in the database. But while trying to edit the entry in the database, I am able to use $_POST to get all entries to display except the value of currency. I would ideally want to display the previously selected value on the dropdown. As of now the drop down on the edit page just displays the default "Please Choose" and doesn't show the previously selected value. any help would be greatly appreciated.
Code :
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled selected style='display:none;'>Please Choose</option>
<option value="SGD">SGD</option>
<option value="USD">USD</option>
</select>
And I am trying to somehow display the previously read value that exists in the database and display that instead of the "Please Choose" so that while editing I don't have to re-select the currency value.
Supposing you stored in $currency the value from DB:
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled style='display:none;'>Please Choose</option>
<option value="SGD"<?php echo $currency == "SGD" ? " selected" : ""; ?>>SGD</option>
<option value="USD"<?php echo $currency == "USD" ? " selected" : ""; ?>>USD</option>
</select>
Use PHP to Solve it..
<select name="select_limitby" onChange="frm_sub()">
<?php if($_SESSION[select_limitby]!='') { ?>
<option value="<?php echo $_SESSION[select_limitby]; ?>" <?php if($_POST[select_limitby]=='$_SESSION[select_limitby]') {?> selected="selected" <?php }?>><?php echo $_SESSION[select_limitby]; ?></option>
<?php } ?>
<option value="">Default</option>
<option value="9" <?php if($_POST[select_limitby]=='9') {?> selected="selected" <?php }?>>9</option>
<option value="12" <?php if($_POST[select_limitby]=='12') {?> selected="selected" <?php }?>>12</option>
<option value="15" <?php if($_POST[select_limitby]=='15') {?> selected="selected" <?php }?> >15</option>
</select>
USE session if it does not work...
otherwise you can leave session...
I'm using cakephp 1.2, and I have a search form which has also this menu:
Classificazione <select style="margin-top: 5px;" name="classificazione">
<option value="art0"></option>
<option value="C">Articoli</option>
<option value="D">Documentazione</option>
<option value="A">Libri</option>
<option value="G">Materiali</option>
<option value="B">Riviste</option>
<default value="A">
</select><br />
In the next page I want to set the default value of this menu with what the user has chosen before.
I SOLVED like this (for example, with the first option):
In the controller:
$getParams['classificazione'] = isset($params['classificazione']) ? $params['classificazione'] : '';
...
$this->set('getParams', $getParams);
In the view:
<option value="C" <?php if ($getParams['classificazione']=="C") echo "selected"; ?> >Articoli</option>
Save the value in a session variable and use that to echo selected for that option
<?php
function is_selected($selected_option, $list_option_value) {
if($selected_option == $list_option_value) {
return 'selected';
}
}
?>
<select>
<option <?php echo is_selected($_SESSION['selected_option'], '1'); ?>>1</option>
</select>