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...
Related
I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>
I am fetching selected value on select, values are stored in session. I am now fetching by echoing variable name $gender but it's not displaying selected value which I selected earlier.
My code
<select value="<?php echo $gender; ?>">
<option>Male</option>
<option>Female</option>
</select>
Can you guyz tell me, what's wrong I am doing here
<select> doesn't work like that. It uses the selected attribute in the <option>:
<select name="gender">
<option value="male" <?php echo $gender=="male"?"selected":""; ?>>Male</option>
<option value="female" <?php echo $gender=="female"?"selected":""; ?>>Female</option>
</select>
I have a drop-down in my manageDevices View.
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile">Mobile</option>
<option value="Device">Device</option>
<option value="Other">Other</option>
</select>
I want to save the value of the drop-down in the database in my addDevice view and load the editDevice view with the values retrieved from database.
I was able to save the value of the drop-down to the database.
Now I want to fetch the stored value to the editDevice page and show the selected value in the drop-down. I can get the name of the selected value using,
<?php echo $devicearray['type'] ?>
I want to show this value as the "selected value" in the drop-down.
I am using codeigniter framework to develop this. Any hint will be highly appreciated
At your view file for each option:
<option value="Device"<?php echo ($devicearray['type']=='Device'?'selected="selected"':''); ?>>Device</option>
It would be easier to store type as an integer, but for 3 values not big difference
you can try this
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile" <?php echo ($devicearray['type']=='Mobile') ? "selected" : ""; ?>>Mobile</option>
<option value="Device" <?php echo ($devicearray['type']=='Device') ? "selected" : ""; ?>>Device</option>
<option value="Other" <?php echo ($devicearray['type']=='Other') ? "selected" : ""; ?>>Other</option>
</select>
Method:2
$$devicearray['type'] = "selected";
?>
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile" <?php echo #$Mobile; ?>>Mobile</option>
<option value="Device" <?php echo #$Device; ?>>Device</option>
<option value="Other" <?php echo #$Other; ?>>Other</option>
</select>
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>
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>