Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
Philistyne Brigid Bellisima Avatar asked Jun 15 '13 16:06

Philistyne Brigid Bellisima


3 Answers

Simply do:

$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
like image 75
Gabriele Medeot Avatar answered Nov 04 '22 08:11

Gabriele Medeot


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...

like image 4
inf3rno Avatar answered Nov 04 '22 07:11

inf3rno


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?

like image 1
tsknakamura Avatar answered Nov 04 '22 07:11

tsknakamura