I have been using the following to get the value of the selected item in a dropdown list on my pages.
$field = $_POST['dropdownlistname'];
However when I create a dropdown list through echoing this doesn't work. The following is my form.
<form name="searchForm" action="newCustomerSearchform.php" method="post">
<label><span></span> <input type="text" name="searchDB" /></label>
<button type="submit" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<?php
echo '<select name="customers">';
foreach ($_SESSION['names'] as $option => $value) {
echo '<option value='.$value['ID'].'>'.$value['First_Name'].' '.$value['Surname'].'</option>';
}
echo '</select>';
$test = $_POST['customers'];
echo $test;
</form>
Once the form is submitted the following query is run on newCustomerSearch.php
<?php
include 'newCustomer.php';
connect('final');
$searchtext = $_POST['searchDB'];
$searchtext = htmlspecialchars($searchtext); // stop HTML charkacters
$searchtext = mysql_real_escape_string($searchtext); //stop SQL injection
$query = "SELECT * FROM customer WHERE First_Name LIKE '%$searchtext%'";
$data = mysql_query($query) or die(mysql_error());
$Customers = array();
$colNames = array();
while($row = mysql_fetch_assoc($data)){// puts data from database into array, loops until no more
$Customers[] = $row;
}
$anymatches = mysql_num_rows($data); //checks if the querys returned any results
if ($anymatches != 0) {
$_SESSION['names']=$Customers;
$colNames = array_keys(reset($Customers));
}
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
header("location: newCustomer.php");
?>
The newCustomer page then reloads with the rendered drop down list contaning all of the rows returned by the query.
I then want to get the value of an item in the drop down list. without resubmitting the form.