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>
Related
I has created dynamic list in php.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['path']."'>'".$row['studentName']."'</option>";
}
?>
</select>
When I'm selected the list it give empty string. But when I'm not selected anything it return the first value which is Student Name. I tried to get the value from this line.
if(isset($_POST['student'])){
$selectedName = $_POST['student'];
}
I also try to checked whether the isset is set or empty. The isset is set but its empty.
This is the only solution Google provided.Ideas?
Edit
I have solved the problem. The explanation on the answer below.
At first I thought $_POST['student'] returning the text inside the <option> tag and I was wrong. I learnt that it return the value which is should be set in <option> attribute, So I just set the value to $row['studentName'], because $row['path'] returned empty string. Correct me if what I said is wrong. So basically, the code looks like below.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['studentName']."'>'".$row['studentName']."'</option>";
}
?>
</select>
Thanks for all your responses. Your responses made me think what should I do.
Use below code:
<select name="student" id="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option name='".$row['studentName']."' value='".$row['path']."'>'".$row['studentName']."'</option>";
}?>
</select>
You should be use this line isset($_POST['studentName']) && $selected = $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null; for selected option
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
$selected = isset($_POST['studentName']) && $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null;
echo "<option ".$selected." value='".$row['path']."'>".$row['studentName']."</option>";
}
?>
</select>
#dongcheng This is the same code but with better alignments. So you can see your code clearly.
<select name="student">
<option selected disable class="hideoption">Student Name</option>
<?php
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo '<option value="' . $row['path'] . '">' . $row['studentName'] . '</option>';
}
?>
</select>
use
$selectedStudent = !empty($_POST['student']) ? $_POST['student'] : '';
if ($selectedStudent) {
// your code
}
I have the following dropdown implemented:
<select style="margin-left:25px;">
<option value="null"> </option>
<option value="single">Single</option>
<option value="in_a_relationship">In a relationship</option>
<option value="engaged">Engaged</option>
<option value="married">Married</option>
<option value="open_rel">In an open relationship</option>
<option value="divorced">Divorced</option>
</select>
Say for example, a user has already selected Single from the drop down menu. When they do back to this page, I want single to be already chosen from the list. For text fields I can simply get the value from the database and then assign it to a variable and then echo it in the value. I.e.
$get_data2 = mysqli_query ($connect, "SELECT * FROM user_bio WHERE username = '$username'");
$row3 = mysqli_fetch_assoc ($get_data2);
$bio = $row3['about_me'];
and then do ...
<textarea name="biotextarea" form="change_details" rows="4" cols="60" maxlength="255"><?php echo $bio; ?> </textarea>
How can I do this for a select tag?
Here's how to select the option Single in HTML :
<select style="margin-left:25px;">
<option value="null"> </option>
<option value="single" selected>Single</option>
<option value="in_a_relationship">In a relationship</option>
<option value="engaged">Engaged</option>
<option value="married">Married</option>
<option value="open_rel">In an open relationship</option>
<option value="divorced">Divorced</option>
</select>
So, how to set this in PHP?!
Well, you could generate your <select> element like this :
<?php
$options = [
'null' => ' ',
'single' => Single',
'in_a_relationship' => 'In a relationship',
'engaged' => 'Engaged',
'married' => 'Married',
'open_rel' => 'In an open relationship',
'divorced' => 'Divorced'
];
?>
<select style="margin-left:25px;"><?php
foreach($options as $key => $value) {
echo '<option value="' . $key . '"';
if ($key === $row3['status']){
echo ' selected';
}
echo '>' . $value . '</option>';
}
?></select>
Setting a preselected option on a select tag requires you to put a selected attribute on the selected option.
<select style="margin-left:25px;">
<?php
$options = array(array("null"," "),array("single","Single"),array("in_a_relationship","In a relationship"),array("engaged","Engaged"),array("married","Married"),array("open_rel","In an open relationship"),array("divorced","Divorced"));
foreach ($option in $options){
if ($option[0] == $row3['status']){
$selected = 'selected ';
}
else{
$selected = '';
}
echo '<option '.$selected.'value="'.htmlspecialchars($option[0]).'">'.htmlspecialchars($option[1]).'</option>';
}
?>
</select>
The solution of Musa it's OK, but If you don't want to change your PHP too much
you can do the task with Javascript.
With jQuery or pure Javascript
function selector(id, value){
select = document.getElementById(id);
for(i=0;i<select.options.length;i++){
if(select.options[i].value == value){
select.selectedIndex = i;
}
}
}
//Pure Javascript
selector("status", "engaged")
//With jQuery
jQuery('#status').val('married');
Check this Snippet
Is there a way to display the $city from the database into a select input ? using a loop or anything ? find my trials below: thank you in advance
PHP CODE
<?php
//get city value
if($city == 'Choose City') {
$city = $row['City'];
}
?>
HTML CODE
<select name="City">
<option value="0" selected>Choose City</option>
<option value="1">Milan</option>
<option value="2">Paris</option>
...
</select>
Here is what I use the $mydata variable being the connection code I can add that if you need it. Tis code will loop round all the cities as many times there are cities.
while($record = mysql_fetch_array($mydata)){
echo $record['City'] ;
echo "<br>";
}
I hope this helps! Ask if you need any other help!
do it simply as:
<select name="City">
<option value="0" selected>Choose City</option>
<?php
//your loop while or any other to fetch city array
//get city value
if($city == 'Choose City')
{ ?>
<option value="<?php echo $row['City']; ?>"><?php echo $row['City']; ?></option>
<?php
}
//end loop
?>
</select>
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 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>