How to echo selected option value. Not select name - php

<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>";

Related

How can I use the selected option from drop down menu to query my database ?

I am using PDO to interact with my database. How can I use the input a user selects to query my database. This is what I have so far. I store the selected option as a variable, but it actually is not being captured. What can I do to fix this?
echo <<<FORMSTART
<form name= "name" method= "post">
<select name = "name" onChange="document.topic_list.submit()">
<option value = "None">Select name</option>
FORMSTART;
$stmt = $pdo->query("SELECT DISTINCT name from users");
//populate drop down menu
while ($row = $stmt->fetch()) {
echo "<option value= '" . $row['name'] . "'>" . $row['name'] . "</option>";
}
//End form for drop down menu
echo <<<FORMEND
</select>
</form>
FORMEND;
$selected_name = $_POST["name"];
//This prints here but when I try to use this variable later it doesnt print anything
echo "$selected_name Selected";

Placing id/option value from dropdown selection into variable PHP, MySQL HTML

I have a dropdown list that is filled with some values from a MySQL table.
The issue i'm facing is that I cannot place the ID from the selected value into a variable.
This is my code up until now:
<?php
mysql_connect('localhost', 'confidential', 'confidential');
mysql_select_db('mydb');
$sql = "SELECT zone_naam FROM zone";
$result = mysql_query($sql);
echo "<select name='zone_1' id='zone_1'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . " </option>";
}
echo "</select>";
?>
I would think that I could get the value from $row['idzone'] by using the code below.
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
I've tried several things, but I cannot come to a solution.
If I want to do the same thing in HTML with self set data like below it always works, but whenever I want to use PHP for this purpose I seem to fail.
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
I hope you all understand what I mean and can help me to find the cause of this problem.
Best regards,
Rudibwoyyy
#Rudi
Then David is correct. If you submit the HTML from your post
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
you can use, according the select name "zone", the variable $_POST['zone'], assuming you are using <form method="post">, (otherwise it's $_GET['zone']) will contain the value (1, 2 or 3 respectively) of the selected entry.
If this doesn't work for you, check if the generated HTML Code hat the correct name.
Below is my code that works!! Thanks for all the tips
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('confidential');
$sql = "SELECT * FROM zone";
$result = mysql_query($sql);
echo "<form type='submit' class='form-inline' action='' method='POST'>
<select name='zone_1' id='zone_1' class='form-control mb-2 mr-sm-2 mb-sm-
0'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<form type=\"submit\" method=\"POST\"> ";
echo " <select name=\"richting\" class='form-control mb-2 mr-sm-2 mb-sm-0'> ";
echo " <option value=\"<-->\"> <--> </option> ";
echo " <option value=\"<--\"> <-- </option> ";
echo " <option value=\"-->\"> --> </option> ";
echo "</select> ";
$result2 = mysql_query($sql);
echo "<select name='zone_2' class='form-control mb-2 mr-sm-2 mb-sm-0'>";
while ($row = mysql_fetch_array($result2)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<br><br>";
echo "<input type='submit' value='Submit' name='submit' id='submit' class='btn btn-primary btn-sm'>";
echo "</form>";
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
?>

Mapping values in dropdown 2 dependent on value selected in dropdwon 1 via php mysql

I am fresher in php & mysql.
I have a from where there are 2 dropdwon's. The value in dropdwon are coming via mysql. But now i am not able to get the value in 2nd dropdown dependent on value selected in 1st dropdown.
Here is the code i have tried so far.
Please Help!
Thank You
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn=mysqli_connect('localhost','root','');
$db="sample";
mysqli_select_db($conn,$db);
$sql="SELECT DISTINCT(Site) FROM `bom`";
$result=mysqli_query($conn,$sql);
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while($row=mysqli_fetch_array($result))
{
echo "<option value='".$row['Site']."'>".$row['Site']."</option>";
}
echo "</select>";
$sql1="SELECT BOM Desc FROM `bom` where Site= ";
$result1=mysqli_query($conn,$sql1);
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while($row1=mysqli_fetch_array($result1))
{
echo "<option value='".$row1['BOM Desc']."'>".$row1['BOM Desc']."</option>";
}
echo "</select>";
?>
</body>
</html>
Hi Your fixed code here:
<html>
<head><title>Sheet</title></head>
<body>
<h2 align="center">SKU Selection</h2>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$db = "sample";
mysqli_select_db($conn, $db);
$sql = "SELECT DISTINCT(Site) FROM `bom`";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); // add error show info
echo "Site Name :";
echo "<select name='Site'>";
echo "<option value='0'>Select Site</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Site'] . "'>" . $row['Site'] . "</option>";
}
echo "</select>";
$sql1 = "SELECT BOM Desc FROM `bom` where Site IS NULL "; // change the for null site
$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn)); // add error show info
echo "<br>";
echo "SKU :";
echo "<select name='SKU'>";
while ($row1 = mysqli_fetch_array($result1)) {
echo "<option value='" . $row1['BOM Desc'] . "'>" . $row1['BOM Desc'] . "</option>";
}
echo "</select>";
?>
</body>
</html>
And if you need load on second select php only cant handle this because you must give time to user for check first select.
I think the better way is using Ajax request for second:
Auto Load Second Dropdown using AJAX
I suggest to use in both <select> the clause selected to show the selected <option>.
Then, in the first <select> add the on change event to launch a page refresh so that:
The selected entry is recognised as selected
The SQL for the second drop-down can be filtered on the selected entry in first dropdown
The first select should appear then like this:
<select onChange='if(options[selectedIndex].value) { location=options[selectedIndex].value;}' size='1'>
<option value='?site=Plant1'>Plant ONE</option>
<option value='?site=Plant2' selected>Plant 2</option>
</select>
When Plant 2 is selected, the page will be refreshed with the URL containing the parameter &site=Plant2 which you can read in the variable $_REQUEST['site'] to be used in the 2nd SQL query

PHP select option using value from MySQL

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>

Selected Value for MySQL generated Drop Down List

I have a mysql generate dropdown list..need to add in a pre selected option such as "Please Choose Availability". Not too sure how to do this as the drop down content is generated from a database
<?php
$sql = "SELECT DISTINCT availability FROM properties";
$result = mysql_query($sql);
echo "<select name='property'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
?>
if you want static first value for dropdown then you can do it like this.
echo "<select name='property'><option value=''>Please Choose Availability </option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['availability']. "'>" . $row['availability'] ."</option>";
}
echo "</select>";
echo "<select name='property'><option value=''>Please Choose Availability</option>";

Categories