以下を使用して、ページのドロップダウン リストで選択した項目の値を取得しています。
$field = $_POST['dropdownlistname'];
ただし、エコーを使用してドロップダウン リストを作成すると、これは機能しません。以下が私のフォームです。
<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>
フォームが送信されると、次のクエリが 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");
?>
次に、newCustomer ページがリロードされ、クエリによって返されたすべての行を含むレンダリングされたドロップダウン リストが表示されます。
次に、ドロップダウン リストの項目の値を取得します。フォームを再送信する必要はありません。