PHP form to email tutorial – Part 1 – Setting up the form
This weekend I did some volunteer work for a local non-profit by constructing an online form in php that would email from their website. I’m going to simplify that form and walk thru a tutorial to help you build your own online php forms.
Part 1 will walk you thru setting up the actual form to get it ready to be processed by the PHP script. The form just gets the information to the PHP script.
Part 2 will be the setup of the receiving PHP script, which will process the info from the form and email it.
Part 3 will look at client-side validation with Javascript.
Part 4 we will create a multiple choice input area for the user, and only emailing their selections in the form. We will do this by creating an Array.
Part 5 will create code to also email a confirmation to the user with the info they filled out.
A well designed form will set the stage for easy processing so, with that said, let’s dive into the form construction.
Open your html editor, for most it will be Dreamweaver or similar. I use Quanta+, an open-source software for Linux. Create a new PHP document.
First, let’s add the form elements to our web page, take notice that we are defining the name, method, and action:
<html>
<head></head>
<form name="adReg" method="POST" action="adaction2.php">
</form>
</html>
name = how we will refer to our form
method = POST – how we will send our data over to PHP – this is the transport
action = when a user clicks the Submit button, where the form should send it’s data.
Now, inside the form tags, let’s add just two items, keeping it simple for right now:
<form name="adReg" method="POST" action="adaction2.php">
Contact Name:
<input type="text" name="cName">
Notes:
<textarea name="notes"></textarea>
</form>
Notice again, we have to define the elements in our form so that in our PHP script we know how to refer to them.
All we need to do now is add in the Submit button and a few line breaks for spacing:
<html>
<head></head>
<form name="adReg" method="POST" action="adaction2.php">
Contact Name:
<br />
<input type="text" name="cName">
<br />
Notes:
<br />
<textarea name="notes"></textarea>
<br />
<input type="submit" name="submit" value="Submit">
</form>
</html>
Preview your PHP page and you should see this basic form:
That’s it! Pretty easy right? Now if you click the Submit button above, it won’t actually do anything – yet – visit back for Part 2 where we will construct the actual PHP code that will take the info from the form we created and send it via email.







Can you provide more information on this, or do you have some resources you can share where i can get more of such stuff?
Hi Tanya, and thanks for your comment. Check out Part 2 here:
http://nancyfusco.com/wp/index.php/2009/03/php-form-to-email-tutorial-part-2/
and Part 3 here:
http://nancyfusco.com/wp/index.php/2009/03/php-form-to-email-tutorial-part-3/
Let me know if that gets you heading in the right direction and what further info you’re looking for.
Nice post on the main points of general construction thought. I have been doing this for a while now and glad to see some good info.