0

I am creating a contact form for a website and when I open the contact page some of my php code is showing at the top of the page. In the very top left corner of the page, ";} ?> is displayed above my menu.

Here is the php/HTML:

<?php
    if($_POST["submit"]) {
        $recipient="collin@klopcrete.com";
        $subject="Contact Form from klopcrete.com";
        $sender=$_POST["sender"];
        $senderEmail=$_POST["email"];
        $senderPhone=$_POST["phone"];
        $senderAddress=$_POST["address"];
        $message=$_POST["message"];

        $mailBody="Name: $sender\nEmail: $senderEmail\nPhone: $senderPhone\nAddress: $senderAddress\n\n$message";

        mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
    }
?>
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>KC Contact Information </title>
    <link type="text/css" rel="stylesheet" href="kc.css" />
    <link type="text/css" rel="stylesheet" href="contact.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

</head>

<body>
    <div class="container">
        <div class="menu-btn">
            <ul class="smallNav">
                <li><a href="#" onClick="showMainMenu()";>Menu</a></li>
                <li style="float:right"><a href="#" onClick="showMainMenu()";><img src="images/hamburger.png" height="20" width="40" alt="menu icon" /></a></li>
            </ul>
        </div>
        <div id="links">
            <ul id="navMenu">
            <li class="nav"><a href="kcweb.htm">Home</a></li>
            <li class="nav"><a href="about.htm">About Us</a></li>
            <li class="nav"><a href="contact.htm">Contact Us</a></li>
            <li class="nav"><a href="#" onClick="showSubMenu()";>Photos</a>
                <ul class="submenu">
                    <li class="gallery"><a href="stamp.htm">Stamped Concrete</a></li>
                    <li class="gallery"><a href="exposed.htm">Exposed Aggregate</a></li>
                    <li class="gallery"><a href="stain.htm">Stained Concrete</a></li>
                    <li class="gallery"><a href="driveway.htm">Driveway/Walkway</a></li>
                    <li class="gallery"><a href="walls.htm">Walls</a></li>
                    <li class="gallery"><a href="countertop.htm">Countertops</a></li>
                    <li class="gallery"><a href="commercial.htm">Commercial</a></li>
                </ul>
            </li>
        </ul>
        </div>
        <div id="sign">
            <p><span>Klopstein Concrete</span><br />
            Everything Concrete...</p>
            <ul class="socMedia">
                <li><a href="https://www.facebook.com/pages/Klopstein-Concrete/256257947822?ref=hl"><img src="images/facebook.png" alt="facebook" /></a></li>
                <li><a href="http://www.bbb.org/stlouis/business-reviews/concrete-contractors/klopstein-concrete-in-foley-mo-310436248#reasonrating"><img src="images/bbb.png" alt="better business bereau" /></a></li>
            </ul>
        </div>
        <div id="main">
        <div id="info">
            <p>Office Phone Number: 636-668-6027</p><br />
                <ul class="contact">
                    <li>Joe Klopstein</li>
                    <li><i>President</i></li>
                    <li>Phone Number: 314-574-1244</li>
                    <li>E-Mail: joe@klopcrete.com</li>
                </ul>
                <ul class="contact">
                    <li>Collin Klopstein<li>
                    <li><i>Vice President</i></li>
                    <li>Phone Number: 314-574-1187</li>
                    <li>E-Mail: collin@klopcrete.com</li>
                </ul>
                <ul class="contact">
                    <li>Conor Klopstein</li>
                    <li><i>Vice President</i></li>
                    <li>Phone Number: 314-220-6070</li>
                    <li>E-Mail: conor@klopcrete.com</li>
                </ul>
                <br />
                <br />
        </div>
        <div id="personal">
        <form id="contact-form" action="contact.php" method="post">
            <p>Fill in the form below, and we'll get back to you within 24 hours.<p>
            <div>
                <label class="contact-form" >
                    Name: (required)<br />
                    <input placeholder="Please enter your name" type="text" name="sender" tabindex="1" required autofocus>
                </label>
            </div>
            <div>
                <label class="contact-form">
                    Email: (required)<br />
                    <input placeholder="Please enter your email address" type="email" name="email" tabindex="2" required>
                </label>
            </div>
            <div>
                <label class="contact-form">
                    Telephone: (required)<br />
                    <input placeholder="Please enter your number" type="tel" name="phone" tabindex="3" required>
                </label>
            </div>
            <div>
                <label class="contact-form">
                    Address: (required)<br />
                    <input placeholder="Please enter your address" type="text" name="address" tabindex="4" required>
                </label>
            </div>
            <div>
                <label class="contact-form">
                    Message: (required)<br />
                    <textarea placeholder="Include all the details you can" tabindex="5" name="message" required></textarea>
                </label>
            </div>
            <div>
                <button name="submit" type="submit" id="contact-submit">Submit</button>
            </div>
        </form>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/scripts.js"></script>
    <script>
        function showSubMenu() {
            $('.submenu').toggle('');
        }

        function showMainMenu() {
            $('#navMenu').toggle('');
        }
    </script>
