I have this code:
if(isset($_POST['search']))
{
$res1=mysql_query("SELECT * FROM aircraft where acode = '$_POST[ac]'") or die(mysql_error());
while($row=mysql_fetch_array($res1))
{
$airc=$row['acode'];
$amode=$row['amodel'];
$stat=$row['status'];
$rem=$row['remarks'];
echo "<center><table><form name=\"frmMain\" method=\"post\">
<tr><td><font face=consolas><b>Aircraft Code:</b></font></td><td><input type=text name=arc value='$airc' readonly=readonly></td></tr>
<tr><td><font face=consolas><b>Aircraft Model:*</b></font></td><td><input type=text name=am value='$amode'></td></tr>
<tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>
<tr><td><font face=consolas><b>Remarks:*</b></font></td><td><input type=text name=rm value='$rem'></td></tr></table>";
}
}
On submit 'search' button, this code displays the data from aircraft table. The user is allowed to update the data with the (*) sign.
Since the Status are the following by default (Available, Not Available), I changed this
<tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>
to this,
<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
<option value=Available>Available</option>
<option value='Not Available'>Not Available</option>
</select></td></tr>
But I want the dropdown to have it's default value depending on
$stat=$row['status']; since this is an update form.
If the data being retrieved has it's status 'Available', then the dropdown should have it's default value as 'Available'.
How can I achieve that? I tried <select name=status value='$stat'> but it doesn't work. Any help will be appreciated. Thanks!
Just put selected="selected" on the option depending on your $row['status'],
<option selected="selected" value="available">Available</option>
write Available and Unavailable into an array
$theArray = array("Available","Not Available");
loop the array:
<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
<?php
foreach ($theArray as $key => $value) {
if ($value == $stat) {
echo('<option selected="selected" value='.$value.'>'.$value.'</option>');
} else {
echo('<option value='.$value.'>'.$value.'</option>');
}
}
?>
</select></td></tr>
and in each loop we check if the value in the array, is the same as it is in the variable, if so, we put the selected there
understand the logic?
<select name=status>
<option value="available" <?php if($row['status']=="available") echo "selected=\"selected\""; ?>>Available</option>
<option value="unavailable" <?php if($row['status']=="unavailable") echo "selected=\"selected\""; ?>>Unvailable</option>
</select>
Basically echo selected="selected" for the option depending on value of the concerned field.
<?php
$status = "navail";
?>
<select name="sel">
<option value="avail" <?php if($status == "avail") echo "SELECTED";?> > Avail </option>
<option value="navail" <?php if($status == "navail") echo "SELECTED";?> > Navail </option>
</select>
You can define your variable value with additional option tag and mark that as selected like:
<select name="role" id="role">
<!-- This is default define value using php variable $r -->
<option selected="selected" value="<?php echo $r; ?>" disabled="disabled"><?php echo $r; ?></option>
<!-- Other options values -->
<option value="2">Option-2</option>
<option value="2">Option-2</option>
</select>
You can set the selected dropdown option from database as below:
<select name="status">
<option <?php echo ($row['status'] == 'Available') ? 'selected' : '' ?> value='Available'>Available</option>
<option <?php echo ($row['status'] == 'Not Available') ? 'selected' : '' ?> value='Not Available'>Not Available</option>
</select>
Declare the options in an array like
$arr = array("available" => "available","unavailable" => "unavailable");
and input the drop down like this
echo form_dropdown("st", $arr, set_value("st", (isset($row['status'];) ? $row['status']; : ""))
This is the method commonly used in frameworks like codeigniter.. i think it works in core php too..
its my first time here though and i tried using basic principles in php, dont know if this helps you but if you're trying to grab the defaulted value which you inputted on your database then edit it again i suggest you try this one
<select name="st">
<?php if($row['status']=="Available"){?><option value="Available">Available</option><?php }?>
<?php if($row['status']=="Unavailable"){?><option value="Unavailable">Unavailable</option><?php }?>
</select>
Assuming that the column name in your database is 'status', give it a try works for me
Related
I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>
I've try several times for dropdownlist validation,
my codes are in PHP and HTML, the the validation part is not working though and i've refer some of the solutions in stackoverflow which has similar case like mine.
Declare variable
$call_department = $db->escape((int)$_POST['call_department']); //where i declare this variable
HTML files
<tr><td>Department</td><td><select name='call_department'>
<option></option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; required?></option>
<?php } ?>
</select></td></tr>
Validation part:
<?php
if(isset($_REQUEST['call_department']) && $_REQUEST['call_department'] == '0') {
echo 'Please select a department.';
}
?>
1) Set first option value="0" like this
<option value="0">select</option>
2) Required attribute should be set to select tag not to option tag <select name='call_department' required>
<select name='call_department' required>
<option value="0">select</option>
<?php $call_dept = $db->get_results("select type_id,type_name from site_types where type=1 order by type_name;");
foreach ($call_dept as $dept )
{
?>
<option value='<?php echo $dept->type_id;?>'><?php echo $dept->type_name; ?></option>
<?php } ?>
</select>
This question already has an answer here:
Select option default based on database variable [duplicate]
(1 answer)
Closed 7 years ago.
I'm beginner in PHP MySQL. I successfully get the value from database in INPUT type but I can't get the data from database in SELECT type:
Here is my sample edit form where Gender & User Type can't output the value from my database:
and here is data from the table I created:
I'm calling the data in my input box by using this code:
<?php $userRow['agentFname']; ?> //$userRow['ROW NAME IN TABLE'];
But I can't call the data where In select box, like Gender: and User Type here is my code of Select box.
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option>Male</option>
<option>Female</option>
</select>
<select class="form-control" name="utype" >
<option selected disabled>*User Type</option>
<option>Agent</option>
<option>Team Leader</option>
<option>Admin</option>
</select>
#Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).
What I do is something like this:
<?php
$array = array("male", "female", "other");
echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
if ($gender == $databaseValue) {
echo "<option selected>$gender</option>";
} else {
echo "<option>$gender</option>";
}
}
echo "</select>";
?>
Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.
I already have my own answer to this
Here how I solved this:
<select class="form-control" name="aGender" >
<?php
if ($userRow['agentGender'] == 'Male') {
echo "<option selected>Male</option>";
echo "<option>Female</option>";
} else {
echo "<option selected>Female</option>";
echo "<option>Male</option>";
}
?>
</select>
I think you can use below conditional code for this issue:
<?php if($userRow['aGender'] == 'Male'){
$selectedMale = 'selected="selected"'
$selectedFemale = ''
}else{
$selectedFemale = 'selected="selected"'
$selectedMale = ''
}?>
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option <?php echo $selectedMale ;?>>Male</option>
<option <?php echo $selectedFemale;?>>Female</option>
</select>
and same for the another select box.
It will work hopefully.
In my opinion it's best to use ternary operator in such a case, combined with options element fetched from db/array. Something like:
<?php $options = Array(1 => "Option 1", 2 => "Option 2")
<select>
<?php foreach($options as $key => $opt): ?>
<option value="<?= $key ?>" ($key == $userRow['value'] ? "selected" : "" )><?= $opt ?></option>
<?php endforeach; ?>
</select>
I have this HTML/PHP Code that lists options in a select element.
Whats the best way to make the correct option selected based on a record from a MySQL database:
This works fine, but is there any easier way to do it with one line of code rather than doing an if statement per option?
<select name="status" id="status">
<option value="Open"<?php if($ticket["status"]=="Open"){echo('selected="selected"');}?>>Open</option>
<option value="Needs Action"<?php if($ticket["status"]=="Needs Action"){echo('selected="selected"');}?>>Needs Action</option>
<option value="Customer Reply"<?php if($ticket["status"]=="Customer Reply"){echo('selected="selected"');}?>>Customer Reply</option>
<option value="Completed"<?php if($ticket["status"]=="Completed"){echo('selected="selected"');}?>>Completed</option>
</select>
use an array:
echo "<select name='status' id='status'>";
$statuses = array('Open', 'Needs Action', 'Customer Reply', 'Completed');
foreach ($statuses as $status) {
echo "<option value='$status' " . ($ticket['status'] == $status) ? "selected='selected'" : "" . "/>";
}
echo "</select>";
You may want to put those values in the database or if you want to avoit it, just put them into an array and print all the options using a loop and mark as selected the right one.
Yes, the code can be shorter (and much more readable):
if($ticket["status"]=="Open") {
$openSelected = "selected='selected'";
}
if($ticket["status"]=="Needs Action") {
$needsActionSelected = "selected='selected'";
}
if($ticket["status"]=="Customer Reply") {
$customerReplySelected = "selected='selected'";
}
if($ticket["status"]=="Completed") {
$completedSelected = "selected='selected'";
}
<select name="status" id="status">
<option value="Open" <?php print($openSelected) ?>>Open</option>
<option value="Needs Action"<?php print($needsActionSelected) ?>>Needs Action</option>
<option value="Customer Reply"<?php print($customerReplySelected) ?>>Customer Reply</option>
<option value="Completed"<?php print($completedSelected) ?>>Completed</option>
</select>
EDIT// #Barmar has a better answer.
am new in php..i am trying to filter my datas from db by using dropdownlist..my code in view form is like
<?php
if(isset($value))
{
if($value==1)
{
echo"<option value='1' selected>option1</option>
<option value='0' >option2</option>";
}
if($value==0)
{
echo"<option value='1' >option1</option>
<option value='0' selected >option2</option>";
}
}
else
{
echo"<option value='1'>option1</option>
<option value='0'>option2</option>";
$value="";
}
?>
but if am selecting option1 its working correctly..but when am clicking option2 its filtering the correct datas but its selecting the status only..selected value is status ,ie same as null value...
Simply try replacing your code with this
<select name="status" id="status">
<option value=""><< Select Option >></option>
<option value="1" <?=($status=="1")?"selected":""?>>Active</option>
<option value="0" <?=($status=="0")?"selected":""?>>Inactive</option>
</select>
Its small and ternary operator is fast then if else conditions.
Before adressing your filtering problem that may need some more explanation
I'll suggest to refactor your code to avoid unnecessary conditions and code duplication
This is one way to do it :
<?php $values = array('Inactive', 'Active') ?>
<?php $selected = (!empty($status)) ? 'selected' : '' ?>
<?php foreach($values as $value => $label): ?>
<option value='<?php echo $value ?>' <?php echo $selected ?> ><?php echo $label ?></option>
<?php endforeach ?>