I'm trying to post values to 2.php
, when the inputs are null
it will go back to 1.php
and then display an error message, the URL changed to 1.php/?error=1
.
The problem is that when I accidentally click the submit button twice, the URL changes to /1.php/2.php
, any idea?
1.php
:
<?php
if(isset($_GET['error']) && $_GET['error'] == 1)
{
echo "Error";
}
?>
<form method="Post" action="2.php">
<input type="text" name="firstname">
<input type="text" name="secondname">
<input type="submit" name="submit">
</form>
?>
2.php
:
if(isset($_POST["submit"]))
{
$first = $_POST["firstname"];
$second = $_POST["secondname"];
if($first == "" || $second == "")
{
header("Location: 1.php/?error=1");
die();
}
else
{
echo $first;
echo $second;
}
}
A) Use a BASE tag in your HTML
<head>
<base href="http://127.0.0.1/">
or
B) prepend a slash to each link
<form action="/2.php">
or
C) disable the button after the first click
<script>
document.forms[0].addEventListener( 'submit', function() {
var submitButton = document.forms[0].querySelector( '[type="submit"]' );
submitButton.setAttribute( 'disabled', 'disabled' );
}, false );
<script>
…or some combination of those.
Amend the redirect code to;
header("Location: ./1.php?error=1");