Value is not fetching on select PHP - php

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>

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 $_GET values from sql to be inserted in <select type> PHP

I'm using xampp PHP
How to do I get values from MySQL in select type like getting value in
<input type="text" value="<?php $variable['valueinsql']; ?>">
But I want the value to be inserted in
<select type>
And I tried
If(isset($_GET['id'])){
<p> Banks:</p><select multiple="multiple" name="otherbanks[]" style=" width:190;" value = "<?php $list["Banks"] ?>"">
<option value="Banksone">One</option>
<option value="Bankstwo">two</option>
<option value="Bankthree">three</option>
I want to see it automatically highlighted.
Use in_array to check the value available in array or not. If available echo 'selected';
Try this ==>
<select multiple="multiple" name="otherbanks[]" style=" width:190;">
<option <?php if (in_array('Banksone',$list["Banks"])) {echo 'selected';} ?> value="Banksone">One</option>
<option <?php if (in_array('Bankstwo',$list["Banks"])) {echo 'selected';} ?> value="Bankstwo">two</option>
<option <?php if (in_array('Bankthree',$list["Banks"])) {echo 'selected';} ?> value="Bankthree">three</option>
</select>
To highlight particular options the selected attribute should be set on each option that is applicable - as below example.
<select name='otherbanks[]' multiple=true>
<option value=1 selected>one
<option value=2>two
<option value=3 selected>three
<option value=4>four
<option value=5 selected>five
</select>
Presumably you generate the select menu using PHP and a query to the database so to accomplish that ( highlighting ) in PHP you would need a loop to iterate through each value in $list["Banks"] and check the value against the value from sql. Without knowing exactly how you generate the select menu or knowing the value of $list['Banks'] it is hard to know exactly and I may be "barking up the wrong tree" as it were.

trying to display previously selected value in dropdown list php

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...

Retrieve stored radio button value from database

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>

Retrieve stored drop-down value from database

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>

Categories