Jump to the home page.

Feedback form via e-mail

Updated: Aug 12, 2003 


Quick overview

One page has a form on it with one or more input tags. Submitting the form calls a PHP file which reads, filters and formats the form responses, puts it in an e-mail message to me, and loads another page in the same window. To run PHP code, your Web server needs to have PHP installed on it.

The feedback form

In the code for the feedback page are these lines:

 1.  <script>
theform.NAME.focus();
</script>
2. <form name="theform" action="..\mail-survey.php" method="post"> ... 3. <input type="submit" name="button" value="* Click here to send your survey responses *" style="font: bold 13px Verdana, Arial, Helvetica, sans-serif; padding: 6px 0px; width: 340px">


Purpose of each line

  1. Puts the cursor in the page rather than in the address bar.
  2. Defines this section of a code as a form with method POST (meaning, pass along the responses internally, rather than in the address bar), and when the form is submitted, call a PHP file called MAIL-SURVEY.
  3. Make a submit button for the form. The button has custom settings for text, padding and width rather than the generic text, dimensions and "Submit" text.

 

The mail processor

In the code for the MAIL-SURVEY.PHP file are these lines:

 1.  <?
2. // help from dimonemon on codewalkers.com
// 10-Jul-03
//
3. $msg="Survey results.\n\n";
4. foreach($_POST as $key => $val)
5. if ($val != "")
$msg.="$key = $val\n"; 6. $msg.="\n== END ==\n";
7. mail( " [e-mail] ", "*survey responses*", $msg, "From: guest"); 8. header( "Location: http://www.therotunda.net/survey_thanks.html" ); 9. ?>


Purpose of each line

  1. (and 9.) Define this section of code as PHP.
  2. A comment. In this case, who helped me figure this out.
  3. Assign "beginning" text to the variable $msg (which will hold the content of the e-mail message).
  4. For each key+value combination inside the thing called $POST (more on this later)...
  5. If the value isn't blank, add the text of the key+value combination to $msg with a new line.
  6. Add "ending" text to $msg.
  7. Mail TO {my address}, with certain text for the SUBJECT field, the text inside $msg as the CONTENT, and with certain text for the FROM field.
  8. Load another page in this window (Basically it says "Thanks for taking my survey, here are some links to other pages on my site that you might be interested in.")

About keys and (response) values

The key in the form is whatever you label that INPUT tag using the name setting.

<input name="AGE" type="text" size="3" maxlength="3">

In the example above, the key is AGE. The value is whatever the user puts in this box (let's say the user enters 29) when they submit the form.

PHP sees this as AGE => 29 ... where the => is an assignment symbol.

About $_POST

In the FORM tag, one of the settings is method="post". The other option is "get". With GET, the key-value pairs are added to the end of the page name in the address bar. With POST, using PHP they can be retrieved from the array called $_POST.

Closing

Was this article worthwhile? See the menu at upper right for more code used on this site - including how to do the menu. If you use these ideas on your site, I'd love to know.

 

My nifty email form stopped working ... but you can still send me email if you would like to.

Original page posted: August 12, 2003. Last tweaked: July 1, 2006.

The address for this page is [ www.therotunda.net/code/email-feedback-form.html ]
 
Nothing on my Web site is the official publication of anyone else. Unauthorized use for profit is not permitted.