0

I am finishing up a ONE-PAGE responsive website for a client. I am using a Bootstrap contact form at the bottom of the HTML page. There is a second Contact.php file to process the form. The PHP code is showing up in the contact form text fields.

I've read and followed the directions for other folks' similar problems, but my client's URL ends in .HTML so I don't want to make the URL end in .PHP to make the contact form work. Is there a workaround to A) get the code to NOT show up in the text fields, and B) process the form without changing the HTML to PHP. Thanks a bunch - this has been driving me nuts for two days now.

HTML DOC.

<!--  FORM SECTION STARTS HERE---- -->
  <section id="contact">
    <div class="container">
        <div class="row">
            <div class="col-md-12"> 
                <h3 class="h3-complete-form">Just complete the form below.</h3>
           </div>
         </div>
    </div>

<form class="form-horizontal" role="form" method="post" action="contact.php">

<div class="row">
   <div class="col-sm-3"></div>
      <div class="contact-icon">
        <i class="fas fa-user"></i>
      </div>

     <div class="form-inline">
       <div class="col-sm-2 form-group">
           <label for="name" class="control-label">Name</label>
             <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">

            <?php 
            echo $errName;?>
       </div>
   </div>
</div>

  <div class="row">
      <div class="col-sm-3"></div>
         <div class="contact-icon">
            <i class="fas fa-envelope"></i>
         </div>

        <div class="form-inline">
            <div class="col-sm-2 form-group">
          <label for="email" class="control-label">Email</label>
              <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com">
              <?php 
              echo $errEmail;?>
          </div>
     </div>
  </div>
</div>
</div>

<div class="row">
  <div class="col-sm-3"></div>
    <div class="contact-icon">
      <i class="fas fa-pencil-alt"></i>
   </div>

   <div class="form-inline">
      <div class="col-sm-10 form-group">
          <label for="message" class="control-label">Message</label>
          <textarea class="form-control" rows="4" name="message"
             placeholder="How can I help you?">
             <?php 
             echo $errMessage;?>
        </div>   
   </div>
</div>


<div class="row">
    <div class="col-sm-3"></div>
      <div class="contact-icon">
        <i class="fas fa-pencil-alt"></i>
</div>

    <div class="form-inline">
        <div class="col-sm-10 form-group">
            <label for="human" class="control-label">2 + 3 = ?</label>
          <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
          <?php echo $errHuman";?>
            </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-4"></div>
    <div class="form-inline">
    <div class="form-group">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
            <?php echo $result; ?> 
     </div>
   </div>
</div>
</div>

    <!-- Displays an alert to user -->
<div class="row">
   <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
          <?php echo $result; ?>    
    </div>
  </div>
</div>
</form>

 </div>
</div>
<!-- --------- SECTION 8: CONTACT ME ENDS HERE------ -->


PHP DOC


<?php include 'validate.php'; ?>

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = "From: ". $name . " <" . $email . ">\r\n";
        $to = 'myname@myemailaddress.com'; 
        $subject = 'Message from Contact Form';

        $body ="From: $name\n E-Mail: $email\n Message:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your first and last name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam answer is incorrect';
        }
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
?>
Angie G
  • 15
  • 3
  • This answer uses htaccess to add a handler to allow html to be processed as php. https://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – TimBrownlaw Jun 10 '19 at 04:35
  • Thank you, I visited the link but that's all way over my head. This is only my second website. I'm using Visual Studio and I just installed Apache extension, but there is no .htaccess extension. It saved as ".conf". Does it matter what I name the file assuming I leave it in the root directory? I'm also using GoDaddy as my host and not sure if I should just include all advice given in the comments. – Angie G Jun 10 '19 at 05:16
  • You should be able to create a .htaccess ( it must start with a DOT ) under your public_html folder for your website. See if you can do that. – TimBrownlaw Jun 10 '19 at 05:26
  • I added a doc called "a.htaccess" to the root folder for the website with the code above. Now I have other issues with the form. It doesn't validate the fields and when I click "Submit" and I get a blank white page for "contact.php". I'm trying the right code to return to the home page upon submitting form, and figure out why it's not validating the form fields. – Angie G Jun 10 '19 at 20:46
  • Sorry I meant just a file called .htaccess as in dot htaccess - there is no a in front. – TimBrownlaw Jun 11 '19 at 02:09

1 Answers1

0

You can try with .htaccess file to treat .html extension as .php.

RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41