I have a simple Contact form script. I want to change it so it shows the email address of the person sending the form, not mine. I also hope I am pasting this code correctly. Sometimes I get a blank form, but all fields are required. I would like to know who is sending me the form. Also, how can the person sending the form get an email copy of what I receive? In case they entered something wrong? Thanks in advance.

Here is the code:

<?php


// Simple Form Script
// Copyright (C) 2005  Eric Zhang
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Please send bugs/questions to erkzh@yahoo.com.


//--------------------------Set these paramaters--------------------------



$subject = 'Game Score Report';                // Subject of email sent to you.
$emailadd = 'bpavone@glghl.com';        // Your email address. This is where the form information will be sent.
$url = 'http://www.glghl.com/thank_you.html';               // Where to redirect after form is processed.
$req = '1';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.


// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = '  ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 45)
{echo "Name of form element $key cannot be longer than 45 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = '  ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

Edited 3 Years Ago by happygeek: fixed formatting

Just edit the from field to show their email address. You will need to add another form element to your contact form then change the folowing code:

mail($emailadd, $subject, $text, 'From: '.$emailadd.'');

To:

mail($emailadd, $subject, $text, 'From: '.$emailfrom.''); 
//set your from address to $emailfrom 
//this will get its value from the form

To email both you and the user just add another mail function like:

mail($emailadd, $subject, $text, 'From: '.$emailfrom.''); //email admin
mail($emailfrom, $subject, $text, 'From: '.$emailadd.''); //email user

HTH
StatiX

Thanks for your quick reply. I appreciate that. I have attached the html in notepad of the form I use. And I attached a copy of the script with the changes you suggested. When I changed the code the way you told me to, I still receive the form, only there is no email address from the person sending it. It comes from "Unknown Sender" and then the sender of the form does not get an email copy of the form. Thanks in advance. Bill

Attachments
<HTML>
<HEAD>
  <META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Win">
  <TITLE>GLGHL - Representative Contact Form</TITLE>
</HEAD>
<BODY style="background-color: #FFFFFF; background-image: url(images187/Ice.jpg);">

<FORM>
<P><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;">
</FORM>
<FORM NAME="form" METHOD="POST" ACTION="/contactform/contactscript1.php"
ENCTYPE="x-www-form-urlencoded">
</P>

<P><IMG SRC="images187/GLGHLLOGOTransNew.gif" WIDTH="576" HEIGHT="149"
ALIGN="BOTTOM" BORDER="0" NATURALSIZEFLAG="3"></P>

<H3><FONT FACE="Verdana">GLGHL Representative Contact Info Form</FONT></H3>

<P><TABLE WIDTH="525" BORDER="1" CELLSPACING="0" CELLPADDING="4">
  <TR>
    <TD WIDTH="100%">
    <FONT SIZE="-1" FACE="Verdana">Please submit this form if you
    are new to the league in order for other teams or the league
    to contact you when necessary. All fields are required. <B>You
    must include at least 1 phone number so people can reach you.</B>
    Use the text area below for any other info you wish to submit.&nbsp;Put
    N/A in a field you can't use. Thank you.</FONT></TD> 
  </TR>
</TABLE></P>

<P></FORM>
<FORM NAME="form" METHOD="POST" ACTION="/contactform/simplescript1.php"
ENCTYPE="x-www-form-urlencoded">
<TABLE BORDER="1" CELLSPACING="1" style="border-collapse: collapse"
bordercolor="#000066" WIDTH="95%" CELLPADDING="5">
  <TR>
    <TD COLSPAN="3" BGCOLOR="#b5cbef" HEIGHT="17" bordercolor="#FFFFFF"
    BACKGROUND="tile_back.gif">
    <P ALIGN="LEFT\&gt;&lt;B&gt;&lt;FONT FACE=" Verdana" SIZE="2"
    COLOR="#FFFFFF"><B><FONT COLOR="#ffffff" SIZE="-1" FACE="Verdana"><!--- You can add a form title here --></FONT><FONT
     COLOR="#ffffff" FACE="Verdana">GLGHL Representative Contact
    Information Form &nbsp;</FONT></B></TD>
     
  </TR>
  <TR>
    <TD COLSPAN="3" BGCOLOR="#b5cbef" HEIGHT="16" bordercolor="#FFFFFF"
    BACKGROUND="tile_sub.gif">
    <B><FONT COLOR="#000066" SIZE="-1" FACE="Verdana"><!--- You can add a brief form description here-->Use
    this form to update your current contact information. &nbsp;</FONT></B></TD>
     
  </TR>
  <TR>
    <TD COLSPAN="3" BGCOLOR="#d6dfef" HEIGHT="16" bordercolor="#FFFFFF">
    <FONT SIZE="-2" FACE="Verdana">Please fill in all fields marked
    with a * (all fields are required)</FONT></TD>
     
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Position</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><SELECT NAME="Position:">
    <OPTION VALUE="Head Coach" SELECTED>Head Coach
    <OPTION VALUE="Asst. Coach">Asst. Coach
    <OPTION VALUE="Manager">Manager
    <OPTION VALUE="Div. Commissioner">Div. Commissioner
    <OPTION VALUE="Other">Other
    </SELECT> *</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">If Other</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Other Position:"
    SIZE="30"> *</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Your Name</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Name" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Your Address</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Address" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">City</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="City" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">State</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="State" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Zip Postal Code</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Zip" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Email Address</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Email" SIZE="30">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Home Phone Number</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Home Phone" SIZE="30">*
    </FONT><FONT SIZE="-2" FACE="Verdana">(With Area Code)</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Work Phone Number</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Work Phone" SIZE="30">*
    </FONT><FONT SIZE="-2" FACE="Verdana">(With Area Code)</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Cell Phone Number</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><INPUT TYPE="text" NAME="Cell Phone" SIZE="30">*
    </FONT><FONT SIZE="-2" FACE="Verdana">(With Area Code)</FONT><FONT
     FACE="Verdana">*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Division</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><SELECT NAME="Division">
    <OPTION VALUE="12U" SELECTED>12U
    <OPTION VALUE="14U">14U
    <OPTION VALUE="16U">16U
    <OPTION VALUE="19U">19U
    <OPTION VALUE="Red">Red
    <OPTION VALUE="White">White
    <OPTION VALUE="Blue">Blue
    </SELECT>*</FONT></TD> 
  </TR>
  <TR>
    <TD HEIGHT="30" WIDTH="57" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <IMG SRC="images187/bc_new.gif" WIDTH="28" HEIGHT="28" ALIGN="BOTTOM"
    BORDER="0" NATURALSIZEFLAG="3">
</TD>
    <TD HEIGHT="30" WIDTH="196" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT SIZE="-1" FACE="Verdana">Your Team</FONT></TD> 
    <TD HEIGHT="30" WIDTH="486" BGCOLOR="#eff3f7" bordercolor="#FFFFFF">
    <FONT FACE="Verdana"><SELECT NAME="Home Team">
    <OPTION VALUE="Amherst" SELECTED>Amherst
    <OPTION VALUE="Buffalo Hornets">Buffalo Hornets
    <OPTION VALUE="Buffalo Regals">Buffalo Regals
    <OPTION VALUE="Camillius">Camillius
    <OPTION VALUE="Canandaigua">Canandaigua
    <OPTION VALUE="Cazenovia">Cazenovia
    <OPTION VALUE="Erie">Erie
    <OPTION VALUE="Fredonia">Fredonia
    <OPTION VALUE="Geneva">Geneva
    <OPTION VALUE="Cleveland">Cleveland
    <OPTION VALUE="Hamburg">Hamburg
    <OPTION VALUE="Jamestown">Jamestown
    <OPTION VALUE="Mohawk Valley">Mohawk Valley
    <OPTION VALUE="Monroe">Monroe
    <OPTION VALUE="Niagara Coyotes">Niagara Coyotes
    <OPTION VALUE="Niagara Purple Eagles">Niagara Purple Eagles
<?php

// Simple Form Script
// Copyright (C) 2005  Eric Zhang
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// Please send bugs/questions to erkzh@yahoo.com.

//--------------------------Set these paramaters--------------------------


$subject = 'GLGHL Representative Contact Form';                // Subject of email sent to you.
$emailadd = 'bpavone@glghl.com';        // Your email address. This is where the form information will be sent.
$url = 'http://www.glghl.com/thank_you.html';               // Where to redirect after form is processed.
$req = '1';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";       
$space = '  ';
$line = '
';
foreach ($_POST as $key => $value)
{
	if ($req == '1')
	{
		if ($value == '')
		{echo "$key is empty";die;}
	}
	$j = strlen($key);
		if ($j >= 45)
		{echo "Name of form element $key cannot be longer than 45 characters";die;}
	$j = 20 - $j;
		for ($i = 1; $i <= $j; $i++)
		{$space .= ' ';}
	$value = str_replace('\n', "$line", $value);
	$conc = "{$key}:$space{$value}$line";
	$text .= $conc;
	$space = '  ';
}
mail($emailadd, $subject, $text, 'From: '.$emailfrom.''); //email admin
mail($emailfrom, $subject, $text, 'From: '.$emailadd.''); //email user
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

From the contact form the user enters a value for their email and is stored in variable $Email. It looks like you just need to use it in your mail function like this:

mail($emailadd, $subject, $text, 'From: '.$Email.''); //email admin
mail($Email, $subject, $text, 'From: '.$emailadd.''); //email user

Note that the variable email is "Email" and not "email". If your server is linux or unix, the capital letter will make a difference.

Let me know how it goes

Regards,
StatiX

Still the same. I still get the form but there is is still no return email address and the sender still gets no email.

I want to add that the form has a .html extension and is not in the same file as the script. Obviously it is a simple script that was provided with the hosting I have through directadmin. I can change anything that is requested. The website is glghl.com and I have 2 forms on it. http://www.glghl.com/GLGHL_Representative_Contact_Form.html and http://www.glghl.com/GLGHL_Game_Score_Form.html

In a nutshell, I want the above attached form to be emailed to me and back to the person who sent it so they can see what they sent me. I also want it to come from their email address, so in case I get a blank form, which has happened 3 times already. I have no idea why. Right now it says I sent the form and if it comes in blank, I have no idea who sent it. I also have all fields required and it can stay that way. The reason I want them to receive an email is because I was told it gets complicated to do code to show them what they entered so they can then send after they check what they filled in.

This is for a hockey league I am running and need help as soon as I can get it. Thanks all in advance.

If someone has a different but simple script, I can change it to let me know.

Add this code to the bottom of the .php script:

echo '<META HTTP-EQUIV=Refresh CONTENT="30; URL='.$url.'">';
echo "<b><br />emailadd var: " .$emailadd;
echo "<br />subject: ".$subject;
echo "<br />text: ".$text;
echo "<br />from: ".$Email;
echo "</b>";

?>

Find out what the vars hold and post them. to see if its not getting set correctly

echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
echo '<META HTTP-EQUIV=Refresh CONTENT="30; URL='.$url.'">';
echo "<b><br />emailadd var: " .$emailadd;
echo "<br />subject: ".$subject;
echo "<br />text: ".$text;
echo "<br />from: ".$Email;
echo "</b>";


It seemed to change and add the one line. Other than that I have no idea what you are talking about. Thanks again. I am going to look for a book to understand all this. I know html but thats it.

This article has been dead for over six months. Start a new discussion instead.