I have the following code that get some options from database using php and mysql
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM Details";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
echo "<option value='{$Type}'>{$Type}</option>";
}
?>
</select>
I want to retain the dropdown list selection after form submission. I am using php $_POST[''] method for form submission. I tried the following way. But it is not working.
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM oaDetails";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
?>
<option <?php if ($_POST['Type']==$Type) echo 'selected="selected"'; ?> >
<?php echo $Type; ?>
</option>
<?php
}
?>
</select>
Does anyone know what am I doing wrong here?
Edit 1
I tried the following way. Now it is not showing any options. Instead some errors
<option value=<?php echo $_POST['Type']; ?> <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>
Edit 2
I tried following way and this time it is retaining the values. But only thing is if I select an option with spacing in between and click submit, the value is not retaining and just go to default blank. You can see what I mean by option with spacing as below
Options with no space as below retaining and working well as per my need
You are missing some quotes in your output HTML, that's why spaces in the value throw you out. It should be
<option value="<?php echo $_POST['Type']; ?>" <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>
Related
While editing the record from database, i display already selected value and option to select other values too. But i want to avoid already selected value to display twice in dropdown. Not getting how to do it
here is my code
<label class="control-label">Sales Area</label>
<?php
$sql5 = "SELECT * FROM sales_area ORDER BY name";
$query5 = mysqli_query($con, $sql5);
?>
<select name="area" class="form-control" required>
<option value="<?php echo $row['sales_area']; ?>"><?php echo $row['areaname']; ?></option>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>"><?php echo $rs5["name"]; ?></option>
<?php } ?>
</select>
in $row['sales_area'], data already present in database, this should not display again.
haven't tested it, but should be something like this:
<select name="area" class="form-control" required>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>" <?php if($rs5["id"] == $row['sales_area'] ) { echo "selected"; } ?> ><?php echo $rs5["name"]; ?>
</option>
<?php } ?>
</select>
Hi I would like to have it to work with two fields, both id and text but I cant even get this to work.
I get a post from the database to update.
If I have a ordinary optionlist. It views the selected value and I can change option and save it to the database.
This works:
<select name="ddlLinkType" title="<?php echo $row_visa_link['LinkType']; ?>">
<option value="" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == '' ) echo 'selected = "selected"'; ?>></option>
<option value="Styrelsen" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Styrelsen') echo 'selected = "selected"'; ?>>Styrelsen</option>
<option value="Alla" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Alla') echo 'selected = "selected"'; ?>>Alla</option>
<option value="Option3" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Option3') echo 'selected = "selected"'; ?>>Option3</option>
</select>
But I would like to get the options dynamic from database table. I have tried many things now but nothing works. I am so confused and everything is messed up right now. I am trying to generate the optionlist without loosing the function that it shows the selected value from the post to be updated.
This doesnt work:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
<!-- Dont know how to solve this-->
<option value="<?php($row['LinkCatID'])?>" <?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>>$row['LinkCatID']</option>
<?php }} ?>
</select>
Thank You.
This code is wrong:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
You dont acces to <?php echo $row['LinkCatID']; ?> in line 1, $row is defined in line 2.
Thank You! Now it Works :)
<select name="ddlLinkCategory" title="<?php echo $row_visa_link['LinkCategory']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategory)) { ?>
<option value= "<?php echo($row['LinkCatID'])?>"<?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>> <?php echo $row['LinkCatID']?></option>
<?php }} ?>
</select>
Sorry in advance for the novice question here...
I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.
However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?
<select name="test_select" style="width: 110px">
<option disabled="disabled" selected="selected">
<?php
if(!empty($_POST['test_select'])){
echo $_POST[test_select'];}
else
echo "Select Option"; ?>
</option>
<?php $sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
?><option><?php echo $row['test']; ?></option><?php }?>
</select>
Thanks in advance,
Dan
I suppose that problem is that forms are not sending disabled values.
I would edit code as following:
<select name="test_select" style="width: 110px">
<?php
if (empty($_POST['test_select']))
echo '<option selected="selected">Select Option</option>';
$sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
$selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select']
? ' selected="selected"'
: '';
echo '<option'.$selected.'>'.$row['test'].'</option>';
?>
</select>
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>
I'm trying to create a drop down menu that will select a value that is stored in the database. here's the code :
require 'koneksi.php';
$sql_select = "SELECT * FROM supplier";
$hasil = mysql_query($sql_select);
if(!$hasil) {
echo "data supplier not found ".mysql_error();
}
$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if($hasil2) {
$supplier = $data['nama_supplier'];
}
<select name="supplier">
<option value="">---pilih supplier---</option>
<?php
while($baris = mysql_fetch_array($hasil)){
?>
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
<?php }?>
</select>
the problem is my code creates a dropdown with nothing selected. here's the screenshot : link
i've tried all the solutions in the stackoverflow. but the dropdown value still nothing selected. i know that it has to be something simple that i am missing but seriously i cannot figure it out. please anyone help, thanks!
I think the problem lies in this line:
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
You're missing an echo and it looks funny :/
Try instead:
<option <?php $val=$baris['nama_supplier']; echo "value='$val'"; if($supplier==$val) echo "selected='selected'>";echo $val;?> </option>;
Try this way..
$sel="select f.*,c.category from final f, category c where f.category_id=c.id and f.id='$id'";
$data=mysql_query($sel);
$res=mysql_fetch_assoc($data);
<select name="cat">
<?php
$sql = mysql_query("SELECT * FROM category");
while ($row = mysql_fetch_array($sql)){
if($row['id'] == $res['category_id']){
echo '<option value="'.$row['id'].'" selected="selected">'.$row['category'].'</option>';
}else{
echo '<option value="'.$row['id'].'">'.$row['category'].'</option>';
}
}
?>
</select>
Try this out
require 'koneksi.php';
$sql_select = "SELECT * FROM supplier";
$hasil = mysql_query($sql_select);
if(!$hasil) {
echo "data supplier not found ".mysql_error();
}
$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if(mysql_num_rows($hasil2) > 0) {
$supplier = $data['nama_supplier'];
}
<select name="supplier">
<option value="">---pilih supplier---</option>
<?php
while($baris = mysql_fetch_array($hasil)){
?>
<option value="<?php echo $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) {echo 'selected="selected"';} ?> > <?php echo $baris['nama_supplier']; ?> </option>;
<?php }?>
</select>
<option value="<?php $baris['nama_supplier'] ?>
should be
<option value="<?php echo $baris['nama_supplier'] ?>
I spent some time trying to find the best solution for this, and came up with a tiny little jQuery code.
First of all you should be using PDO or mysqli instead of mysql, since it's deprecated. Let's assume you've fixed that.
Inside the <form> tag, add an <input type="hidden"/> so that it can storage your database value, for example:
HTML
<form>
<input id="valueFromDatabase" type="hidden" value="<?php echo $stringFromDB ?>"/>
</form>
Note: in this case, $stringFromDB is a variable that holds your query's return from DB.
So now we have the value of our database inside our HTML code. Now we just need to check if any of the options inside the <select> tag is equal to this value. We'll be doing it with jQuery:
jQuery
$( document ).ready(function() {
$('option').each(function(){
if (this.value == $('#valueFromDatabase').val()){
this.setAttribute('selected', 'selected');
}
});
});
What's happening here? We are telling to jQuery to analyze all the <option> tags in the HTML and compare its value with our value from database; if its equal, we add the selected attribute to the equivalent <option>.
You can it working here (used a calendar example).
Hope that helps!