Pooja Kshatriya Pooja Kshatriya - 1 month ago 7
Javascript Question

How to get value from dynamically filled drop down on button click in php

I am stuck in getting value from drop down. Drop down is dynamically filled from sql server database.
Dropdown 1 displays product name and it is dynamically filled.
Dropdown 2 displays environment name and it is filled by HTML.

I am getting value of environment but not product.
Please help me. Thanks

Here is the code:

<form action="" method="post">

//Dropdown 1
<p>Product Name:
<select name="productname">
<option value="">Select</option>

<?php
if( $conn )
{
$sql_dd = "SELECT ProductName from Product";
$stmt = sqlsrv_query( $conn, $sql_dd );
if( $stmt === false) {die( print_r( sqlsrv_errors(), true) );}
$rows = sqlsrv_has_rows( $stmt );
if ($rows === false)
echo "There is no data. <br />";
else
{ while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
echo "<option value=''>".$row['ProductName']."</option>";
}
}
?>

//Dropdown 2
Client Type:
<select name="environment" style="width: 10%;">
<option value="">Select</option>
<option value="en1">en1</option>
<option value="en2">en2</option>
<option value="en3">en3</option>
</select>&emsp;
<input type="submit" class="theme-btn" value="Search" name="submit"/>

<?php
if(isset($_POST['submit']) )
{
$productname = $_POST['productname'];
$environment= $_POST['environment'];

echo "productname: ".$productname." environment: ".$environment;
}?>

Answer

The value is not added this:

echo "<option value=''>".$row['ProductName']."</option>";

should be

echo "<option value='" . $row['ProductName'] . "'>". $row['ProductName'] ."</option>";