我一直在使用以下命令在我的頁面上的下拉列表中獲取所選項目的值。如何獲取通過echo創建的下拉列表的選定值?
$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頁面所呈現的下拉列表contaning全部由查詢返回的行的重新加載運行。
然後我想要在下拉列表中獲取項目的值。無需重新提交表單。
_dynamically_是什麼意思? POST請求會發送到包含您的代碼的PHP頁面嗎? – kero
select語句的名稱不是動態的。所以它不應該和使用靜態選擇列表有什麼不同? –
當你試圖讀取'$ _POST ['customers']'的值時,你是否發佈了表單? –