PHP: redirect one page after clicking submit

Is there a way to redirect to the same page using PHP. ??? I just need to reboot or upgrade gridview. I do not need to redirect to the site or link, I need to redirect it to the same page or the same form as main.php.

I already used header (), but it doesn’t work with me, and all I see is a link to a website.

Thanks.

+4
source share
3 answers

Here are two examples for redirecting to a form. Let's say your main.php file name

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

Is this the answer to your question?

-2
source

You have to redirect with the location header after each message, because otherwise, when the user clicks the refresh button, he will send the same form again ...

 <?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 the right thing, you should try the PHP framework instead of this type of spaghetti code ...

+2
source

Just do:

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

Source: https://habr.com/ru/post/1486405/


All Articles