Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect to the same page with $_GET parameters in URL

i've got a question about redirecting. I put some parameters in my URL so I can use $_GET to get them out en them put them in a variable. Now, when I fill in my form wrong i get redirected but all the parameters are missing and they don't are in the variables anymore. Is there a way you can redirect to the same page with the same parameters?

url with parameters: http://localhost:8888/WD2/Week6/Project/flight_bestel.php?stoelnr=5&prijs=99.00&bestemming=Spanje&aankomst=%20Barcelona%20Airport

url after redirect: http://localhost:8888/WD2/Week6/Project/flight_bestel.php

Thanks

<?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' ); ?>
like image 557
bjorn Avatar asked Oct 18 '22 03:10

bjorn


1 Answers

So your question becomes..."How do I rebuild the original URL as the action for my form"?

One way to do that is to grab the query string and use it to rebuild your forms "action".

Something like... Grab your original query string

$query_string = '?'.$_SERVER['QUERY_STRING'];

And append it to your forms action

<form action= "<?php echo $_SERVER['PHP_SELF'] .$query_string; ?>" method="post" class="form-horizontal">

That should get your going in regards to that bit of the puzzle you are working on.

like image 199
TimBrownlaw Avatar answered Oct 21 '22 00:10

TimBrownlaw