0

I need some help with validation. I have a Bootstrap form inside an HTML page - and a PHP to make it submit; however, when someone clicks on Submit, the page is redirected to where it shows my PHP code, and it doesn't actually submit anything. Am I missing a script somewhere?

Note: I am coding this in AMP HTML.

Edit: I installed MAMP to test the form. I clicked the submit button and now it shows up with a blank page.

HTML

<form id="submit-form" action="php/sendemail.php" method="POST" class="cf-validation">
<label><h3>Your Name (Required)</h3></label>
    <div class="input-group">
      <input type="text" name="fname" class="form-control">
      <span class="input-group-addon" id="name"><span class="fa fa-user"></span></span>
    </div>

<label><h3>Email (Required)</h3></label>
    <div class="input-group">
      <input type="email" name="senderEmail" class="form-control">
      <span class="input-group-addon" id="email"><span class="fa fa-envelope"></span></span>
    </div>

<label><h3>Phone</h3></label>
    <div class="input-group">
      <input type="phone" name="phone" class="form-control">
      <span class="input-group-addon" id="phone"><span class="fa fa-phone"></span></span>
    </div>

<label><h3>How Can We Help You?</h3></label>
    <div class="single_form"> 
        <select class="selectmenu input-lg form-control" name="category">
            <option selected="selected">Services</option>
            <option>Spa Parties</option>
            <option>Specials</option>
            <option>Other</option>
        </select>
    </div>

<label><h3>Inquiry Details</h3></label>
    <div class="input-group input_group_textarea">
        <textarea name="message" aria-describedby="basic-addon4" class="form-control"></textarea>
        <span class="input-group-addon" id="basic-addon4"><i class="fa fa-comments"></i></span>
    </div>
    <br />
    <button type="submit" class="btn btn-success btn-lg"  style="color:black;">Submit</button>
</form> <!-- End Form -->

PHP

<?php
// Define some constants
define( "RECIPIENT_NAME", "Name" );
define( "RECIPIENT_EMAIL", "email@example.com" );

// Read the form values
$success = false;
$fname = isset( $_POST['fname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['fname'] ) : "";
$lname = isset( $_POST['lname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['lname'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$phone = isset( $_POST['phone'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['phone'] ) : "";
$category = isset( $_POST['category'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['category'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

$mail_subject = 'A contact request send by ' . $fname. $lname;

$body = 'Name: '. $fname . $lname . "\r\n";
$body .= 'Email: '. $senderEmail . "\r\n";
$body .= 'Phone: '. $phone . "\r\n";
$body .= 'Category: '. $category . "\r\n";
$body .= 'Message: ' . "\r\n" . $message;

// If all values exist, send the email
if ( $fname && $senderEmail && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $fname . $lname . " <" . $senderEmail . ">";  
$success = mail( $recipient, $mail_subject, $body, $headers );
echo "<p class='success'>Thanks for contacting us. We will contact you ASAP!   </p>";
}

?>

Scripts

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
halfer
  • 19,824
  • 17
  • 99
  • 186
Robert Dewitt
  • 324
  • 5
  • 17
  • Did you var_dump the $_POST variable? If so, what does it say, if not, do that and then tell me what it says. – amflare Apr 29 '16 at 20:35
  • @amflare I actually have zero experience in PHP ... how do I do that? – Robert Dewitt Apr 29 '16 at 20:39
  • 1
    @amflare The OP said the PHP code is being displayed to the browser. Trying to do `var_dump` or execute any PHP functions won't work if the code is not being parsed. – Mike Apr 29 '16 at 20:42
  • That didn't work - unless I'm putting it in the wrong place. – Robert Dewitt Apr 29 '16 at 20:43
  • Since you said that it is just showing your php code, I would make sure that php is running at all. Make a file called `test.php` and in that file put ``. If it shows hello world when you view it in your browser, then it should be working. If it just shows your code, I would check the question that Mike linked to. – Cave Johnson Apr 29 '16 at 20:43
  • @Mike ooooh, ok. I missed that, thanks. – amflare Apr 29 '16 at 20:44
  • @Mike Parsed? That term sounds familiar ... how do I go about that? – Robert Dewitt Apr 29 '16 at 20:44
  • @Andrew's answer is how to go about it. – amflare Apr 29 '16 at 20:44
  • @RobertDewitt Click the link in my first comment and go through all the answers to see if you can get this figured out. If you're seeing the PHP source code you're doing something wrong. – Mike Apr 29 '16 at 20:45
  • @Andrew when I type in - thats all I see in the browser window when I hit submit, all by exact words. – Robert Dewitt Apr 29 '16 at 20:45
  • https://en.wikipedia.org/wiki/Parsing – Mike Apr 29 '16 at 20:45
  • What kind of server are you running this on? – Cave Johnson Apr 29 '16 at 20:46
  • @amflare Yes it looks like it is specific to Apache. OP: if you're using IIS or Nginx (or anything else) instead of Apache let me know and I'll look for a link for you. – Mike Apr 29 '16 at 20:48
  • @Andrew I can't run the submit form locally ... ? I am using Adobe Brackets to edit my code and then Chrome to view my changes. This means I cant see if it actually works until I upload it to the domain? – Robert Dewitt Apr 29 '16 at 20:50
  • What server is your domain running on? Do you know? – amflare Apr 29 '16 at 20:52
  • @RobertDewitt That is correct, you have to upload it unless you have a webserver and php installed on your computer. – Cave Johnson Apr 29 '16 at 20:52
  • @Andrew I have one site that I was able to use this same PHP file - and when I used the contact page on there, it worked without throwing the PHP code visually at me. But this new site I'm working on has the same PHP file basically, but it's throwing this code at me instead. I can't see the effects of the form until I upload it to my client's site? – Robert Dewitt Apr 29 '16 at 20:55
  • I forgot to mention I am using AMP HTML to code my pages. – Robert Dewitt Apr 29 '16 at 21:03
  • @RobertDewitt The tool used to produce the pages is irrelevant. – Mike Apr 29 '16 at 21:07
  • 1
    @RobertDewitt Do you have a webserver installed on your computer? I can't explain why it would have worked before if you don't have a webserver + php installed. – Cave Johnson Apr 29 '16 at 21:11
  • I am about to install Wampserver to see if this runs. Maybe I just have to do that, after all. – Robert Dewitt Apr 29 '16 at 21:33
  • 1
    I installed MAMP to test the form. I click the submit button and now it shows up with a blank page. – Robert Dewitt Apr 29 '16 at 22:22
  • @KodosJohnson - so basically I had no server back then, silly me – Robert Dewitt Jun 21 '17 at 08:49

2 Answers2

0

enable you php module in apache, make sure that

LoadModule php5_module modules/libphp5.so

is not comment in httpd.conf file of apache server

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
-1

Removing this part of the code worked for me:

 action="php/sendemail.php" 
Robert Dewitt
  • 324
  • 5
  • 17