php - Registration form is not registering users

I want to validate a registration form and send an email to the client once registration is successful. My form is being validated but once I click the register button, literally nothing happens, I don't get an error message, or redirection to the login page and neither is the user created.
<?php
include_once(session_start());
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'] ;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// redirect back and display error
if (empty($_POST['email'])) {
$session_error= 'Please enter your email';
} elseif ($_POST['email']){
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$session_error= 'Invalid Email Format';
}
}else{
$email = test_input($_POST['email']);
}
if (empty($_POST['last_name'])) {
$session_error = 'Last Name should be filled';
} elseif ($_POST['last_name']) {
if (!preg_match('/^[a-zA-Z ]+$/', $last_name)) {
$session_error = 'last Name can only contain letters and white spaces';
}
} else {
$last_name = test_input($_POST['last_name']);
}
if (empty($_POST['first_name'])) {
$session_error = 'First Name should be filled';
} elseif ($_POST['first_name']) {
if (!preg_match('/^[a-zA-Z ]+$/', $first_name)) {
$session_error = 'First Name can only contain letters and white spaces';
}
} else {
$first_name = test_input($_POST['first_name']);
}
$_SESSION["error"] = $session_error;
header("Location: register.php ");
}else{
// count all users
$allUsers = scandir("db/users/");
$countAllUsers = count($allUsers);
$newUserId = ($countAllUsers -2) +1;
// assign ID to the new user
$userObject =[
'id' => $newUserId,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT ), // password hashing
];
// check if user already exists
// assign the next ID to the new user
// count($users =>2, next should then be ID 3
for($counter = 0; $counter < $countAllUsers; $counter++) {
$currentUser = $allUsers[$counter];
if($currentUser == $email . ".json"){
$_SESSION["error"] = "User already exists " . $first_name;
header("Location: register.php");
die();
}
}
header("Location: login.php");
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
}
My register.php:
<body>
<?php include_once('lib/header.php');
if(isset($_SESSION['loggedIn']) && !empty ($_SESSION['loggedIn'])){
// redirect to dashboard
header("Location: dashboard.php");
}
?>
<h3><strong>Register</strong></h3>
<form method="POST" action="processRegister.php">
<p>
<?php
if(isset($_SESSION['error']) && !empty($_SESSION['error'])){
echo "<span style='color:red'> " . $_SESSION['error'] . "</span>";
session_destroy();
}
?>
</p>
<p>
<label>First Name</label><br/>
<input
<?php
if(isset($_SESSION['first_name'])) {
echo "value=" . $_SESSION['first_name'];
}
?>
type="text"name="first_name" placeholder="First Name" /></p>
<p> <label>Last Name</label><br/>
<input
<?php
if(isset($_SESSION['last_name'])) {
echo "value=" . $_SESSION['last_name'];
}
?>
type="text"name="last_name" placeholder="Last Name" /></p>
<p> <label>Email</label><br/>
<input <?php
if(isset($_SESSION['email'])) {
echo "value=" . $_SESSION['email'];
}
?> type="text"name="email" placeholder="Email" /> </p>
<p> <label>Password</label><br/>
<input type="password" name="password" placeholder="Password" /> </p>
<p> <button type="submit">Register</button> </p>
</form>
<?php include('lib/footer.php'); ?>
</body>
</html>
Answer
Solution:
You have to add a name attribut value to your submit input element. For example:
<input type="submit" name="submit-register" value="Register">
And to verif in your PHP script if submit has been clicked:
if (isset($_POST['submit-register'])) {
// Do you stuff here
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.