I have drop down list in my EditDevice view file which I have loaded values from the database.
<select name="TrackerType" name="Type" id="Type" style="width:68% !important;">
<option <?php echo ($devicearray['type']=='mobile'?'selected="selected"':''); ?>>mobile</option>
<option <?php echo ($devicearray['type']=='third party'?'selected="selected"':''); ?>>third party</option>
<option <?php echo ($devicearray['type']=='other'?'selected="selected"':''); ?>>other</option>
Now I want to retrieve the selected value in my controller file.
$NewDeviceArray["Type"] = $this->input->post("Type");
But I don't get the selected value when I print the Type.
var_dump($NewDeviceArray["Type"]);
So, my question is how to get the selected value from drop down to a variable in controller.
try this
<select name="Type" id="Type" style="width:68% !important;">
<option value="mobile" <?php echo ($devicearray['type']=='mobile'?'selected="selected"':''); ?>>mobile</option>
<option value="third party" <?php echo ($devicearray['type']=='third party'?'selected="selected"':''); ?>>third party</option>
<option value="other" <?php echo ($devicearray['type']=='other'?'selected="selected"':''); ?>">other</option>
</select>
Related
I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>
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 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>
my code is written below
<select name="year" required value="<?php echo htmlspecialchars($_POST['year']); ?>" >
<option>----SELECT----</option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3<sup>rd</sup></option>
<option value="4">4<sup>th</sup></option>
</select>
htmlspecialchars() is not working for select tag.is there any mistake or there is some other way to do it
if you want to set the default value of a select you have to set the option that contains this value as selected like this:
<select name="year" required="required">
<option>----SELECT----</option>
<option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
<option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
<option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
<option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
</select>
You don't assign a value attribute to a select tag; you assign it to option tags. It's not clear what you are trying to do, but <select ... value="..."> is just invalid HTML. See the specification.
you cannnot give value in <select> tag. Value is given in <option> tag.
like
<select name="year">
<option>----SELECT----</option>
<option value="<?php echo htmlspecialchars($_POST['year']); ?>">
<?php echo htmlspecialchars($_POST['year']); ?></option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3</sup>rd<sup></b></option>
<option value="4">4<sup>rth</sup></option>
</select>
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>