I have done a simple form like this:
index.php:
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<?php
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
// handle missing field
} else {
$name = $_POST["name"];
}
}
if($name != '') {
header('location: http://localhost/new_page.php');
exit();
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<span class="label">Name</span>
<input type="text" name="name">
<button><span>Validate</span></button>
</form>
</body>
</html>
I would like the following behaviour:
- staying on the form if the name field is not set by the user
- going to new_page.php if name field is set
I have the following error right now: if I set the name, I am redirected to a blank.php page. Can someone help me on this ? Or at least give me some tips to debug this as I am not really a PHP specialist.
Thank you !