7

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.

3 Answers3

10

Simply do:

$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
Gabriele Medeot
  • 153
  • 1
  • 10
4

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

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • I know this is old, but your 'spaghetti code' comment made me think you may be the right person to ask. I've been learning to code for a few years now in my spare time. Recently, I've gotten to a point where I feel I can start looking for employment and I'm building a portfolio. I have used a couple of frameworks in different projects and I'm wondering, other than showing off my ability to use it, is there any framework you'd say is worth using when the statement in your answer is the only php that I'll be using on the site? – Muckee Dec 05 '18 at 00:16
  • @JoshuaFlood I work mostly with (node)js nowadays I don't even have the IDE for PHP projects. If you wanted to ask which PHP frameworks are really good, then Symfony and Laravel are. But if I were you I would just check whether the actual framework has many stars on its github page and if so, then it is popular, so it is worth to mention. I think it is a plus point if you use git, composer, phpunit and some sort of TDD or BDD approach to develop. – inf3rno Dec 05 '18 at 05:41
  • @JoshuaFlood I see you use Slim. I liked that too. :-) – inf3rno Dec 05 '18 at 05:43
  • 1
    Thanks for the advice! Unit testing is something I'm still having to spend a lot of time on google to work with, so I guess once that is a bit clearer in my head then I'll know how to answer my own question a bit better. Yeah I was just looking for something to help me develop a simple API that just returns JSON and all of the obvious choices seemed far too heavy, but Slim is definitely the most elegant framework I've found and I love that it doesn't really force devs into using any particular set of dependencies. – Muckee Dec 06 '18 at 12:46
1

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?

echo_Me
  • 37,078
  • 5
  • 58
  • 78
tsknakamura
  • 183
  • 4