How to set the value for dropdown box? - php

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>

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

select multiple options at a time

<select class="form-control default-select2 " id="group" data-size="10" data-live-search="true" data-style="btn-white" multiple="multiple">
<option value="" disabled="disabled" selected="selected">Select Group</option>
<!-- <option value="1">Admin</option>
<option value="4">User</option> -->
<?php $groups = $objUser->GetGroups(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php echo $group['id'] ?>"<?php echo ($data[0]['group_id'] == $group['id'] ? "selected='selected'" : "") ?>><?php echo $group['primary_name'] ?></option> <? } ?>
</select>
Now I want user to select multiple option at a time. i searched the whole internet but i couldn't find the answer to fix this issue. i dont know what i am missing.
Your code can actually manage multiple selections. Just hold Ctrl key and click on different options.
Exemple of multiple select : here
When you submit the form, you will be able to get all the selected options into an array.

Html Select Box Get value from mysql

Hi I have a stored value in mysql from a select box. When i call a edit page I would like the select box to populate with the value i have stored in mysql
$taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); This returns 1
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1">building</option>
<option value="2">maintenace</option>
</select></div>
I would like the select box to show the correct option when loading the edit page. I do not store any select option in the database values or text just the value that is select when job was created.
Thanks Jon
I'd I have understood correctly, you have he value/values in your MySQL database and you want them to be shown in your drop down list:
You could do it like:
<option value = "1" <?php if($taskCompany == 1) echo "select='selected'";?>>building</option>
Or if you have the names coming from the database too.
<select id="taskCompany" required name="taskCompany" class="form-control">
<option>SELECT</option>
<?php
$res = musql_query("SELECT * FROM yourtablename");
while($row = mysql_fetch_array($res))
{
$taskCompany = $row['taskCompany'];
$co_name = $row['taskCompanyName'];
?>
<option value = "<?php echo $taskCompany; ?>" ><?php echo co_name; ?></option>
<?php } ?>
Here I have used $co_name to be displayed as a name assuming that you have building, maintenance etc stored in your database.
This is for the time being, as I wanted to demonstrate but Please
consider mysqli/PDO as mysql_* is deprecated.
<?php $taskCompany = $mysqli->real_escape_string($_POST['taskCompany']); ?>
<select id="taskCompany" required name="taskCompany" class="form-control">
<option value="" disabled selected>Select a Company</option>
<option value="1" <?php if($taskCompany == 1) echo "selected='selected'"; ?>>building</option>
<option value="2" <?php if($taskCompany == 2) echo "selected='selected'"; ?>>maintenace</option>
</select></div>
You can do this
<?php foreach($taskCompany as $row){ ?>
<option value="unique id for select options">name for select options</option>
<?php } ?>

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

in an update, how to select an option from a select box?

Good afternoon,
Using cakephp, how can I select a value in a select box in an update operation?
in the view I get the variables like this for example:
<?php $category = $itemEditar['Anuncio']['category']; ?>
and I need to select an option from a select box:
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option value="Category1">Categoria1</option>
<option value="Category2">Categoria2</option>
<option value="Category3">Categoria2</option>
</select>
to be an update operation, I need to mark the category that was saved in the database, and am not getting how to do this.
You want to check the $category against each or the options and if they match set the selected attribute .
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option<?= $category == "Category1"?" selected = 'selected'":"" ?> value="Category1">Categoria1</option>
<option<?= $category == "Category2"?" selected = 'selected'":"" ?> value="Category2">Categoria2</option>
<option<?= $category == "Category3"?" selected = 'selected'":"" ?> value="Category3">Categoria2</option>
</select>
the correct answer is:
<select id="select_category" name="enquiry" >
<option value="Category1" <?php echo $category == "Category1"?" selected = 'selected'":"" ?> >Categoria1</option>
<option value="Category2" <?php echo $category == "Category2"?" selected = 'selected'":"" ?> >Categoria2</option>
<option value="Category3" <?php echo $category == "Category3"?" selected = 'selected'":"" ?> >Categoria2</option>
</select>

Categories