I need to select value dynamically using PHP in drop down list but in my case its not working as expected. I am explaining my code below.
<?php
$citySelected = "";
if(!isset($_GET['city'])){
$citySelected = 'selected="selected"';
}
?>
<select id="selectedLoc" name="selectedLoc" class="chosen-select form-control">
<option value="">Select City</option>
<option value="0" <?php echo $citySelected; ?>>Global</option>
<?php
foreach ($locationArr as $key => $value) {
$id = $value['id'];
$city = $value['city'];
$location = $value['location'];
$selected = "";
if(isset($_GET['city']) && $_GET['city']== $value['id']) {
$selected ='selected="selected"';
}
echo "<option value='$id' $selected>$location</option>";
}
?>
</select>
Here I need when there is any query string value then it will match with respective id and select that option and if there is no query string value at all then the global option will select.
Inside if condition ';' is missing. It should be like
if(isset($_GET['city']) && $_GET['city']== $value['id']){echo ' selected="selected"';}else{echo '';}
You can also use conditional operator for code simplicity, eg:-
<?php echo (isset($_GET['city']) && $_GET['city']== $value['id'])? ' selected="selected"':''; ?>
Related
I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.
These is my code to get the information from the database. So status is the one where in my database would be either Interested or Not Interested.
while($row= mysqli_fetch_array($result))
{
$ID =$row['Particulars_ID'];
$name = $row['Name'];
$number =$row['Number'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
I would like to echo out the value Not Interested if my database shows that this person is not interested. However from my below code, it always show Interested no matter which person i click on.
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
first - you do not need the comma in the select, second - ensure that this is the only element with the id of status, third - simply check the $status value in each option and echo selected if it is.
echo "<select name = 'status' id = 'status'>
<option value='Interested'";
if($status == "Interested"){echo " selected";}
echo">Interested</option>
<option value='Not Interested' ";
if($status == "Not Interested"){echo " selected";}
echo">Not Interested</option>
</select><br>";
since the default is the first option Interested, then if ($status === "Not Interested") set the option attribute selected
<?php
if ($status === "Not Interested") $selected = "selected";
else $selected = "";
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested' $selected>Not Interested</option>
</select><br>";
Change this
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
to this
// Set the selected attributes based on the value of status
$interested = ($status === 'Interested') ? ' selected' : '';
$notInterested = ($interested === '') ? ' selected' : '';
echo <<< SELECT
<select name="status" id="status">
<option$interested>Interested</option>
<option$notInterested>Not Interested</option>
</select>
SELECT;
You don't need the value attribute if the value is the same as the text.
If there were more than two options, I recommend a loop like this:
<?php foreach ($options as $o) : ?>
<option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option>
<?php endforeach ?>
i am selecting an entry from the database to be edited on php/html. i can call the values of a table from the database and put it in a multiple tag.
$job_exp_tags = $row[16];//value from another table
$job_tags = explode(',',$job_exp_tags);//array to be put "selected" on the multiple select tag
$sql_2 = "SELECT _id, job_name from job_tags";
$sel_2 = mysql_query($sql_2);
$array = array();
while($row_new = mysql_fetch_assoc($sel_2)){
$array[] = $row_new;
}
<tr>
<td>Job Experience:<br>
(Hold ctrl to select multiple)</td>
<td><select name = 'job_tags[]' multiple>
<?php
foreach($array as $value){ ?>
<option value ='<?php echo $value['_id']; ?>'> <?php echo $value['job_name']; ?> </option>
<?php
}
?>
</select> </td>
</tr>
my problem is how can i put selected values to it from the explode statement
edit:
i've solved the problem, thanks anyway guys!
<option value ="<?php echo $value['_id']; ?>" <?php echo in_array($value['_id'], $job_tags) ? 'selected="true"' : null; ?>><?php echo $value['job_name']; ?></option>
Just check if its in your array, if so, set it selected:
foreach($array as $value){ ?>
$selected = in_array($value, $job_tags) ? ' selected ' : '';
/* Or [selected="selected"] if you dont use html5 yet (which you should) */
<option value ='<?php echo $value['_id']; ?>' <?php echo $selected; ?>> <?php echo $value['job_name']; ?> </option>
<?php
}
Your code can be simplified though:
foreach($array as $value){
$selected = in_array($value, $job_tags) ? ' selected="selected" ' : '';
?>
<option value="<?=$value['_id']?>" <?=$selected?> > <?=$value['job_name']?> </option>
<?php
}
I changed the quotes arround the value to doubles, not really a rule, but it is a good practice to do so. The other change is the short echo. Small demo, both do the same:
<php $var = 'foorbar'; ?> <!-- A bit weird, but this is demo-purpose -->
<span><?php echo $var; ?></span>
<span><?=$var?></span>
You could look for the value in the string $job_exp_tags and if found set the selected property.
foreach($array as $value){
$selected = strpos($job_exp_tags, $value) ? 'selected' : '';
echo '<option value="'.$value['_id'].'" '.$selected.'>'.$value['job_name'].'</option>';
}
My code looks like:
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country();?>
</select>
I am populating all the countries of the world in that dropdown. I want a particular country name(say Japan) as default selected. I am not sure which attribute or property of the select tag does this. Tried to use many, but didnt work out. Can anyone suggest?
The code of populate_country goes like this:
if(connect_to_DB()==1)
$result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result))
{
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
}
In FetchCountriesList(), there is a select query like:
SELECT distinct CountryName from Countries
Thanks
Edit your function and try to find Japan and set this one as Selected :)
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
if($row['CountryName'] !== "Japan") {
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
} else {
echo '<option value='.$row['CountryName'].' selected="selected">'.$row['CountryName'].'</option>';
}
}
Add attribute selected. Try this:
<select name="country">
<option value="kor">Korea</option>
<option value="rus">Russia</option>
<option selected="selected" value="jap">Japan</option>
</select>
Add selected for your option in your function populate_country(). (in the option you want to select as default only)
Add the seleteced attribute, e.g.:
<option value="test" selected="selected">Bla</option>
$defValue = 'Japan';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['CountryName'] . '"';
if ($row['CountryName'] === $defValue) {
echo ' selected="selected"';
}
echo '>';
echo $row['CountryName'] . '</option>';
}
Just add attribute selected="selected" on the option value that you wish to be selected.
E.g.:
<option value="Japan" selected="selected">Japan</option>
To use this in your function, replace your function with this:
function populate_country($selected = NULL) {
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['CountryName'].'" '.($selected == $row['CountryName'] ? 'selected="selected"' : NULL).'>'.$row['CountryName'].'</option>';
}
}
You can then use this in your HTML:
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country("Japan");?>
</select>
<select>
<option> Red </option>
<option selected="selected"> blue </option>
<option> yellow </option>
</select>
Blue would be the selected in that dropdown.
Basically you need to loop throught your values and match if the value you want matches with the ones you have, and add the selected="selected" string to that option:
I would do something like this: (untested)
function populate_country($country){
$countries = array('algeria', 'japan', 'mexico', 'united kingdom' ..... );
foreach($countries as $country){
$selected = (strtolower($selected) == $country) ? ' selected="selected"' : null;
echo "<option$selected>".ucwords($country)."</option>\r\n";
}
}
I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down
Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!
Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page
If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>
i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>
Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above
<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>