[英]Need to change date variable with drop down menu selection dynamically in PHP
[英]PHP selection of drop down menu not storing in variable
我有一個簡單的表,其中工作人員姓名存儲在f_operator_name列中。 我有一個php形式的下拉菜單,其中有這些工作人員姓名可供選擇。 這是相關代碼的片段:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
當有人選擇職員名稱時,我希望將其存儲在變量中。 我正在底部測試提交表單,該表單旨在打印出已選擇的名稱(在變量運算符中),但不打印任何內容。 誰能看到任何問題嗎?
編輯***這是從Barmar得到一些建議后的更新代碼,其中還包含變量信息,由於某些原因,echo語句仍然無法正常工作:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
您不能將<select>
放在<h2>
,然后將<option>
和</select>
放在它外面。 HTML元素必須正確嵌套,並且<option>
必須在<select>
內部。
更改為:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$_POST
的索引必須與<select>
的名稱匹配,因此$_POST['operator']
應為$_POST['f_operator_id']
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.