PHP and HTML form

Hi,

I am working on a website project and building an enquire form. I have MAMP on my machine for local server. The form doesn’t seem to send for some reason. I have tried to check everything, but struggling to find why it’s not working as it should.

This is my HTML code.

<main>
  <h2>Enquire Form</h2>
    <p>Welcome, please add your details and I will get back to you as soon as possible.</p>
    <form method="post" name="enquire-form" action="form.php" enctype="multipart/form-data">
      <fieldset>
         <label for="first-name">First Name<input id="first-name" name="first-name" type="text" placeholder="First name" required /><span class="error">* <?php echo $fnameErr;?></span></label>
            <label for="last-name">Last Name<input id="last-name" name="last-name" type="text" placeholder="Last name"required /><span class="error">* <?php echo $lnameErr;?></span></label><br/>
            <label for="email-address">Email<input id="email" name="email" type="email" placeholder="Your e-mail" required /><span class="error">* <?php echo $emailErr;?></span></label><br />
            <label for="description">Please describe your enquiry:<br/><textarea id="description" name="description" rows="5" cols="50" placeholder="I would like to find out..."></textarea><span class="error"><?php echo $descriptionErr;?></span></label>
      </fieldset>
     <input type="submit" name="submit-btn" value="Submit Enquiry" class="btn btn-success btn-lg btn-block"></input>
    </form>
</main>

And this is the PHP code.

var_dump($_POST);

$fnameErr = $lnameErr = $emailErr = $descriptionErr = "";
$fname = $lname = $email = $description = "";
$message = "Success! You have submitted the form and it will be received shortly.";
$request_method = strtoupper(getenv('REQUEST_METHOD'));$http_methods = array('GET', 'POST', 'HEAD');

$fname = $_POST['first-name'];
$lname = $_POST['last-name'];
$email = $_POST['email'];
$description = $_POST['description'];

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if (in_array($request_method, $http_methods)) {
    if($request_method == "POST") {
        //this checks that the variable is not empty//
        if (empty($_POST["first-name"])) {
            $fnameErr = "First name is required";
        } else {
            $fname = test_input($_POST["first-name"]);
            //check name format - only letters and whitespace
            if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {
                $fnameErr = "Only letters and white space allowed";
           }
        }
    
        if (empty($_POST["last-name"])) {
            $lnameErr = "Last name is required";
        } else {
            $lname = test_input($_POST["last-name"]);
            //check name format - only letters and whitespace
            if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {
                $lnameErr = "Only letters and white space allowed";
           }
        } 
    
        if (empty($_POST["email"])) {
            $emailErr = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            //check the email format
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $emailErr = "Invalid email format";
            }
        } 
    
        if (empty($_POST["description"])) {
            $descriptionErr = "Enquiry is required";
        } else {
            $description = test_input($_POST["description"]);
        } 
    }    
}

if (isset($_POST['submit'])) {
    $fname = $_POST['first-name'];
    $lname = $_POST['last-name'];
    $subject = $_POST['subject'];
    $mailFrom = $_POST['mail'];
    $message = $_POST['description'];

    $mailTo = "email-adress";
    $headers = "From: .$mailFrom \n";
    $txt = "You have received an email from ".$name. ".\n\n".$message;

    echo $message;

    mail($mailTo, $subject, $txt, $headers);
    header("Location: form.php?mailsend");//to get a confirmation that the mail has been sent//
} else {
    echo 'The form has not been submitted. Please check your entry.';
}

I enabled var_dump to see what the form is sending, and this is what it brings up:

array(5) { [“first-name”]=> string(4) “angn” [“last-name”]=> string(5) “agngi” [“email”]=> string(16) “aigimgi@agjimaig” [“description”]=> string(15) “msigamigmaigmig” [“submit-btn”]=> string(14) “Submit Enquiry” }

I don’t think it’s anything helpful as it’s just showing the variables but i may be wrong.

This is my first time doing backend development, so I am likely missing something simple. Thank you for any help at all.

You can also just use fabform a form backend service to collect form submissions.