如何将php变量从隐藏的输入值传递给php脚本?

时间:2017-12-05 10:13:24

标签: php html mysql

我在表格中有这个表格:

while($row=mysqli_fetch_array($result)){
    echo "<tr>";
        echo "<td>".$row['first_name'] . "</td>";
        echo "<td>" . $row['last_name'] . "</td>";
        <td>
            <form action="send_notification.php" method="POST">
                <input type="hidden" name="id" value="<?php echo $row['first_name']; ?>">
                <input type="submit" name="submit" value="request">
            </form>
       </td>

如果我点击它,我会在每行旁边有一个名为“请求”的按钮 将php变量$row['first_name']传递给另一个PHP脚本。

我该怎么做? 我试图在php文件send_notification.php中执行此操作:

<?php
session_start();
require("connect.php");

$id=$_GET['first_name'];

echo $id;
?>

但它给了我这个错误:

  

注意:未定义索引:第5行路径/ send_notification.php中的first_name

更新一次:

我将$_GET['first_name']更改为$_POST['id'],输出没有出错但是它是一个空白页面。它不应该回应$ id吗?

3 个答案:

答案 0 :(得分:0)

您指的是错误的变量名称。

通过POST / GET发送数据时,name属性将被视为变量名称。

此外,您的表单方法设置为POST,您正在检查$_GET数组。

尝试获取如下所述的值。

$id = $_POST['id'];

更改了代码

<?php
session_start();
require("connect.php");

$id = $_POST['id'];

echo $id;

?>

答案 1 :(得分:0)

<form action="send_notification.php" method="POST">

这里你指定了你希望用POST传递的值

 $id=$_GET['first_name'];

在这里你只是要求它在GET超全局中,现在应该很明显,使用$ _POST,你的隐藏输入的名称也是id,你要求名字

但我只是想知道,你的php第5行的td是不是缺少一个echo或一个关闭的php标签?这是一种奇怪的做事方式,似乎你将php回声与包含php标签的html混合

答案 2 :(得分:0)

将隐藏的输入名称replace id更改为first_name。

while($row=mysqli_fetch_array($result)){
echo "<tr>";

 echo "<td>".$row['first_name'] . "</td>";
 echo "<td>" . $row['last_name'] . "</td>";
     <td>
                    <form action="send_notification.php" method="POST">
                        <input type="hidden" name="first_name" value="<?php echo $row['first_name']; ?>">
                        <input type="submit" name="submit" value="request">
                    </form>
                </td>