php form for web sites

Posted on 2013-01-08
Last Modified: 2013-01-09
I downloaded  a form to put on my web site , I need help figuring out how to send  the information to me, I  am attaching the file.
thank you
Question by:imagekrazy
LVL 18

Assisted Solution

nap0leon earned 100 total points
ID: 38756058
Depends on your hosting provider.

GoDaddy, for example, has a feature that let's you tell them to "send this form to me in an email" and it automatically attaches itself to whatever form (or forms!) on your site you want it to.

They also have sample code that you can use so that you can submit the form that you attached to a page that processes the form's information and sends it to you in an email.  

I could write you up some PHP for how to send an email to yourself, but a lot of the information is going to come from your hosting provider anyway... so  you really should start there.

Accepted Solution

wadehults earned 400 total points
ID: 38757097
Here you go:

Normally I wouldn't post this much code, but I have this method saved as a snippet in my Dreamweaver snippet files. I just modified mine to match your form fields.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Form</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
<script type="text/javascript" src="calendar.js"></script>

//initialize form variables
$element_2_1 = $element_2_2 = $element_2_2 = $element_5 = $element_1 = $element_3_1 = $element_3_2 = $element_3_3 = $element_4 = $form_id = $saveForm = "";
$emailErr = "";	
	//check page for load or submit
		//verify correct form id has been submitted to reduce Cross-Site Scripting
		if (isset($_POST["form_id"]))	{
			if ($_POST["form_id"] === "550034")	{
				//validate email field using regular expression
				$normal = "^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$^"; 
				$validButRare = "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$^";
				$email = $_POST["element_4"];
				if (preg_match($normal, $email)===1)  {
					$email = $_POST["element_4"];
					else if (preg_match($validButRare, $email)===1)	{
						$email = $_POST["element_4"];
						$emailErr = "The address ".$email." looks a bit strange but it is syntactically valid. You might want to check it for typos.";
					else	{
						$email = $_POST["element_4"];
						$emailErr = "Invalid email format";
				if ($emailErr <> "Invalid email format")	{	
					//process email for delivery
					$sendto = "{your email address}";
					$sendto .= ",";
					$sendto .= $email;
					$subject = $_POST["form_id"];
					//assemble email
					$message = "
								<table border='1' cellpadding='2'>
										<td>Form ID</td>
										<td>First Name</td>
										<td>Last Name</td>
										<td>Email Address</td>
										<td>Type of Car</td>
										<td>How we can help</td>
										<td>Date needed</td>
										<td>$element_3_1 $element_3_2 , $element_3_3</td>
						$mime_boundary = "----BOUNDARY HEADER----".time();
						$headers = "From: $element_2_1 $element_2_2 <$element_4>" . "\r\n";
						$headers .= "Reply-To: $element_2_1 $element_2_2 <$element_4>" . "\r\n";
						$headers .= "Return-Path: {your email address}" . "\r\n";
						$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
						$headers .= "MIME-Version: 1.0" . "\r\n";
						$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
						$headers .= "Organization: {Name of your company}";
						mail($sendto, $subject, $message, $headers);
						foreach($_POST as $value)	{
							$value = "";
	//if the form was not submitted, do nothing 
<body id="main_body" >
	<img id="top" src="top.png" alt="">
	<div id="form_container">
		<h1><a>AAA1 Hudson Limo</a></h1>
		<form id="form_550034" class="appnitro"  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
					<div class="form_description">
			<h2>AAA1 Hudson Limo</h2>
			<ul >
					<li id="li_2" >
		<label class="description" for="element_2">Name </label>
			<input id="element_2_1" name= "element_2_1" class="element text" maxlength="255" size="8" value=""/>
			<input id="element_2_2" name= "element_2_2" class="element text" maxlength="255" size="14" value=""/>
		</li>		<li id="li_5" >
		<label class="description" for="element_5">Drop Down </label>
		<select class="element select medium" id="element_5" name="element_5"> 
			<option value="" selected="selected"></option>
<option value="1" >Sedan</option>
<option value="2" >Limo</option>

		</li>		<li id="li_1" >
		<label class="description" for="element_1">How may we help you </label>
			<textarea id="element_1" name="element_1" class="element textarea medium"></textarea> 
		</li>		<li id="li_3" >
		<label class="description" for="element_3">Date/when is the special occasion </label>
			<input id="element_3_1" name="element_3_1" class="element text" size="2" maxlength="2" value="" type="text"> /
			<label for="element_3_1">MM</label>
			<input id="element_3_2" name="element_3_2" class="element text" size="2" maxlength="2" value="" type="text"> /
			<label for="element_3_2">DD</label>
	 		<input id="element_3_3" name="element_3_3" class="element text" size="4" maxlength="4" value="" type="text">
			<label for="element_3_3">YYYY</label>
		<span id="calendar_3">
			<img id="cal_img_3" class="datepicker" src="calendar.gif" alt="Pick a date.">	
		<script type="text/javascript">
			inputField	 : "element_3_3",
			baseField    : "element_3",
			displayArea  : "calendar_3",
			button		 : "cal_img_3",
			ifFormat	 : "%B %e, %Y",
			onSelect	 : selectDate
		</li>		<li id="li_4" >
		<label class="description" for="element_4">Email </label>
			<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/> <?php echo $emailErr ?>
					<li class="buttons">
			    <input type="hidden" name="form_id" value="550034" />
				<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
		<div id="footer">
	<img id="bottom" src="bottom.png" alt="">

Open in new window

You will notice that lines 10-100 are all PHP script elements. These are essential to creating output from your form if you are not going to use hosting service form mailers. As napoleon stated earlier, GoDaddy has a script built for form results, but it is extremely limited as to whom the emails may be delivered (only a single address). When you use the PHP mail() function, you can specify any email address desired.

You will also notice that I included a regular expression method which validates the email address. I found this wonderful snippet elsewhere and love it.

You will still need to sift through the code to find places in curly braces {  } where I have put a place holder for your personal information. Specifically in the header for your email address and your organization name.

Also notice that I modified your form action to read: action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" . This makes your page self-processing.

IMPORTANT!! -- you will need to save your file as a PHP, not HTML, in order for these processes to work properly. Otherwise, you will need to place the PHP script into a separate file and point to it from your form action=""

Additional note: You should rename your form names/id's with more intuitive names instead of element**_**. It makes your scripting a bit easier since you won't need to be constantly checking a reference sheet to find which variables are which.

Author Closing Comment

ID: 38759762
thank you

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question