Tristan Huyck
Tristan Huyck

Reputation: 25

404 Not Found Error when submitting form on website

I am trying to build a contact form that uses bootstrap form HTML structure and PHP to control the backend validation and send the form information to my email.

Right now whenever I hit the submit button, I get a 404 Not Found Error which I assume has to do with the HTML not communicating the correct way with my PHP code. My website is 100% live (being hosted through NameCheap), and have been using cPanel's File Manager to upload all HTML, CSS, Image, and PHP files. I followed a Youtube tutorial on what the PHP should look like to validate my form's data and send it to my email.

I hadn't tried the $invalid_class_name aspect of the code yet, as I was trying to get the data to pass successfully first. I have the contact-form.php file in the same location as my HTML file, but I am wondering if I need to save my index.html file as a PHP file to make this work or an extra PHP plug-in on my website.

I have this PHP code included in HTML right above my form code

<?php 
      if($message_sent);
      ?>
      
        <h3>Thanks,we'll be in touch</h3>
      
      <?php
      else:
      ?>

HTML

<form name=”contact_form” action=”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 type="submit" class="pcs-cta-button form-submit-button">Submit</button>
        </div>
      </form>

      
        
     

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'];

    $to = "[email protected]";
    $body = "";

    $body .= "From: ".$userFirstName." ".$userLastName "\r\n";
    $body .= "Email: ".$userFirstName. "\r\n";
    $body .= "Notes: ".$message. "\r\n";

    mail($to, $messageSubject, $body);
    $message_sent = true;
  }
  else {
    $invalid_class_name = "form-invalid";
  }
}
?>

Upvotes: 0

Views: 1919

Answers (1)

Flash Thunder
Flash Thunder

Reputation: 12036

This is because you are using instead of " in action of the form (and generally form attributes)

Change this:

<form name=”contact_form” action=”contact-form.php” method=”POST” class="row g-4">

to this:

<form name="contact_form" action="contact-form.php" method="POST" class="row g-4">

Now your browser tries to GET "%E2%80%9Dcontact-form.php%E2%80%9D" instead of "contact-form.php". And yes, it tries GET (default method) not POST, as your form method is invalid as well (same reasons - wrong quotation marks).

Upvotes: 2

Related Questions