如何使用 URL 中的 $_GET 参数重定向到同一页面

bjo*_*orn 5 html php api redirect get

我有一个关于重定向的问题。我在我的 URL 中放置了一些参数,因此我可以使用 $_GET 将它们取出并将它们放入变量中。现在,当我填写错误时,我被重定向,但所有参数都丢失了,它们不再出现在变量中。有没有办法可以使用相同的参数重定向到同一页面?

带参数的网址:http://localhost:8888/WD2/Week6/Project/flight_bestel.php?stoelnr=5&prijs=99.00&bestemming=Spanje&aankomst=%20Barcelona%20Airport

重定向后的网址:http://localhost:8888/WD2/Week6/Project/flight_bestel.php

谢谢

<?php
session_start();
include_once ("scripts/config.php");
include_once ("scripts/api.php");

$stoelNr = $_GET['stoelnr'];
$prijs = $_GET['prijs'];
$bestemming = $_GET['bestemming'];
$aankomst = $_GET['aankomst'];


if($_SERVER['REQUEST_METHOD'] === 'POST') {

}else{};

?>

<?php require_once( 'views/shared/_header.inc' ); ?>
<body>
<header>
    <?php include( 'views/shared/_nav.inc' ); ?>
</header>

<main>

    <div id="contents" class="container">
        <section id="summary">
        
            <form action= "<?php echo $_SERVER['PHP_SELF']; ?>"     method="post" class="form-horizontal">
            
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-info"     name="bestel">Bestel nu!</button>
                    </div>
                </div>
            </form>

        </section>
    </div>
</main>
<?php require_once( 'views/shared/_footer.inc' ); ?>
Run Code Online (Sandbox Code Playgroud)

Tim*_*law 4

所以你的问题变成了......“如何重建原始 URL 作为我的表单的操作”?

一种方法是获取查询字符串并使用它来重建表单“操作”。

就像......抓住你的原始查询字符串

$query_string = '?'.$_SERVER['QUERY_STRING'];
Run Code Online (Sandbox Code Playgroud)

并将其附加到您的表单操作中

<form action= "<?php echo $_SERVER['PHP_SELF'] .$query_string; ?>" method="post" class="form-horizontal">
Run Code Online (Sandbox Code Playgroud)

这应该能让你继续解决你正在解决的难题。