with this code I get dropdown menu to select:
<select name='select'>
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_array($sql)) {
<OPTION selected='{$kategorija}' VALUE='" . $result[0] . "'>" . $result[0] . "</OPTION>
}
</select></td>
This is how my table looks:
ID DropDownMenu xxx yyy zzz
How to set that dropdown menu selected value is the value connected to that ID.
In my case I always get last value from dropdown menu as selected one.
Try this: UPDATED
$out = '<select name="select">';
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_assoc($sql)) {
$out .= "<OPTION selected='" .$result['$kategorija']. "' VALUE='" .$result[0]. "'";
if ($result['$kategorija'] == 'something') {
$out .= "selected";
}
$out .= ">" .$result[0]. "</OPTION>";
}
$out .= "</select></td>";
echo $out;
first store the selected sport in database to a variable
eg:
$sport='cricket';//substitute with database fetch value
Sport: <select name="sport" >
<option value="All" >All</option>
<?php $a="select * FROM kategorija";
$d2=mysql_query($a);
while($d4=mysql_fetch_array($d2))
{?> <option value="<?php echo $d4['sport']; ?>"<?php if($sport==$d4['sport']){ echo "selected";} ?> > <?php echo $d4['sport']; ?> </option> <?php
}
?></select>
Related
<select name= "NEE_category">
<?php
$sql = mysqli_query($dbConn, "SELECT catDesc FROM NEE_category");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"NEE_category\">" . $row['catDesc'] . "</option>";
}
?>
</select>
I want to echo the selected option in PHP however using this:
$NEE_category = isset($_REQUEST['NEE_category']) ? $_REQUEST['NEE_category'] : null;
echo "\t<p>Event Category: $NEE_category</p>\n";
Returns: Event Category: NEE_category
How do i return the selected option value and not the name?
You're setting the value to "NEE_category" instead of the values you're pulling from your database.
echo "<option value=\"". $row['catDesc'] ."\">". $row['catDesc'] ."</option>";
I want to make select options list with data from my database
I did something like this
<?php
if ($resultt->num_rows > 0) {
// output data of each row
while($row = $resultt->fetch_assoc()) {
?>
<select name="name" class="custom-select">
<?php
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
?>
</select>
<?php
}
} else {
echo "0 results";
}
?>
I see my output but every data is in seperated "box". I just want list to choose for example options1 or 2.
What I did wrong?
You need to take the <select> element outside the loop.
<?php
if ($resultt->num_rows > 0) {
echo '<select name="name" class="custom-select">';
// output data of each row
while($row = $resultt->fetch_assoc()) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo '</select>';
} else {
echo "0 results";
}
its happen because you put <select> inside loop so it create every time new <select>
just put your select outside loop like as follow
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
You are nesting select elements inside each loop
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='".$row['name']."'>" .$row['name']."</option>";
}
?>
</select>
And watch out for unnecessary single quote that will break your html:
"'>"
I have made a form that lets the user to select a year and place they want. The select tag for year is as below:
<strong>Tahun: </strong>
<?php
echo ('<select name="tahun">');
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo ('<option value= "'.$i.'">'.$i.'</option>');
}
echo ('</select>');
?>
And code for place selection tag as below:
<strong>Tempat: </strong>
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error._LINE_);
while($row = $results->fetch_assoc()){
echo ('<option value= "'.$row['nama'].'">'.$row['nama'].'</option>');
}
echo ('</select>');
?>
Now, what I want to do is add empty value with a message "Please choose one" for both selection tag. So, can someone help me how to add that value and message in this tag?
If you wanted to add select options to your code, this is how it should be:
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo ('<option>Please choose one </option>'); // Added this here
while ($row = $results->fetch_assoc()) {
echo ('<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>');
}
echo ('</select>');
?>
When you add default option with empty value then you should add as <option value="">Select...</option>
<strong>Tahun: </strong>
<?php
echo '<select name="tahun">';
echo '<option value="">Please Choose One</option>';
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo '<option value= "'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
and
<?php
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo '<select name="tempat">';
echo '<option value="">Please choose one</option>'; //Please choose one
while ($row = $results->fetch_assoc()) {
echo '<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>';
}
echo '</select>';
?>
I'm wondering if it is possible to SET a heading (i.e. Select Name)for the select dropdown that query data from the database. Many thanks
Current code:
<select>
<?php
include("db.php";)
$sql = SELECT * FROM Persons;
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";}?>
</select><br>
Desired effect
<select>
<option value="default">Select Name</option>
<option value="Tom">Tom</option>
<option value="Mary">Mary</option>
<option value="John">John</option>
</select><br>
Just prepend a <option value="default"> to the other options:
<select>
<option value="default">Select Person</option> <!-- default option -->
<?php
include("db.php";)
$sql = "SELECT * FROM Persons";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo " <option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
Here i am populating my dropdowns with database fields using php
my code follows,
while ($row = mysql_fetch_array($result)) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
after fetching i echo it to html
<select id="Series1" onchange="changeVal('Series1')">
<option value="">Please select</option>
<?php echo $series1 ?>
</select>
my problem is i have some null values in the database field, i don't want that to be inserted in the options field. my final result now look like this
please help me.
Try this
while ($row = mysql_fetch_array($result)) {
if($row['Series'] != '' || $row['Series'] != NULL) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
}
OR
In your sql query
SELECT * FROM your_table WHERE Series IS NOT NULL
You can try like this
while ($row = mysql_fetch_array($result)) {
if(isset($row['Series'])) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
}
Try this...
<select id="Series1" onchange="changeVal('Series1')">
<option value="">Please select</option>
<?php
while ($row = mysql_fetch_array($result))
{
if($row['Series'] != '' || $row['Series'] != NULL)
{
?>
<option value="<?php echo $row['Series']; ?>"><?php echo $row['Series']; ?></option>
</select>
<?php
}
}
?>