0

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!

Yuchen Ren
  • 287
  • 5
  • 13
  • 1
    If the file has PHP code in it then yes, the extension should be `.php`. Is all of the validation on the same page and is the form supposed to be posting to itself? My guess is that the path in your `action` is incorrect, try removing the action altogether *if* the form is supposed to be posting to itself. Or you have a syntax error in your PHP and you should check the php error log. – Andrew Apr 27 '22 at 03:03
  • Hello Andrew, basically I have my form included in my index.html file. In the action of that form, I have my /php/contact-form.php which is where the PHP code above is being stored. In the example I took this from, they had their file with the form on it saved as a PHP extension file, and had additional PHP code that sent a confirmation message if the message sent. Can I keep my index file as an HTML extension? Or should I rename it with a PHP extension and tell my website hosting to look for the .PHP version? – Tristan Huyck Apr 27 '22 at 03:32
  • 1
    I am not sure what your entire `index.html` file looks like, but if there's PHP at all in there, you will want to change the extension to `.php` and tell the website to look for the `.php` version (you could do this in the `.htaccess` file). Please add more information and context. Are you getting php errors? Does the email send? Are you trying to redirect back to `index.html` once the email is sent? – Andrew Apr 27 '22 at 03:44
  • 1
    Question: Now you 've got a blank page when you submit the data --- but did you receive the email ? – Ken Lee Apr 27 '22 at 03:47
  • Hey Andrew, what I would like to accomplish is after the form is submitted, a message gets placed in the form area that says "Thank you for your Information" The email does not send (or I don't think it does because I don't receive one in my mailbox). The PHP errors I saw in the erro_log had to do with some syntax errors which I corrected. – Tristan Huyck Apr 27 '22 at 04:13
  • If I want the message to appear after the form is submitted then I would need to change my index file to a PHP extension then correct? – Tristan Huyck Apr 27 '22 at 04:13
  • 1
    For what you want, just add the line `if ($message_sent) { echo "Thank you for your information"; }` to the end of your php file – Ken Lee Apr 27 '22 at 05:54
  • Ken Lee - I added that code and when I hit submit, the screen that was totally blank now says "Thank you for your information" but I don't get an email. – Tristan Huyck Apr 28 '22 at 21:39
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – kmoser Jun 25 '22 at 22:27

1 Answers1

0

If your index page contains some php code, then you have to change the extension to '.php'.

Either ways, you may need to add

//previous codes

mail($to,$messageSubject, $body);

$message_sent = true;

header('Location: myexamplewebsite.com');

to redirect back to the index page