Google Answers Logo
View Question
 
Q: Need Help on Website CGI Script ( Answered 5 out of 5 stars,   0 Comments )
Question  
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 www.dynamicsacappella.com/contact.html 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. www.dynamicsacappella.com/contact.htm
Answer  
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
 
Hi, 

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
http://php.resourceindex.com/

Hotscripts: PHP
http://www.hotscripts.com/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:
http://www.upload2.net/page/download/ua8alfqJAhsYkPo/form.php.html)

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.

---CODE BEGINS---

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<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>
</head>
<body>
<div class="main">
	
  <div class="gfx"> 
    <h1>&nbsp;</h1>
    <h1>&nbsp;</h1>

  </div>
	
  <div class="menu"> 
    <div align="center"><a href="index.html"><span><font
color="#FFFFFF">Home</font></span></a>
      <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
color="#FFFFFF">Auditions</font></span></a>
      <a href="events.htm"><span><font color="#FFFFFF">Events</font></span></a> 
      <a href="repertiore.htm"><span><font
color="#FFFFFF">Repertiore</font></span></a>
      <a href="contact.htm"><span><font color="#FFFFFF">Contact
Us</font> </span></a>
      <a href="http://www.cafepress.com/umassdynamics"><span><font
color="#FFFFFF">Dynamics
      Store</font></span></a> </font></div>

  </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>
      <?php
      do_form();
      ?>
      
    </div>
    <div class="item">
	<p><img src="img/seperate.gif" width="590" height="9" /></p>
			
    </div>
    <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:
bold;">
        at EMAIL ADDRESS<br>

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


<?php

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

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"); ?>>
          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
Event</option>
            <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>
          </select>
          Other: 
          <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>
  <dl>

    <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>
  <dl>
    <dd>

    <table>
      <tr>

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

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

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

      <tr>

                <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>
      </tr>
    </table>
    </dd>
  </dl>
        <dl>

          <dd>&nbsp;</dd>
          <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>
        </dl>
        <p>&nbsp;</p>
        <p><font color="#FFFFFF"><b><input type="submit" name="submit"
value="Submit">
  <input type="reset" value="Clear Form"></b></font></p>
        <p align="left">&nbsp;</p>

      </form>
      
      <?php
}

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

	print_r($_POST);

	$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 php.org
	$error = "";

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

	// WINDOWS ONLY
	// 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";
		draw_form();
	}
}


?>

---CODE ENDS---

Regards,
Sycophant

Request for Answer Clarification by aroz21-ga on 07 Aug 2006 04:35 PDT
I have posted your code on http://www.dynamicsacappella.com/trial.htm
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
Hi,

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
like:
$email_to = "user@domain.com"

Regards,
Sycophant

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 www.dynamicsacappella.com/bookus.php

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()

print_r($_POST);

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.

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

Comments  
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 answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


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