2

I am trying my hand at creating an HTML5 form styled with CSS3. However, in the past, I have only used form building tools that generate the PHP action code. I have found such varied examples online, that I want to ask here if someone can give me an example of simple, clean and valid PHP for a 'post' method.

I am going to take advantage of the HTML5 'input' types and attributes, so I would like for the PHP to simply take the data (already validated by the browser through HTML5) and send it to me by email and then display a success message on the website for the sender. Thank you.

Edit: What I am asking for is the code for the PHP file that the form will point to for processing. I know how to do the HTML and CSS, but would appreciate an example of the PHP code to complete the actions I mention above.

6
  • 4
    Sounds like a plan! Good luck with that. I'm going to do a little more work, then probably hit the sack soon. Commented Jan 6, 2012 at 4:05
  • Well, any basic knowledge about php do you have? And if you have show us some of your mastery in it regarding the "question" and maybe we could help you. Commented Jan 6, 2012 at 4:11
  • 5
    Apparently, Doris knows HTML5. Commented Jan 6, 2012 at 4:13
  • 2
    Whatever you do, do not trust html5 validation to handle everything for you. It is trivial for an attacker to bypass. Commented Jan 6, 2012 at 4:16
  • Sorry if my question was not clear, I have added to it above. Hopefully, I have asked it correctly now. Commented Jan 6, 2012 at 4:23

1 Answer 1

8

It seems you are confusing html5 with css3 with php. They are all separate technologies that are used together. A form is just a form. You submit it and your php processes it. You need to read about post and get processing in php. For the html side you just submit a form and specify which of the two methods (get or post) you want to use

<!-- none of this has anything to do with html5 or css3 -->
<!-- for method specify POST or GET -->
<form method="POST" action="blah.php" >
   <input type="text" name="foo" value="hello world" />
   <input type="submit" value="Submit" />
</form>

//receive the form data in php and do something with it
<?php  $blah = $_POST['foo']; //returns hello world ?>

Handling emailing stuff to yourself is whole other can of worms. Get this down first and then ask that part in a new SO question.

Update - based on update to question by OP

The two things you are looking for are pretty simple. First create a php file that the form submits to. In my example above I chose blah.php. There you can query the global variable $_POST[]. This variable will contain a hash for each input name and input value. In the example above these are foo and hello world.

$message .= $_POST['foo'];

You can then use the php mail method to send an email to yourself.

mail('[email protected]', 'Subject', $message);

And finally whatever you echo out will be returned to the user

echo 'this is blah.php. Thank you for submitting a form!';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. I am familiar with the HTML. What I am looking for is the code (that would go in the blah.php file) to which the 'method=post' will point. I would like to gather the data from the 'input' fields, email the data to me and then display a success message on the website for the visitor.
Thanks. I figured it was pretty simple, I am just not familiar with PHP and was finding all kinds of different suggestions when searching. I will play with this a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.