I have a mysql table which contains following cols.
Id Name Sex
and sex column have type of enum('Male','Female','Unspecified')
How can i list enum values in a dropdown and make current stored value as selected
Check this link...its pretty awesome..the script is reusable for any enum column:
http://jadendreamer.wordpress.com/2011/03/16/php-tutorial-put-mysql-enum-values-into-drop-down-select-box/
The fact that it is an enum field doesn't matter much when creating the select (dropdown) fields. Enum fields behave the same as a text input field, they just reject any data that doesn't match an enum option and store the data more efficiently. Thus, interacting with an enum field is the same as interacting with a text input field.
So you will need a normal html select field:
<form>
<select name="gender">
<option value="Unspecified">Unspecified</option>
<option value="Male">Male</option>
<option value="Female">Female</option
</select>
</form>
And you will need to select your value:
<form>
<select name="gender">
<option value="Unspecified" <?php if($gender == "Unspecified") { echo "SELECTED"; } ?>>Unspecified</option>
<option value="Male" <?php if($gender == "Male") { echo "SELECTED"; } ?>>Male</option>
<option value="Female" <?php if($gender == "Female") { echo "SELECTED"; } ?>>Female</option
</select>
</form>
This can be broken out into functions:
function gender_select($default_value='') {
$select = '<select name="gender">';
$options = array('Unspecified','Male','Female',);
foreach($options as $option) {
$select .= write_option($option, $option, $default_value);
}
$select .= '</select>';
return $select;
}
function write_option($value, $display, $default_value='') {
$option = '<option value="'.$value.'"';
$option .= ($default_value == $value) ? ' SELECTED' : '';
$option .= '>'.$display.'</option>';
return $option;
}
So your final code would be:
<form>
<?php echo $gender_select($gender); ?>
</form>
Related
am doing like this. this is my dynamic dropdown which is populating from database result. when i submit the form, validation apply, if validation error occur on empty field, form stops to submit. BUT all dropdown also removes its values, so i again fill all form instead of empty fields
my previous code is: before apply set select
<select name="position_filled_against_id" id="position_filled_against_id">
<option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
$selected = "";
echo '<option value="'.$position_filled->position_filled_against_id.'" >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>
after apply set select: but syntax error
<select name="position_filled_against_id" id="position_filled_against_id"><option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
$selected = "";
echo '<option value="'.set_select("position_filled_against_id",$position_filled->position_filled_against_id,TRUE).'" >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>
You can try with below code it will help you
<select name="position_filled_against_id" id="position_filled_against_id">
<option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
$selected = "";
if($_REQUEST['position_filled_against_id']==$position_filled->position_filled_against_id)
{
$selected = 'selected="selected"';
}
echo '<option value="'.$position_filled->position_filled_against_id.'" '.$selected.' >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>
i remove the syntax error
<select name="position_filled_against_id" id="position_filled_against_id">
<option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
$selected = "";
echo '<option value="'.$position_filled->position_filled_against_id.'" '.set_select("position_filled_against_id","$position_filled->position_filled_against_id", TRUE).' >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>
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 ?>
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'm dealing with a registration form at the moment, specifically, I'm dealing with redisplaying any information the user enters into the inputs if there is an error in the registration verification.
Normally, when a user hits 'submit' - if there is an error, the page refreshes and they are echoed out and the form is redisplayed. The problem is there is sometimes genuinely valid information in some of the fields, and by regrabbing that data, it can be redisplayed using the value attribute and the isset() function like so (if of course that data has been POSTed, which in this case it has):
<input type="email" name="anEmail"
value=" <?php echo (isset($email)) ? $email : false; ?>" />
That works fine for the <input> element, but is the same achievable with a dropdown list through the <select> element?
I initially tried:
<select name="region"
value="<?php echo (isset($region)) ? $region : 'Auckland'; ?>" >
<!-- with 'Auckland' being the default value of the list -->
However, it doesn't seem like that value="" is a valid attribute for the <select> tag.
Any ideas on how I could accomplish this? Cheers.
function build_select($name, array $options = NULL, $selected = NULL)
{
foreach($options as $value => $option)
$return .= ($value != $selected)? '<option value="'.$value.'">'.$option.'</option>': '<option selected="selected" value="'.$value.'">'.$option.'</option>';
return '<select name="'.$name.'">'.$return.'</select>';
}
echo build_select('region', array('Lorem' => 'Ipsum', 'Auckland' => 'Auckland', 'Ipsum' => 'Lorem'), 'Auckland');
HTML
<select name="region">
<option value="Lorem">Ipsum</option>
<option selected="selected" value="Auckland">Auckland</option>
<option value="Ipsum">Lorem</option>
</select>
You have to add selected="selected" to specific option. Like so:
<select name="region">
<option value="opt1">Option 1</option>
<option value="opt2" selected="selected">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
With this code Option 2 is selected
And in pehe you have to check if specific option equals the input.
<option value="opt4"<?php if ($region == "opt2"){ echo ' selected="selected"'; } ?>>Option 4</option>
it's selected="selected", i think. or just "selected".
a quick example of how you might implement that:
case "value0":
$selected0 = "selected";
break;
case "value1":
$selected1 = "selected";
break;
<option value="value0" <?php echo $selected0;?>></option>
<option value="value1" <?php echo $selected1;?>></option>
function __selected($ctrlName,$value)
{
if(isset($_REQUEST[$ctrlName]) && trim($_REQUEST[$ctrlName]) == trim($value))
return "selected='selected'";
else
return false;
}
use like
<select name="cmbCondition">
<option value="Excellent" <?php echo __selected('cmbCondition',"Excellent")?>>Excellent</option>
<option value="Good" <?php echo __selected('cmbCondition',"Good")?>>Good</option>
<option value="Poor" <?php echo __selected('cmbCondition',"Poor")?>>Poor</option>
</select>
One way you can do this if number of options are not much:
In every option put the code:
<select>
<option selected="<?php echo (isset($region) && $region=='Volvo') ? $region : ''; ?>">Volvo</option>
<option selected="<?php echo (isset($region) && $region=='Saab') ? $region : ''; ?>">Saab</option>
<option selected="<?php echo (isset($region) && $region=='Mercedes') ? $region : ''; ?>">Mercedes</option>
<option selected="<?php echo (isset($region) && $region=='Audi') ? $region : ''; ?>">Audi</option>
</select>
But if options are much in number javascript code is needed to be written..
if(isset($region))
{
echo '
<script>$("#id option").each(function()
{
if($(this).val()=='.$region.')
$(this).attr("selected", "selected");
});
</script>';
}
Dynamiclly creating options basod on array.
Usage should be pretty straightforward.
<?php
function rennder_options($options = array(), $selection = null) {
if (!is_array($options)) {
return false;
}
$result = "";
foreach($option as $option) {
$option_str = '<option value="'.$option['value'].'"';
if ($selection !== null && $option['value'] == $selection){
$option_str .= ' selected="selected"';
}
$option_str .= '>'.$option['name'].'</option>';
$result .= $option_str;
}
return result;
}
$options = array(
array( "value" => "opt1", "name" => "Option 1" )
array( "value" => "opt2", "name" => "Option 2" ),
array( "value" => "opt3", "name" => "Option 3" ),
);
?>
<select name="region">
<?php echo render_options($options, $_POST['region']); ?>
</select>
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>