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>
Related
I'm not sure how to describe it, so here's a video where I explain my problem.
I tried rearranging some of the code, as I do believe nothing is faulty, attempting to make sure that the table refreshes with the new data inside it, however every time I tried to place my code in a different order (executing the queries in different orders), it either functions differently than how I want it to function or it doesn't function at all.
Both queries do function separately, I'm just unsure why they're not working together.
Searchbar has the value seen inputted in the homepage on both my Search page and this page in question. However it was left blank for this page, which gave me the result of having the full table display which is what I wanted to happen. I'm just not sure how I can edit my code so, when submitted, it will display the newly added data.
My PHP:
<?php
$find = $_POST['searchbar'];
$host = "localhost";
$username = "FFF";
$pword = "L3FhqJNey8Op2qJY";
$database = "Project";
include 'includes/db.inc.php';
$Name2 = $_POST['Name'];
$YearOfRelease2 = $_POST['YearOfRelease'];
$Studio2 = $_POST['Studio'];
$Age2 = $_POST['Age'];
$Score2 = $_POST['Score'];
?>
My HTML:
<html>
<head>
<title>Add a Film - Films! Films! FILMS!</title>
</head>
<body>
<h1>Films! Films! FILMS!</h1>
<h2>Add a Film</h2>
<p>If you wish to add a film to our database, feel free to add data relating to the film in the respective boxes below. You should then refresh the page.</p>
<p>Add Film:</p>
<form method="POST" action="AddFilm.php">
<p>Name of Film: <input type="text" name="Name"></p>
<p>Year of Release: <input type="text" name="YearOfRelease"></p>
<p>Name of Studio: <input type="text" name="Studio"></p>
<p>Age Rating: <select name="Age" size="1">
<optgroup label="Select Age Rating">
<option value="U">U</option>
<option value="PG">PG</option>
<option value="12">12</option>
<option value="15">15</option>
<option value="18">18</option>
</optgroup>
</select></p>
<p>Review Score: <input type="text" name="Score"></p>
<p><input type="submit" name="submit" value="Submit and Refresh"></p>
</form>
<?php
echo "<h2>$output</h2>";
$query_string = "SELECT * FROM movies WHERE Name LIKE '%$find%' OR YearOfRelease LIKE '%$find%' OR Studio LIKE '%$find%' OR Age LIKE '%$find%' OR Score LIKE '%$find%'";
$query_string2 = "INSERT INTO movies (Name, YearOfRelease, Studio, Age, Score) VALUES ('$Name2', '$YearOfRelease2', '$Studio2', '$Age2', '$Score2');";
if ($result = $mysqli->query($query_string2)) {
$output2 = $Name2 ." has been added to the database.";
echo "<p>$output2</p>";
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$result->close();
if ($result = $mysqli->query($query_string)) {
echo "<table border='1'>";
echo "<tr><th>FilmID</th><th>Name</th><th>YearOfRelease</th><th>Studio</th><th>Age</th><th>Score</th></tr>";
while ($row = $result->fetch_object())
{
$FilmID = $row->FilmID;
$Name = $row->Name;
$YearOfRelease = $row->YearOfRelease;
$Studio = $row->Studio;
$Age = $row->Age;
$Score = $row->Score;
$output ="<tr><td> $FilmID";
$output = $output . "<td> $Name";
$output = $output . "<td> $YearOfRelease";
$output = $output . "<td> $Studio";
$output = $output . "<td> $Age";
$output = $output . "<td> $Score </tr>";
echo "<p>$output</p>";
}
echo "</table>";
echo "<hr>";
echo '<p>Back to Home Page</p>';
$result->close();
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$mysqli->close();
?>
</body>
</html>
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'm trying to add results into a html dropdown.
The php works if I take it outside the html form: it shows the results, but I need it inside the form
<form><form method="post" action="selldo.php">
<label><br /><br /><br /><br />What slot do you want to Sell?</label>
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<option value=""></option>
<?php
$result = mysql_query("SELECT * FROM user_pokemon
WHERE belongsto='$_SESSION[username]'");
while($row = mysql_fetch_array($result))
{
echo $row['id'] . " " . $row['pokemon'];
echo "<br />";
}
?>
</select><br/><br/>
<label>Price You Would Like For The Pokemon?</label>
<input type="int" name="cost" id="cost" maxlength="30"/><br/><br/>
<button name="submit" type="submit" id="Submit_But">Sell</button>
<p> </p><p> </p>
</form>
When I look in the dropdown menu there is nothing but if it makes the SQL out of the form it posts the results to the page so it works fine I just need it in side the drop down html form
p.s i have the connect ontop of the page
You will need to echo out HTML option elements:
while($row = mysql_fetch_array($result)) {
echo "<option>" . $row['id'] . " " . $row['pokemon'] . "</option>";
}
You will probably want to give the option elements a value so the selected option is passed along properly when the form is submitted.
Did you look at the source this code generates?
You will find that your options are all there but just somewhere in the void, not wrapped by any html tags. You'll see something like:
<form>
<select>
<option></option>
your first option
your second option
your third option
your n'th option
</select>
</form>
But what you really need, for the markup to be correct, is this:
<option>your first option</option>
<option>your second options</option>
And so forth... that should be enough for you to get it right! If not...
echo '<option value="' . $row['id'] . '">' . $row['pokemon'] . '</option>';
You have an SQL-injection hole and a possible XSS security hole:
Correct this by changing the php code to:
<?php
$username = mysql_real_escape_string($_SESSION['username']);
$result = mysql_query("SELECT * FROM user_pokemon
WHERE belongsto = '$username' ");
while($row = mysql_fetch_array($result))
{
$id = htmlentities($row['id']);
$pokemon = htmlentities($row['pokemon']);
echo '<option value = "$id"> $pokemon </option>';
}
?>
See: What are the best practices for avoiding xss attacks in a PHP site
And How does the SQL injection from the "Bobby Tables" XKCD comic work?
You're not creating a select! you need the <option></option> tags for that, not just echo out your results...
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<option value=""></option>
<?php
$username = mysql_real_escape_string($_SESSION['username']);
$result = mysql_query("SELECT * FROM user_pokemon WHERE belongsto='$username'");
while($row = mysql_fetch_array($result)) : ?>
<option value="<?php echo htmlentities($row['id']);?>"><?php echo htmlentities($row['pokemon']);?></option>
<?php endwhile;?>
</select>
This should do the trick:
<select name="pokeSLOT" id="pokeSLOT" style="width:150px;padding-left:5px;">
<?php
$result = mysql_query("SELECT * FROM user_pokemon WHERE belongsto = '$_SESSION[username]'");
while($row = mysql_fetch_array($result)) {
echo "<option value=\"\">" . $row['id'] . " " . $row['pokemon'] . "</option>
?>
</select>
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>";
}
?>