</body>

</html>
howdydoody
  • 93
  • 3
  • 17
  • 5
    View the page source in your browser. You'll find that all your PHP code is there, being mis-rendered as broken HTML. Your web server has failed to parse this as PHP at all. Does it have a .php extension? Are you actually running a web server, rather than just trying to view a local .php file with your browser? – Michael Berkowski Nov 20 '14 at 03:07
  • I am just trying to open a local .php file. I have never really worked with php, just started today so I can send the completed form to an email address. – howdydoody Nov 20 '14 at 03:10
  • 4
    When opening a file with the browser, it can only attempt to render HTML. I cannot parse or execute PHP. You will need a web server. Many people start with WAMP, as a quick way to get up and running with a development server. http://www.wampserver.com/en/ – Michael Berkowski Nov 20 '14 at 03:12
  • I removed <$senderEmail> from the mail(). The page is now not showing any code. I guess it was using the closing bracket in <$senderEmail> as the closing bracket for the php code? – howdydoody Nov 20 '14 at 03:16
  • And thanks for the link to WAMP – howdydoody Nov 20 '14 at 03:16
  • 1
    If you view source in your browser, _all_ the code will be there. What it did was attempt to make an HTML tag out of `` on the email address. That's not an HTML tag, so the browser ignored it and displayed nothing. – Michael Berkowski Nov 20 '14 at 03:17

1 Answers1

0

It looks like your server isn't processing the php and is sending the code to the browser like a non-server side language. The problem may be because you are putting php code inside a file with a .html (or other) extension. To fix this just go to your .htaccess file and make this edit:

See this question (my reference for the code below): a good answer to your problem.

Create a .htaccess file at the root of your website and add this line:

AddType application/x-httpd-php .html .htm
If your are running PHP as CGI (probably not the case), you should write instead:

AddHandler application/x-httpd-php .html .htm 
Community
  • 1
  • 1
www139
  • 4,960
  • 3
  • 31
  • 56
  • I have added this file to the root of my website. When I click the submit button, the only thing that happens is the page refreshes. The form data isn't sent to the email address. – howdydoody Nov 26 '14 at 05:32
  • Do this for me: load in your page. Then do view page source and see if your php is showing up. If so then something is wrong on the server with your configuration with .htaccess. – www139 Nov 26 '14 at 13:59
  • also try commenting out the mail code and replace it with an echo statement and see if the echo statement runs. – www139 Nov 26 '14 at 14:00
  • I just viewed the page source in my browser and my php isn't showing up. – howdydoody Nov 27 '14 at 16:58
  • Post a copy of your .htaccess file in the question. If you have one. If you don't have one tell me. – www139 Nov 27 '14 at 17:17
  • I removed the .htaccess file and gave the file for this page a .php extension. When I had the .htaccess file, the remainder of the web pages were displaying differently. – howdydoody Nov 27 '14 at 17:23
  • I also tried commenting out the mail() code and tried an echo statement, nothing happened – howdydoody Nov 27 '14 at 17:24
  • I changed the to and now I have my $thankYou message showing after submission but, I am still not receiving an email with the form information. – howdydoody Nov 27 '14 at 17:43
  • If you haven't done this try using the code in my answer for the htaccess file. Also try creating two files one .html and one .php then put an echo statement in each file tell me what you see. Good luck! :) – www139 Nov 27 '14 at 18:01
  • I have it working now. It was a problem with the settings on the server side. The hosting site changed the settings to allow Remote mailserver access. – howdydoody Nov 28 '14 at 17:59
  • hi-five. Good for you! :-). Celebrations, cheers, etc, etc.... awesome. – www139 Nov 29 '14 at 00:07