-1

I'm having a pretty basic mistake here somewhere..

Trying to make this bootstrap form to work. I have two files: index.html, index.php and they're both in the same folder.

As you can see, the PHP content inside my HTML is showing, no clue why. Im running it on Apache, but wait before you say that's the problem, I've tested it on another server (Linux) and it doesn't work either.

edit: adding AddType application/x-httpd-php5 .html .htm

AddHandler fcgid-script .html FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html

AddHandler x-httpd-php5-cgi .html

to my .htaccess and other combinations don't work....

Thank you.

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
    <meta name="author" content="BootstrapBay.com">
    <title>Bootstrap Contact Form With PHP Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="page-header text-center">Contact Form Example</h1>
                <form class="form-horizontal" role="form" method="post" action="index.php">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                            <?php echo "<p class='text-danger'>$errName</p>";?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-10">
                            <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>
                    <div class="form-group">
                        <label for="message" class="col-sm-2 control-label">Message</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                            <?php echo "<p class='text-danger'>$errMessage</p>";?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
                            <?php echo "<p class='text-danger'>$errHuman</p>";?>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <?php echo $result; ?>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  </body>
</html>

and my index.php:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Demo Contact Form';
        $to = 'example@domain.com';
        $subject = 'Message from Contact Demo ';

        $body ="From: $name\n E-Mail: $email\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 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 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>';
    }
}
    }
?>

What I see is this:

enter image description here

Sando K
  • 119
  • 9
  • 1
    `.html` wont execute as `PHP` unless you tell the server to do so. – chris85 Dec 11 '15 at 22:49
  • 3
    Possible duplicate of [PHP code is not being executed (I can see it on source code of page)](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) or See http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – chris85 Dec 11 '15 at 22:51
  • Gave ya 2 links that should be able to help you. – chris85 Dec 11 '15 at 22:54
  • 1
    @Wiser Just call the page `index.php` and it should be fine. You may need to remove any existing `index.html` or the server might pick it as default. Usually you can see the order it will use page extensions in through info in your service provider dashboard. – Steve Dec 11 '15 at 22:57
  • 1
    Lose the index.html altogether and rename index.html index.php You also need a form in your `index.php` which submits to another page called anything_you_want.php with your existing index.php processing work inside that. You could put all on the same page but that would be a different learning curve. – Steve Dec 11 '15 at 23:04
  • 1
    @wiser In fact you don't need any php in your index page as you are currently working with two pages. One to submit what is typed and the other to process it. – Steve Dec 11 '15 at 23:10
  • OK guys, combined it into one php file and it works, but is it truly the best way to do it? – Sando K Dec 11 '15 at 23:26
  • 2
    @Wiser There is no best way as such. This way is perfectly acceptable; if/when you've been programming using PHP for many more years you might want to try frameworks to handle things like this better, but this is just fine for now. – worldofjr Dec 11 '15 at 23:34
  • 1
    @Wiser If you want to have what was submitted appear in your text boxes once the form is submitted then the one-page approach will probably work best - it looks as though you already have code that will stop the script from sending email if there is nothing typed and that is the most crucial part. For your email form to work, you will need to ensure that the from address is a valid email on your server or it will be eaten before it even gets a chance to be considered as spam. See:https://stackoverflow.com/questions/33948622/php-mail-function-server-and-localhost-not-working/33948920#33948920 – Steve Dec 12 '15 at 00:54

1 Answers1

1

Another solution is to do away with the .html file and put everything in index.php, which will then be the default file to run when someone goes to to your url (most web servers with PHP installed will be configured to run index.php if there is no index.html).

Your php file would look something like this:

<?php
[your php code here]
?>
[your html code here]

But your php snippets must be in a PHP as a .html file won't be parsed by the PHP interpreter at all.

GuyH
  • 786
  • 1
  • 4
  • 8