繁体   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