I am creating a contact form that uses bootstrap HTML framework and then PHP to validate the form fields and send the data to an email. I just asked a question about why I was getting a 404 Error when I hit the submit button which was solved by changing out some of the wrong quotation marks I had around my action and method elements. After changing those, and fixing a couple of PHP code errors, my form now takes me to a blank white screen on button submit (address bar reads myexamplewebsite.com/php/contact-form.php
), and I have tried finding a solution to this issue as it seems pretty common but was not successful in solving my own problem. For further information, I am hosting my site through NameCheap and have been using the cPanel to upload all web assets. I currently have my index file saved as HTML, but after watching many youtube videos wonder if I need to change it to a .php extension for the form to work. Lastly, before I post my code, I have not tried adding the invalid_class_name to my code yet.
My HTML:
<form name="contact_form" action="php/contact-form.php" method="POST" class="row g-4">
<div class="col-md-6">
<label for="first-name" class="form-label">First Name</label>
<input type="text" name="first-name" class="form-control" id="first-name" placeholder="John" required>
</div>
<div class="col-md-6">
<label for="last-name" class="form-label">Last Name</label>
<input type="text" name="last-name" class="form-control" id="last-name" placeholder="Smith" required>
</div>
<div class="col-md-6">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" id="email-address" required>
</div>
<div class="col-md-12">
<label for="notes" class="form-label">Notes</label>
<textarea class="form-control" name="notes" id="notes" rows="4" placeholder="Include any additional information"></textarea>
</div>
<div class="col-12">
<button id="btnsubmit" type="submit" name="btnsubmit" class="pcs-cta-button form-submit-button">Submit</button>
</div>
</form>
My PHP:
<?php
$message_sent = false;
if (isset($_POST['email']) && $_POST['email'] != '') {
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
//submit the form
$userFirstName = $_POST['first-name'];
$userLastName = $_POST['last-name'];
$userEmail = $_POST['email'];
$message = $_POST['notes'];
$messageSubject = "New Concrete Job";
$to = "myemail@gmail.com";
$body = "";
$body .= "From: ".$userFirstName." ".$userLastName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Notes: ".$message. "\r\n";
mail($to,$messageSubject, $body);
$message_sent = true;
}
else {
$invalid_class_name = "form-invalid";
}
}
?>
If anyone has any tips for getting forms to work with NameCheap/cPanel I would appreciate the advice!