1

I made a contact form which works perfectly. I combined both the html and php in a .php file, but I noticed that after I used a .htaccess file in my GoDaddy server to remove the PHP extension from my website, the contact form no longer works. If you click the submit button, it only reloads the page, no error messages, no success message, nothing.

Here's the code that I used.

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
                $number = $_POST['phoneNumber'];
        $message = $_POST['message'];
        $from = $_POST['name'];
        $to = 'xxxxxxxxxxx'; 
        $subject = 'Client Enquiry';

        $body =" From: $name\n E-Mail: $email\n Mobile: $number\n Message:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your 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 number has been entered and is valid
                if (!$_POST['phoneNumber']) {
            $errNumber= 'Please enter your number';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
    if (mail ($to, $subject, $body)) {
        $result='<div class="alert alert-success">Thank You! We would get back to you within 24 hours.</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
?>


<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html lang="en">

<body>




   <section class="space"> 


    <main class="site-main parallax">



        </section>


    <div id="conct" class="container-fluid" style="background-color: #333; padding-top: 25px; margin-top: 2px;  ">
        <div class="container col-md-6 col-md-offset-3" >

                <h2 class="section-title" style=" text-align: center; color: white; font-size: 45px" ><span class="glyphicon glyphicon-send"></span> &nbsp;contact us</h2><br></br>
  <form role="form" method="post" action="contact.php" >
    <div class="form-group">
      <label for="name"><span class="glyphicon glyphicon-user"></span> Full Name:</label>
      <input type="text" class="form-control" id="name" name="name" placeholder="full name"value="<?php echo htmlspecialchars($_POST['name']); ?>">
            <?php echo "<p class='text-danger'>$errName</p>";?>
    </div>
    <div class="form-group">
      <label for="email"><span class="glyphicon glyphicon-envelope"></span> Email:</label>
      <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
    </div>
      <div class="form-group">
      <label for="phoneNumber"><span class="glyphicon glyphicon-earphone"></span> Mobile:</label>
      <input type="text" class="form-control" id="phoneNumber" placeholder="telephone" name="phoneNumber" <?php echo htmlspecialchars($_POST['phoneNumber']); ?>>
            <?php echo "<p class='text-danger'>$errNumber</p>";?>
    </div>

    <div class="form-group">
      <label for="message"><span class="glyphicon glyphicon-comment"></span> Message:</label>
      <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>";?>
    </div>



  <div class="form-group text-center">

            <input id="submit" name="submit" type="submit" value="Send" class="btn">

    </div>
      <div class="form-group">
          <div class="col-sm-10 col-sm-offset-1" align="center" style=" padding-bottom: 10px">
            <?php echo $result; ?>  
        </div>
    </div>
  </form>
</div>
    </div>




    </main>

</body>
</html>

I would go ahead and add the rewrite rule I used.

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /


RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

If anyone can point me in the right direction, I would really appreciate that.

Ibrahim Tareq
  • 336
  • 2
  • 12
Dr who
  • 113
  • 2
  • 11
  • Form target= is `contact.php` still? The copy'n'pasted RewriteRule does a plain redirect for that, whcih implies all POST params getting dropped. – mario May 13 '18 at 12:54

0 Answers0