【问题标题】:sending a hidden post variable and a query string get variable to a page将隐藏的 post 变量和查询字符串 get 变量发送到页面
【发布时间】:2016-11-17 07:49:44
【问题描述】:

我有三个页面 start.html page2.php 和 final.php

start.html 获取一些表单数据变量(名字、姓氏、城市、州)并使用 POST 表单将其发送到 page2.php。

然后我想使用不同的方法将这些 post 变量发送到 final.php。 firstname 和 lastname 作为查询字符串(GET 变量),city 和 state 作为隐藏变量,通过 POST 传递到 page3。

我知道如何分别做每一项,但可以同时做这两项吗?

我试过的,这是最好的客人,在page2.php上:

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$city = $_POST['city'];
$state = $_POST['state'];

$url = "final.php?firstname=".$firstname."&lastname=".$lastname;


<form action="<?php echo $url; ?>" method="post">
    <input type="hidden" name ="city" value="<?php echo $city; ?>">
    <input type="hidden" name ="state" value="<?php echo $state; ?>">
    <input type="submit" value="next page">
</form>


?>

或标题(位置:$url);

也许使用href?

【问题讨论】:

  • 您只能单独执行POSTGET,并且一次只能执行一个。为什么你需要发送一些GET和一些POST
  • 可能重复,请检查:stackoverflow.com/questions/2749406/…
  • 上面的代码不符合你的要求吗? $_GET 数组应该用final.php 中的名字/姓氏填充,$_POST 数组也应该用城市和州比这个看起来......??
  • blinkydamo 这是一个演示/测试,通常我可能会同意你的看法
  • 由于某种原因这开始工作了,我之前遇到了一个错误,但是这段代码现在应该可以工作了

标签: php post get query-string


【解决方案1】:

您可以使用session 来执行此操作,作为您的问题,我使用get 传递了firstnamelastname,并使用sessions 传递其他值。

page2.php

<?php
session_start();

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$city = $_POST['city'];
$state = $_POST['state'];

$_SESSION['city'] = $city;
$_SESSION['state'] = $state;

$url = "final.php?firstname=".$firstname."&lastname=".$lastname;

?>

<a href="<?php echo $url; ?>">next page</a>

final.php

<?php
session_start();

echo $_GET['firstname'];
echo $_GET['lastname'];

echo $_SESSION['city'];
echo $_SESSION['state'];

?>

【讨论】:

    【解决方案2】:
    <?php
    
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    
        $url = "final.php?firstname=".$firstname."&lastname=".$lastname;
        ?>
    
        <form action="final.php" method="post">
            <input type="hidden" name ="city" value="<?php echo $city; ?>">
            <input type="hidden" name ="state" value="<?php echo $state; ?>">
            <a href="<?php echo $url; ?>">next page</a>
        </form>
    

    使用表单将城市和州发布到 final.php,并使用链接获取名字和姓氏。

    仅供参考:您可以按照@user3099298 代码使其更简单。

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 2013-08-18
      • 2016-01-17
      • 1970-01-01
      • 2017-07-20
      • 2015-04-18
      • 2014-04-25
      • 2016-08-22
      相关资源
      最近更新 更多