This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
I have a table ("venues") that stores all the possible venues a volunteer can work, each volunteer is assigned to work one venue each.
I want to create a select drop down from the venues table.
Right now I can display the venue each volunteer is assigned, but I want it to display the drop down box, with the venue already selected in the list.
<form action="upd.php?id=7">
<select name="venue_id">
<?php //some sort of loop goes here
print '<option value="'.$row['venue_id'].'">'.$row['venue_name'].'</option>';
//end loop here ?>
</select>
<input type="submit" value="submit" name="submit">
</form>
For example, volunteer with the id of 7, is assigned to venue_id 4
<form action="upd.php?id=7">
<select name="venue_id">
<option value="1">Bagpipe Competition</option>
<option value="2">Band Assistance</option>
<option value="3">Beer/Wine Pouring</option>
<option value="4" selected>Brochure Distribution</option>
<option value="5">Childrens Area</option>
<option value="6">Cleanup</option>
<option value="7">Cultural Center Display</option>
<option value="8">Festival Merch</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
Brochure Distribution option will already be selected when it displays the drop down list, because in the volunteers_2009 table, column venue_id is 4.
I know it will take a form of a for or while loop to pull the list of venues from the venues table
My query is:
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
How do I populate the select drop down box with the venues (volunteers_2009.venue_id, venues.id) from the venues table and have it pre-select the venue in the list?
$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['venue_id']}'";
if ($selected_venue_id == $row['venue_id'])
echo "selected = 'selected'";
echo ">{$row['venue_name']}</option>";
}
echo "</select>";
assuming you have an array of venues...personally i don't like to mix the sql with other wizardry.
function displayDropDown($items, $name, $label, $default='') {
if (count($items)) {
echo '<select name="' . $name . '">';
echo '<option value="">' . $label . '</option>';
echo '<option value="">----------</option>';
foreach($items as $item) {
$selected = ($item['id'] == $default) ? ' selected="selected" : '';
echo <option value="' . $item['id'] . '"' . $selected . '>' . $item['name'] . '</option>';
}
echo '</select>';
} else {
echo 'There are no venues';
}
}
<?php
$query = "SELECT * from blogcategory";
//$res = mysql_query($query);
$rows = $db->query($query);
echo "<select name = 'venue'>";
// while (($row = mysql_fetch_row($res)) != null)
while ($record = $db->fetch_array($rows))
{
echo "<option value = '{$record['CategoryId']}'";
if ($CategoryId == $record['CategoryId'])
echo "selected = 'selected'";
echo ">{$record['CategoryName']}</option>";
}
echo "</select>";
?>
<!DOCTYPE html>
<html>
<head>
<title>table binding</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="mydiv" style="width:100px;height:100px;background-color:yellow">
<select id="myselect"></select>
</div>
</body>
</html>
<?php
include('dbconnection.php');
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
echo "<script>
var z = document.createElement('option');
z.setAttribute('value', '".$row[0]."');
var t = document.createTextNode('".$row[0]."');
z.appendChild(t);
document.getElementById('myselect').appendChild(z);</script>";
}
?>
Related
I have made a dropdown search form that is auto populated by my database content. The voices in the table would be for example types of woods with varing dimensions.
So there are repeatable wood names with diverse data.
To avoid repetition the dropdown is populated with wood types combined to be selected then displayed with all their variants.
The problem is, upon selecting an input, the results are of the item listed above and not the one selected.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
echo "<option value=\"\">Selezione Materiale</option>";
while($row = $info->fetch_assoc()){
if($row['finit'] != $finit) {
echo "<option value=\"$finit\">" . $row['finit'] . "</option>";
$finit = $row['finit'];
}
}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Since there are many variants(dimensions) associated with a single wood type, you have to first take the wood type as input from user(via dropdown list), and probably then you may want to display all possible variants(dimensions) of that particular wood type.
So, change the SQL query in the following way,
$query = "SELECT DISTINCT finit FROM prime";
and the while loop in the following way,
while($row = $info->fetch_assoc()){
$output = "<option value='" . $row['finit'] . "'";
if($row['finit'] == $_POST['finit']){
$output .= " selected='selected'";
}
$output .= ">" . $row['finit'] . "</option>";
echo $output;
}
Try this but change if condition according your default value and sql value should be match.
<form action="search2.php" method="POST">
<select name="finit" onchange='this.form.submit()'>
<?php
include("connect.php");
$query = "SELECT finit FROM prime";
$info = mysqli_query($conn, $query);
$finit = '';
?>
<option value="">Selezione Materiale</option>;
<?php
while($row = $info->fetch_assoc()){
if($row['finit'] == $finit) {
$selected = 'selected';
}else{
$selected = '';
$finit = $row['finit'];
}
?>
<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>
<?php } ?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';
I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">