Keep options selected in multiple combobox - php

this is a code that generates a multiple dropdown list from a MySQL database and creates a table after clicking on a submit button with a selected option by default.
I need the table to be created when the page is loaded and also the dropdown list to keep the options selected after clicking the button.
Thanks in advance.
<?php
include 'conBBDD_1.php';
$db_handle = new conBBDD();
$TourResult = $db_handle->runQuery("SELECT DISTINCT bbcTour FROM concerts ORDER BY bbcDate DESC");
?>
<form method="POST" name="Search" action="example.php">
<?php
if (! empty($TourResult)) {
$format = "Tour 2020";
$select = 'selected';
echo "<select name='tour[]' id='bb_tour' multiple='multiple'>";
foreach ($TourResult as $key => $value) {
echo "<option value='" . $TourResult[$key]['bbcTour']."'";
if($TourResult[$key]['bbcTour']==$format) // to select one option only
echo $select. ">";
else
echo ">";
echo $TourResult[$key]['bbcTour'].'</option>' . "\n";
}
}
echo "</select>";
?>
<input type="submit" id="button" value="OK">
</form>

I don't quite understand the issue, but have cleaned this up for you.
If the issue is that (like your comment) you just want one item to be selected, remove the 'multiple' attribute from the select tag here.
Otherwise the 'multiple' tag and 'selected' just need to be lone strings w/ values.
<?php
include 'conBBDD_1.php';
$db_handle = new conBBDD();
$TourResult = $db_handle->runQuery("SELECT DISTINCT bbcTour FROM concerts ORDER BY bbcDate DESC");
echo '<form method="POST" name="Search" action="example.php">';
echo "<select name='tour[]' id='bb_tour' multiple>";
if (!empty($TourResults)) {
$format = "Tour 2020";
foreach ($TourResults as $Tour) {
echo "<option value='" . $Tour . "'";
if($Tour === $format) {
echo ' selected';
}
}
echo ">" . $Tour . "</option>\n";
}
echo "</select>";
echo "</form>";
?>

Related

PHP MySQL keep selected option from dropdown menu when menu is select from database

I have a problem with my page. I was trying to solve it by a lot of tutorials but i don´t know how to make it work. Simply put i have a database of objects. When i select object, page will redirect to another where are shown all informations about the object. But i need to keep the selected option in drop down menu. There is 110 objects so if i select object number 25, informations will show but the drop down menu wont stay on number 25. Can somebody help me with it?
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Vyber">
</form>
<?php
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
?>
First page php
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
Vyberte si zvolený objekt z menu a stlačte tlačidlo výber<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
Check whether the post value matches the object id in the loop.
$selcted = $_POST['Objekt'] == $row['Objekt'] ? ' selected' : '';
If it does $selected is set to " selected" and added to the option.
echo "<option value='" . $row['Objekt'] . "'" . $selected . ">" . $row['Objekt'] . "</option>";
You can send that value $_POST['Objket'] via a variable let us say $selected_value and in the option tag you can write
<option value='" .$row['Objekt']. "' '.if($row['Objket']==$selected_value) echo selected.'>" .$row['Objekt']. "</option>
When you select a option and click on button then it will work otherwise no.
if(isSet($_POST['Objekt']))
{
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
}

How to edit drop-down selected value?

I have created a drop-down menu and many other text fields on my html page which fetches the option values from database. It is inserting the selected values in my database table. I want to edit the selected values from the drop-down menu and update them in database. When I open the edit page, the form shows the previous saved values to edit them. But the problem is; When I don't edit the drop-down values and submit the form, It shows me error of undefined index and when I edit or select different value of drop-down then it works fine and don't show any error. Here is my code:
HTML
<label>Courses</label><select name="courses" id="courses" class="dropdownclass"><option selected="selected" value="" disabled selected hidden>-- Select an option --</option><?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
$sql = "SELECT courses FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
<!-- begin snippet: js hide: false console: true babel: false -->
EDITRECORD
<?php
include("connection.php");
$Sno = (int)$_GET['Sno'];
$query = mysql_query("SELECT * FROM table WHERE Sno = '$Sno'") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
if (isset($_POST)) {
echo "";
$id=$row['id'];
$courses=$row['courses'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<form id="form" action="update.php" method="post" enctype="multipart/form-data">
<fieldset>
<input type="hidden" name="new" id="Sno" value="<?=$Sno;?>" />
<label>Courses</label><select name="courses" id="courses" class="dropdownclass" ><option selected="selected" value="" disabled selected hidden><?php echo $courses; ?></option><?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
$sql = "SELECT courses FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
?>
</select>
</fieldset>
</form>
</body>
</html>
UPDATE
<?php
include("connection.php");
$Sno ='';
if( isset( $_POST['new'])) {
$Sno = (int)$_POST['new'];
}
$id = mysql_real_escape_string($_POST['id']);
if(isset($_POST['courses'])){
$courses = mysql_real_escape_string($_POST['courses']);
}else{
$courses=$_POST['courses'];
}
query="UPDATE technicalsol
SET id= '$id',
courses = '$courses'
WHERE Sno=$Sno";
$res= mysql_query($query);
if($res){
echo "<div style ='font-size:20px; margin-left:140px;'>Records updated Successfully</div>";
include("search.php");
}else{
echo "Problem updating record. MY SQL Error: " . mysql_error();
}
?>
I want that; If I don't edit the drop-down selected value, It just takes the previous value and saves it.
What you are doing right now is listing the options in dropdown list.
You are not setting up any answer
In your HTML:
while ($row = mysql_fetch_array($result)) {
echo "<option value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
}
make sure you make the old value selected.
may be by using an IF statement

PHP-HTML5-Form - My query-generated dropdown posts a blank value

When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)

Modify a selection list in mysql using PHP to radio buttons

This is my code for the selection list, i need to modify the code so instead of a drop-down select list, its a bunch of radio buttons (whithout hard coding).
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT studentID FROM student ";
$result = mysql_query($sql);
echo "<select name='studentID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['studentID'] . "'>" . $row['studentName'] . "</option>";
}
echo "</select>";
?>
Any help would be appreciated.
Try following
while ($row = mysql_fetch_array($result)) {
echo '<input type="radio" name="studentID" value="'.$row['studentID'].'"> '.$row['studentName'].'<br>';
}

Form values from dropdown list not passing

I created a drop down list that has been populated by the database and now I'm having trouble retrieving the data. Normally, I would know how to retrieve the value of the drop down list if I had to manually name the data, but in this case, I'm not quite sure how I would name it.
Here is my current code:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('host', 'root', 'password');
mysql_select_db ("db");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
<?php
$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];
if(isset($generate))
{
echo $material;
echo $locations;
}
?>
You're trying to capture value before the submit button is hit. Also, as Hanky pointed out you're using the wrong names while referring to select data. You should do this instead
if(isset($_POST['submit'])) // this code will run after the button is clicked
{
$material = $_POST['materials']; // and not material_name
$locations = $_POST['locations']; // and not location_name
echo $material;
echo $locations;
}
PS: You're following a very unsecure way of developing a web application. At the very least you need to switch to PDO and always escape the data.

Categories