9
votes

I created a contact form for my website. When I click the submit button, it just shows the PHP code instead of the message it is supposed to show, like the text information the user inputs into the form.

Here is the HTML code for the form:

<html>
    <link href="contact.css" rel="stylesheet" type="text/css" />
    <head>
        <title>Contact Us</title>
    </head>

    <body>
        <form action="contact.php" method="post">
            <div id="ContactForm">
                <fieldset>
                    <h4>Contact Parquest:</h4>

                    <label class="labelone" for="name">Name:</label>
                    <input name="name"/>

                    <label for="email">Email: </label>
                    <input name="email"email"/>

                    <label for="commens">Comments: </label>
                    <textarea name="commens"></textarea>
                </fieldset>
                <fieldset>
                    <input class="btn" type="submit" value="Send Email"/>
                    <input class="btn" type="reset" value="Reset Form"/>
                </fieldset>
        </form>
        </div>
</html>

I couldn't post the PHP code, because it just doesn't show when I try, so here's a screenshot:

Enter image description here

5
You should include the CSS file inside the HEAD tags... though that isn't your problem.. What is 'contact.php' and where is it's source code? To include php code, put it into say Notepad++ and select all of it and add an extra indent (tab) so everything is indented at least once.. Then paste it here into SO.. Remember, you need 4 spaces to add the code formatting here. So if it broke your question, then you had something break the 4 spaces so SO didn't know it was code.Wade
i included a screenshot of the php fileDennis Orlovski
Is the php Module enabled on your server?Amit Verma
Starkeen, how do i enable it?Dennis Orlovski
As another user stated, you have a typo ($commets = $_POST['comments']) which would throw an error when you echo $comments, since $comments is not declared. So my first hunch is you have error reporting turned off and your just getting a white screen. In development, your error reporting should be set to E_ALL so you can see all the errors to fix your code..Wade

5 Answers

17
votes

There is no problem to your code.. The problem is on your environment.. I guess you are not running the html file through a server..

If the url on your browser looks like these:

file:///c:/path/to/your/file/page.html

then you are doing it wrong.. in order to run .php scripts, you need a web server like apache or nginx... the url of should be like these

http://localhost/path/to/file/page.html

then the php file should run as expected..

php files are interpreted scripting language and thus it needs an interpreter in the server in order to run.. if it is just browsed in the browser without a server, it will just output the code inside..

2
votes

You need a web server to run php scripts. Try installing wamp and then run your page under localhost.

1
votes

I have executed the given code. It's working fine on my local server. Conventionally it should print the value inputs posted by form on contact.php.

But the thing I noticed in your PHP code is the variable name is $commets, instead of $comments.

1
votes

i had the same issue, even if appache & mysql were running on my computer. in the html file, i wrote : form action="http://localhost/file.php" method="POST" instead of : form action="file.php" method="POST"

0
votes

It seems like connecting HTML forms to php files only works when you download wampserver and turn on all services...

Here's how it worked for me:

You can download wampserver from here: http://www.wampserver.com/en/ (follow instructions) I saved it to the C: drive on my computer.

Next, you can create a php file and an html file both in the 'www' folder of the wamp folder in whatever drive you put it in. Copy the form code into the HTML file, and the php code into the php file.

Finally, go to 'localhost/yourhtmlname.html' and fill out the form. It should work as normal.