PHP: Redirect same page after click the submit button

Is there a way to redirects to the same page using PHP.??? I just need to reload or refresh the gridview. I don't need to redirect to a website or link, I need is redirect it to the same page or same form like main.php.

I already used the header(), but its not working to me and all I see is linking in the website.

Thanks.

Upvotes: 7

Views: 59108

Answers (3)

Gabriele Medeot
Gabriele Medeot

Reputation: 153

Simply do:

$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");

Upvotes: 10

tsknakamura
tsknakamura

Reputation: 193

Here are two example to redirect to a form. Lets say that your filename is main.php

<form action="main.php">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form> 

Or you can use this

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form> 

Did that answer your question?

Upvotes: 1

inf3rno
inf3rno

Reputation: 26129

You should redirect with a location header after every post, because otherwise when the user presses the refresh button, it will send again the same form...

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    file_put_contents('data.txt', $_POST['data']);
    header('location: ' . $_SERVER['PHP_SELF']);
} else {
    header('content-type: text/html; charset=utf-8');
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
          enctype="application/x-www-form-urlencoded; charset=utf-8">
        <input name="data" type="text" value="<?php echo file_get_contents('data.txt'); ?>"/>
        <button>küldés</button>
    </form>
<?php
}

Btw. if you want to do proper work, you should try out a php framework instead of this kind of spaghetti code...

Upvotes: 4

Related Questions