I'm having issues with repopulating my input fields after being submitted if there is an error.
I've tried the simple <?php if(isset($_POST['value'])){echo $_POST['value'];} ?>
That doesn't work. And I'm thinking it's because of how I have my else statements set up. I am currently using get to repopulate my fields, and it's working but I'm not so sure it's the best practice.
Example of original code:
(form-check.php)
<?php
if(isset($_POST['firstname'])) {
$firstname = $_POST['firstname'];
// do something
} else {
header("Location: index.php?error=This field is required!");
}
?>
(index.php)
<form action="form-check.php" method="POST">
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])){echo htmlentities($_POST['firstname']);} ?>" />
<input type="submit" value="Submit" />
<?php if(isset($_GET['error'])){echo $_GET['error'];} ?>
Example of working code:
(form-check.php)
<?php
if(isset($_POST['firstname'])) {
$firstname = $_POST['firstname'];
// do something
} else {
header("Location: index.php?error=This field is required!&firstname=$firstname");
}
?>
(index.php)
<form action="form-check.php" method="POST">
<input type="text" name="firstname" value="<?php if(isset($_GET['firstname'])){echo htmlentities($_GET['firstname']);} ?>" />
<input type="submit" value="Submit" />
<?php if(isset($_GET['error'])){echo $_GET['error'];} ?>
I'm using my else statement and passing my values and error through the same GET method by using header().
Like I said it's working this way but I wanted to ask some opinions on it.