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:
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:
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
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..
You need a web server to run php scripts.
Try installing wamp and then run your page under localhost.
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
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”
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.