Google Answers Logo
View Question
Q: Need Help on Website CGI Script ( Answered 5 out of 5 stars,   0 Comments )
Subject: Need Help on Website CGI Script
Category: Computers > Programming
Asked by: aroz21-ga
List Price: $5.00
Posted: 06 Aug 2006 14:03 PDT
Expires: 05 Sep 2006 14:03 PDT
Question ID: 753185
On my website I have put up a
form that I want to send mail to me when the form is filled out and
submitted. I am looking for someone who will be able to write the cgi
script or any script that will be supported like asp or php or perl
ect. I need this script tailored to my form to get it to work. You can
view source and see what I have so far. I am not looking for resources
on how to fix this, I am look for an answer on how to fix it to get it
to work. Perribly step by step in easy instructions that a novice in
this area can understand. Thank you!

Clarification of Question by aroz21-ga on 06 Aug 2006 21:00 PDT
The address is htm not html.
Subject: Re: Need Help on Website CGI Script
Answered By: sycophant-ga on 07 Aug 2006 02:46 PDT
Rated:5 out of 5 stars

A form mail script is very simple. There are a number of applications
that will do what you want, good places to look for these types of
things (in PHP anyway) are:
PHP Resource Index

Hotscripts: PHP

However, as you have asked for a customised solution, here is a script
I have written for your form as it exists on that page (if you have
problems copying and pasting the following code, please check here:

Please note, I have removed email addressed to comply with Google Answers terms. 

They can all be replaced exactly as they are in your current version,
also the '$email_to' address will need to be filled in.

It is fairly simple and should be easy to change later if you like. 

I have changed the names of the form fields for clarity. 

Let me know if you have any questions.


<meta http-equiv="Dynamics college umass acapella acappella sing
singer group university best Massachusetts coed co-ed music good great
group" content="text/html; charset=iso-8859-1"/>
<meta name="Dynamics college umass acapella acappella sing singer
group university best Massachusetts coed co-ed music good great group"
content="Dynamics college umass acapella acappella sing singer group
university best Massachusetts coed co-ed music good great group"/>
<meta name="Dynamics college umass acapella acappella sing singer
group university best Massachusetts coed co-ed music good great group"
content="Dynamics college umass acapella acappella sing singer group
university best Massachusetts coed co-ed music good great group"/>
<meta name="Dynamics college umass acapella acappella sing singer
group university best Massachusetts coed co-ed music good great group"
content="Dynamics college umass acapella acappella sing singer group
university best Massachusetts coed co-ed music good great group"/>
<link rel="stylesheet" type="text/css" href="default.css"/>
<title>Dynamics Acappella: Umass's Hottest Acappella Group</title>
<div class="main">
  <div class="gfx"> 

  <div class="menu"> 
    <div align="center"><a href="index.html"><span><font
      <a href="aboutus.htm"><span><font color="#FFFFFF">About Us</font></span></a> 
      <a href="members.htm"><span><font color="#FFFFFF">Members</font></span></a> 
      <a href="auditions.htm" id="last"><span><font
      <a href="events.htm"><span><font color="#FFFFFF">Events</font></span></a> 
      <a href="repertiore.htm"><span><font
      <a href="contact.htm"><span><font color="#FFFFFF">Contact
Us</font> </span></a>
      <a href=""><span><font
      Store</font></span></a> </font></div>

	<div class="content">
    <div class="item"> 
      <h1><img src="img/contact.gif" width="117" height="21" /></h1>
      <p><img src="img/seperate.gif" width="590" height="9" /></p>
      <p><font color="#FFFFFF"><strong>Please do not hesitate to
contact us</strong>
        with any questions you have about the group, our performances
and recordings,
        or in regards to auditioning.</font></p>
      <p><font color="#FFFFFF"><strong>General Group E-mail</strong></font></p>

      <p>EMAIL ADDRESS</p>
      <p><font color="#FFFFFF"><strong>General Manager</strong></font></p>
      <p><font color="#FFFFFF">Andrew Rozynski - EMAIL ADDRESS</font></p>
      <p><font color="#FFFFFF"><strong>Music Director</strong></font></p>
      <p><font color="#FFFFFF">Emily Hall - EMAIL ADDRESS</font></p>

      <p><img src="img/seperate.gif" width="590" height="9" /></p>
      <p><font color="#FFFFFF"><strong>Booking information:</strong> The Dynamics 
        love to sing and spread the joy of singing to others. Because of this, 
        we offer extremely competitive, flexible rates. For your next
party, conference,
        wedding, anniversary celebration or other social engagement, consider 
        hiring an a cappella group! We are available for singing engagements in 
        the Amherst/Hartford/Boston area throughout the academic year, and tour 
        around the nation during academic break.</font></p>
      <p><img src="img/seperate.gif" width="590" height="9" /></p>
      <p><font color="#FFFFFF"><img src="img/form.gif" width="137"
height="24" /></font></p>
    <div class="item">
	<p><img src="img/seperate.gif" width="590" height="9" /></p>
    <div class="item">
      <h1><font color="#FFFFFF">If you would like the Dynamics to sing at your 
        next Benefit, Fund Raiser, or Show please Contact
Us&nbsp;</span></font><font color="#000000"><span style="font-weight:
        at EMAIL ADDRESS<br>

        </span> </font><BR>
  <div class="footer">Website and All contents &copy; Dynamics Acappella 2006 


function do_form() {
	echo "<h1> Submit: ".$_POST["submit"]."</h1>";
	if (!empty($_POST["submit"])) {
	} else {

function get_val($varname) {
	if (!empty($_POST[$varname])) {
		echo $_POST[$varname];

function get_checked($varname,$value,$default=FALSE) {
	if (empty($_POST[$varname]) && $default) {
		echo "checked";
	} elseif (strtolower($_POST[$varname]) == strtolower($value)) {
		echo "checked";

function get_selected($varname,$value,$default=FALSE) {
	if (empty($_POST[$varname]) && $default) {
		echo "selected";
	} elseif (strtolower($_POST[$varname]) == strtolower($value)) {
		echo "selected";

function draw_form() {

	    <form method="POST" action="<? echo $_SERVER['PHP_SELF']; ?>">

        <p><img src="img/howlong.gif" width="434" height="16" /></p>
        <p><font color="#FFFFFF"><b> 
          <input type="radio" name="Event_Dur" value="Serenade" <?
get_checked("Event_Dur","Serenade"); ?>>
          <input type="radio" name="Event_Dur" value="Few_Songs" <?
get_checked("Event_Dur","Few_Songs"); ?>>
          2-3 Songs 
          <input type="radio" name="Event_Dur" value="Full_Concert" <?
get_checked("Event_Dur","Full_Concert",TRUE); ?>>
          Full Concert 
          <input type="radio" name="Event_Dur" value="Special_Event"
<? get_checked("Event_Dur","Special_Event"); ?>>
          Special Event</b></font></p>

        <p><font color="#FFFFFF" size="2"><img src="img/whattype.gif"
width="418" height="16" /></font></p>
        <p><font color="#FFFFFF"><b>
          <select name="Event_Type" size="1">
            <option value="Company" <?
get_selected("Event_Type","Company",TRUE); ?>>Company Event</option>
            <option value="High_School" <?
get_selected("Event_Type","High_School"); ?>>High School
            <option value="Charity" <?
get_selected("Event_Type","Charity"); ?>>Charity Event</option>
            <option value="College" <?
get_selected("Event_Type","College"); ?>>College Event</option>
            <option value="Sporting" <?
get_selected("Event_Type","Sporting"); ?>>Sporting Event</option>
            <option value="Other" <?
get_selected("Event_Type","Other"); ?>>(Other)</option>
          <input type="text" size="26" maxlength="256"
name="Other_Detail" value="<? get_val("Other_Detail"); ?>">
          </b></font></p>      <p>&nbsp;</p>
        <p><img src="img/please.gif" width="398" height="16" /></p>

    <dd><font color="#FFFFFF"><b><textarea name="Comments" rows="5"
cols="42"><? get_val("Comments"); ?></textarea></b></font></dd>
  </dl>      <p>&nbsp;</p>
        <p><font color="#FFFFFF" size="2"><strong><img
src="img/tellus.gif" width="230" height="16" /></strong></font></p>


        <td><b><font color="#FFFFFF" face="Times New Roman, Times,
        <td><font color="#FFFFFF"><b>
        <input type="text" size="35" maxlength="256" name="Name"
value="<? get_val("Name"); ?>"></b></font></td>
        <td><b><font color="#FFFFFF" face="Times New Roman, Times,

        <td><font color="#FFFFFF"><b>

        <input type="text" size="35" maxlength="256" name="Email"
value="<? get_val("Email"); ?>"></b></font></td>
        <td><b><font color="#FFFFFF" face="Times New Roman, Times,
        <td><font color="#FFFFFF"><b>
        <input type="text" size="35" maxlength="256" name="Telephone"
value="<? get_val("Telephone"); ?>"></b></font></td>


                <td><b><font color="#FFFFFF" face="Times New Roman,
Times, serif">Fax</font></b></td>
        <td><font color="#FFFFFF"><b>
        <input type="text" size="35" maxlength="256" name="Fax"
value="<? get_val("Fax"); ?>"></b></font></td>

          <dt><font color="#FFFFFF"><b> 
            <input type="checkbox" name="Contact_Requested"
value="Yes" <? get_checked("Contact_Requested","Yes"); ?>>
            Please contact us as soon as possible for this gig.</b></font></dt>
        <p><font color="#FFFFFF"><b><input type="submit" name="submit"
  <input type="reset" value="Clear Form"></b></font></p>
        <p align="left">&nbsp;</p>


function process_form() {
	$variables = array("Name","Email","Telephone","Fax","Event_Dur","Event_Type","Other_Detail","Contact_Requested");
	$required = array("Name","Email");


	$email_to = "EMAIL ADDRESS";
	$email_subject = "Website Contact Form";
	$email_from = $_POST["Name"] ." <". $_POST["Email"] .">";
	$email_headers = "From: $email_from\r\n";
	$time = date("h:i:sa d-M-y"); // See 'date' docunmentation at
	$error = "";

	$success_message = "Thank you. Your information has been received. We
will be in contact shortly.";

	// If the webserver is running Windows, uncomment the following two
lines and replace <<SMTP SERVER>> with a
	// valid SMTP server address.

	// ini_set("SMTP","<<SMTP SERVER>>");
	// ini_set("sendmail_from",$_POST["Email"]);

	$email_body = "A visitor to the website submitted a contact form at
$time with the following information: \r\n";

	foreach ($variables as $var) {
		if (empty($_POST[$var])) {
			$value = "EMPTY";
			if (in_array($var,$required)) {
				$error .= "<b>$var</b> needs to be filled out.<br />\n";
		} else {
			$value = $_POST[$var];
		$email_body .= str_replace("_"," ",$var).": $value\r\n";


	$email_body .= "\r\n\r\nRemote IP Address: ".$_REQUEST["REMOTE_ADDR"]."\r\n";

	if (empty($error)) {
		$success = mail($email_to,$email_subject,$email_body,$email_headers);
		if ($success) {
			echo $success_message;
		} else {
			echo "<b>An error occured sending your form. We cannot deliver it
at present. Please contact one of us directly by email instead.</b>";
	} else {
		echo "<b>Please correct the following errors, then try again:</b><br />$error";




Request for Answer Clarification by aroz21-ga on 07 Aug 2006 04:35 PDT
I have posted your code on
however it does not look or work properly, Could you please view
source on this and clarify what I need to do? I copy and pasted the
code and it does not work.

Clarification of Answer by sycophant-ga on 07 Aug 2006 05:39 PDT

Your webserver is not parsing the code as PHP. 

You will probably need to rename the file with a .php extension. 

If this does not work, contact your hosting provider to ensure PHP is
enabled for your site, and check if any other changes should be made
to allow it to be processed.

The script requires no external libraries or classes and uses only
core PHP commands.

Also, you have not set the '$email_to' variable in the script. It
needs a valid email address or no email will be received. Something
$email_to = ""


Request for Answer Clarification by aroz21-ga on 15 Aug 2006 14:00 PDT
The php is working now, but the confirmation page looks like it is all
over the place. Could you please fix the code so it just says Thank
you for you submission. And not what looks like 10 lines of php code?
The website is

Clarification of Answer by sycophant-ga on 15 Aug 2006 17:39 PDT
Sorry about that, I seem to have left some debug commands in the code
I pasted here.

They two lines that should be removed are in do_form()

echo "<h1> Submit: ".$_POST["submit"]."</h1>";

and process_form()


Once those two are changed it should simply return what is defined in
$success_message - which can contain HTML if you would like to further
style it.

aroz21-ga rated this answer:5 out of 5 stars
Sycophant was very helpful and knowledgeable and quickly tended to my
question and clarifacations.

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy