0

So I've been trying to program a button that redirects you to another web page for quite some time now, and this is the Best solution I've come up with:

<form action="#" method="post">
<input type="submit" name="submit" value="LLL" />
</form>
<?php
$redirect_after_login='index.php';

if(isset($_POST['submit'])){
    echo 'test';  //this was just a test
    header('Location: ' . $redirect_after_login);
    exit;
}
?>

So basically, what happens is that when I press the button I do not get redirected to the other page, but the echo command does emit test. Any ideas on how I could fix this? (Sorry for any gramar mistakes, english is not my first language, and if this is very obvious, I'm new to php :D)

Canbach
  • 97
  • 8
  • 1
    This does not work, because `header()` needs to be called prior to any output - see [https://www.php.net/manual/en/function.header.php](https://www.php.net/manual/en/function.header.php) – Jirka Hrazdil Dec 30 '19 at 16:45
  • Why not just use an anchor `something` then style it with CSS? Seems easier. You could have a check on the second page to validate the request. – user3783243 Dec 30 '19 at 16:51

1 Answers1

0

You are most likely not actually executing the form through PHP. You're being redirected to the anchor # (action="#"), which does not result in a reload/re-request of the page, but only jumping to the top of the page. The action is mandatory to receive and process the forms data.

So try to change your forms action attribute to the filename of the PHP-File or just leave it blank to send the form data to "the same page".

<form action="" method="post">
<input type="submit" name="submit" value="LLL" />
</form>

Also you should do any processing first, and output last. Move the form to the bottom of the file, because header() can't send its headers when something has been output before (your form in this case).