首页 > 解决方案 > 将 post 方法数据传递到导航栏而不标签

问题描述

我在导航栏中有一些页面链接在一起。我想使用 post 在它们之间传递信息而不使用tag<intput method='POST'.....>. 例如,我有带有页面的导航栏:姓名年龄地址。

在页面 Name.php

$query="SELECT code,name FROM people WHERE code=123;
        $query_result = pg_query($conn1, $query);
        echo '<form action=\"Age.php\" method=\"GET\">' ;
        echo 'Reparto:<br>';
        while($row = pg_fetch_assoc($query_result)) { 
                echo '<a href="Age.php code='.$row["code"].'&name='.$row["name"].'">'.$row["name"]. '</a><br>';
                
            };
               echo '</form>';

现在我想在导航栏中单击 Age 并在 url 中,我看到使用 post 方法传递的数据。我的解决方案不起作用。我能怎么做?

标签: php

解决方案


尝试像这样使用http_build_query()

$query="SELECT code,name FROM people WHERE code=123;
$query_result = pg_query($conn1, $query);
//echo '<form action=\"Age.php\" method=\"GET\">' ;
echo 'Reparto:<br>';
while($row = pg_fetch_assoc($query_result)) {
    echo '<a href=\"/Age.php?' . http_build_query($row) . '\">' . $row["name"] . '</a><br>';
};
//echo '</form>';

由于这是一个锚点,它会将您重定向到具有所需key=>values对的特定地址。单击锚点后,您可以使用和获取Age.php脚本中的所有参数。$_GET['name']$_GET['code']

这也可以这样做:

<?php
$query="SELECT code,name FROM people WHERE code=123;
$query_result = pg_query($conn1, $query);
?>
Reparto:<br>
<?php while($row = pg_fetch_assoc($query_result)): ?>
    <a href="/Age.php?<?= http_build_query($row) ?>"><?= $row["name"] ?></a><br>
<?php endwhile;?>

这是更干净的变体。


推荐阅读