Home > other >  404 Not Found Error when submitting form on website
404 Not Found Error when submitting form on website

Time:04-27

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” >
        <div >
          <label for="first-name" >First Name</label>
          <input type="text" name="first-name"  id="first-name" placeholder="John" required>
        </div>
        <div >
          <label for="last-name" >Last Name</label>
          <input type="text" name="last-name"  id="last-name" placeholder="Smith" required>
        </div>
        <div >
          <label for="email" >Email</label>
          <input type="email" name="email"  id="email-address" required>
        </div>
        <div >
          <label for="notes" >Notes</label>
          <textarea  name="notes" id="notes" rows="4" placeholder="Include any additional information"></textarea>
        </div>
        <div >
          <button type="submit" >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";
  }
}
?>

CodePudding user response:

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” >

to this:

<form name="contact_form" action="contact-form.php" method="POST" >

Now your browser tries to GET "”contact-form.php”" 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).

  • Related