簡體   English   中英

PHP/MYSQLI 如何從表單內的 while 循環返回一個變量,以便在該表單的操作/方法中的 $_GET 請求中使用?

[英]PHP/MYSQLI how can I return a variable from a while loop inside a form to use in a $_GET request in that form's action/method?

我有一個 html 表單,它使用 PHP while 循環從 ZF7C7306FBE23983B7CZ 數據庫中填充 select 菜單。 代碼如下;

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1 ";
$result = mysqli_query($conn, $query);

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients">
        <option> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option>'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>'; 

以上工作,表單填充了所需的客戶記錄。 但是,然后我希望使用這些數據鏈接到我嘗試使用的單個客戶端頁面

echo '&emsp;<a href="selected_client.php?id='.$row['id'].'" onClick="this.form.submit()">Go!</a>';

這不起作用,所以我嘗試將表單操作設置為

action="selected_client.php?id='.$row['id'].'"

使用方法為“GET”,這也不起作用。 正如您在查詢中看到的那樣,我也在從表中選擇“id”,因此我可以稍后調用數組的那部分,我只是不需要或不想回顯它。 使用表單操作方法,未設置 $row['id'] 變量,使用 onClick 方法,我收到“嘗試訪問 null 類型值的數組偏移量”錯誤。

我想在表單動作發生之前需要設置變量,但我想不出如果不移動 while 循環或至少從中返回一個值,我將如何做到這一點。 是否可以在提交時調用表單方法和操作,而不是在表單頂部? 那會有什么不同嗎?

編輯; 根據要求,我嘗試對表單使用隱藏方法

echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" value="'.$row['id'].'">';

仍然返回 null 類型錯誤的值。

編輯#2 - 所有代碼內聯。

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1 ";
$result = mysqli_query($conn, $query);
echo  '<form action="selected_client.php" method="get">
    <fieldset>';

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients">
        <option> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option>'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>';
echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" value="'.$row['id'].'">';

echo '</fieldset></form>';

添加 onChange 事件並將選擇的值傳遞給某個 function,在該 function 中使用該選擇的值設置隱藏字段。現在您可以提交表單,並且可以接收您選擇的值。以下是解決方案。

$query = "SELECT on_programme, last_name, first_name, middle_name, id FROM constant_client WHERE on_programme=1";
$result = mysqli_query($conn, $query);
echo  '<form action="selected_client.php" method="get">
    <fieldset>';

echo '<p><label for="current_clients">Select Client by name: </label>
        <select id="current_clients" name="current_clients" onChange="setId(this.value);">
        <option value=""> -- Select Client -- </option>';

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          echo '<option value="'.$row['id'].'">'.ucwords($row['last_name']).', '.ucwords($row['first_name']).' '.ucwords($row['middle_name']).'</option>';
        }
echo '</select>';
echo '<input type="submit" name="submit" value="Go!">
<input type="hidden" name="id" id="hidden_field" value="">';

echo '</fieldset></form>';

添加此腳本以在隱藏字段中設置值。

<script>
function setId(rowValue){
   document.getElementById("hidden_field").value=rowValue;
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM