Please help | PHP 404 error when hitting submit button

kelloggs_cornflakes

New Member
Joined
Jan 28, 2021
Messages
1
Reaction score
0
Good day,
I am very new to coding and have just done my first website. I have a form on my page to which i would like guests to RSVP, but get the 404 error when hitting the accept or regret button. jtconfirmation.co.za
PHP:
<?php

use PHPMailer\PHPMailer\PHPMailer;
require __DIR__ . '/vendor/autoload.php';

$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    

    if (empty($name)) {
        $errors[] = 'Name is empty';
    }

    if (empty($email)) {
        $errors[] = 'Email is empty';
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Email is invalid';
    }

    
    if (!empty($errors)) {
        $allErrors = join('<br/>', $errors);
        $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
    } else {
        $mail = new PHPMailer();

        // specify SMTP credentials
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'P@ssword123';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->setFrom($email, 'Mailtrap Website');
        $mail->addAddress('[email protected]    ', 'Me');
        $mail->Subject = 'RSVP Church';

        // Enable HTML if needed
        $mail->isHTML(true);

        $bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", nl2br($message)];
        $body = join('<br />', $bodyParagraphs);
        $mail->Body = $body;

        echo $body;
        if($mail->send()){

            header('Location: thank-you.html'); // redirect to 'thank you' page
        } else {
            $errorMessage = 'Oops, something went wrong. Mailer Error: ' . $mail->ErrorInfo;
        }
    }
}

?>
HTML:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Names Confirmation RSVP</title>
    <link rel="stylesheet" href="church.css">
</head>

<body>
    <form action="/mail_form.php" method="POST" id="church-form">
        <div class="top">
            <nav>
                <a href="index.html" class="mybutton">Home</a>
            </nav>
        </div>
        <div class="form">

            
            <div class="info">
                <h1>RSVP</h1>
                <h2>for the Confirmation Service of</h2>
                <h1>Name</h1>
                <p class="line">________________________________________</p>
                <h2>The Details</h2>
                <p>Sunday, 28 February 2021</p>
                <p>10:00 AM</p>
                <p>Please RSVP by 15 February 2021</p>
                <h2>Confirmation Service</h2>
                <p><a href="#" target="_blank">
                        Calvyn Protestant Church Diep River</a></p>
                <p>10 Keswick St, Elfindale</p>
                <p class="line">________________________________________</p>
                
                <input type="text" value="name" placeholder="Name">
                <input type="email" value="email" placeholder="Email">
            </div>
            <?php echo((!empty($errorMessage)) ? $errorMessage : '') ?>
            <button type="submit" class="accept" value="Send">Accept</button>
            <button type="submit" class="regret" value="Send">Regret</button>
        </div>
    </form>

    <script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
  <script>
      const constraints = {
          name: {
              presence: {allowEmpty: false}
          },
          email: {
              presence: {allowEmpty: false},
              email: true
          }
      };

      const form = document.getElementById('church-form');

      form.addEventListener('submit', function (event) {
          const formValues = {
              name: form.elements.name.value,
              email: form.elements.email.value
          };

          const errors = validate(formValues, constraints);

          if (errors) {
              event.preventDefault();
              const errorMessage = Object
                  .values(errors)
                  .map(function (fieldValues) {
                      return fieldValues.join(', ')
                  })
                  .join("\n");

              alert(errorMessage);
          }
      }, false);
  </script>



</body>

</html>
 
what happens in the browser if you go to `/mail_form.php`?

add an `else` block

Code:
if (!empty($_POST)) {
   ...
} else {
   echo "Everything is fine";
}
 
<form action="/mail_form.php" method="POST" id="church-form">

"/mail_form.php" is an absolute path to your webspace, meaning that it sits at the root of your webspace.

You should always use relative path to where your html page is located. If mail_form.php sits in the same directory as your html, the path should be "./mail_form.php" or simply "mail_form.php".
 
Top
Sign up to the MyBroadband newsletter
X