Page 1 of 2

Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 1:02 pm
by Misty
I love to use Classic ASP. But I cannot use ASP on one of my clients' web hosting plans because it doesn't support ASP so I have to use PHP. I don't know that much PHP.

Please look at http://www.stellohomeservices.com/contact.htm. This form is supposed to post to a PHP file. I got a simple PHP file, but it doesn't submit all of the results.

I would like for all of the fields such as the check boxes to be included in the results. I would like for there to be a <br> tag between each field. I also would like for the email that is sent to show the person's name and the email address as from on the email. I don't want it to just say it's from the email address.

Here's the simple PHP File I have so far:

Code: Select all

 
<?php
$to = "misty@you.com";
$subject = "Test Email From PHP Contact Me Form";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>
 
 

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 1:12 pm
by aceconcepts
As you're using a POST method for your form, use $_POST to get your fields:

Code: Select all

$email=$_POST['email'];
Also, are you hoping to get name from email address?

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 1:15 pm
by Misty
Yes, I want both name and email address to be displayed on the email that the person receives.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 1:52 pm
by mukunthan
<?php //SET HEADERS TO ACCEPT A HTML MAIL
$headers = "MIME-Version: 1.0"."\n";
$headers .= "Content-type: text/html; charset=utf-8"."\n";
$headers .= "Content-Transfer-Encoding: 8bit"."\n";
$headers .= "From: $from_email"."\n";
$headers .= "Return-Path: $from_email"."\n";
$headers .= "Reply-To: $from_email";

//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
$message ="Name:".$_POST["name"]."<br />";
$message .="Email:".$_POST["email"]."<br />";

mail($to,$subject,$message,$headers);?>

Hi,
Hope this felps
U can even build a HML table embedded with the form fileds instead of simple "<br>"tag
$message .="<table border=0><tr><td>Name:</td><td>".$_POST["name"]."</td></tr><table>";

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 2:20 pm
by Misty
THis is not exactly what I wanted.

Here's a copy of the email I received for the form below:

From: stelloho@host165.hostmonster.com [mailto:stelloho@host165.hostmonster.com]
Sent: Friday, October 10, 2008 3:16 PM
To: misty@myaddress.com
Subject: Test Email From PHP Contact Me Form

Name: Misty
Email: info@anywhere.com

I am not happy with how the from field looks. It should say Misty [info@anywhere.com) based on how the form was filled.

Can someone please help me to fix this? You can see my latest code below.

Code: Select all

 
 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
$to = "misty@myaddress.com";
$subject = "Test Email From PHP Contact Me Form";
$headers = "MIME-Version: 1.0"."\n";
$headers .= "Content-type: text/html; charset=utf-8"."\n";
$headers .= "Content-Transfer-Encoding: 8bit"."\n";
[b]$headers .= "From: $from_email"."\n";
$headers .= "Return-Path: $from_email"."\n";
$headers .= "Reply-To: $from_email";[/b]
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
$message ="Name:&nbsp;".$_POST["txtName"]."<br />";
$message .="Email:&nbsp;".$_POST["txtEmail"]."<br />";
 
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>
 

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 2:45 pm
by koen.h
$headers .= "From: $from_email"."\n";

to

$headers .= "From: whateveryouwanttoappear\n";

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 2:52 pm
by Misty
No, that is not what I wanted. I want the from field to say it is from the person (txtName) and email address (txtEmail) that was filled out on the form.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 3:08 pm
by koen.h
Then you'll have to use the $_POST["name"] and $_POST["email"] variables.

$from_email = $_POST["name"] . '[' . $_POST["name"] . ']';
$headers .= "From: $from_email"."\n";

Now that I come to think of it, I'm not sure this will work though in email headers. I would like to see the results.

Also filter the $_POST variables first.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 3:36 pm
by Misty
koen.h wrote:Then you'll have to use the $_POST["name"] and $_POST["email"] variables.

$from_email = $_POST["name"] . '[' . $_POST["name"] . ']';
$headers .= "From: $from_email"."\n";

Now that I come to think of it, I'm not sure this will work though in email headers. I would like to see the results.

Also filter the $_POST variables first.
That did not work. I got the below message:

We encountered an error sending your mail

How do I filter the $_Post variables first? I am new at PHP.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 3:46 pm
by koen.h
We'll first try to get the mail script working before adding the input check. Can you post the script you have so far here?

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 4:07 pm
by Misty
koen.h wrote:We'll first try to get the mail script working before adding the input check. Can you post the script you have so far here?
Here's my code:

Code: Select all

 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
$to = "misty@you.com";
$subject = "Test Email From PHP Contact Me Form";
$headers = "MIME-Version: 1.0"."\n";
$headers .= "Content-type: text/html; charset=utf-8"."\n";
$headers .= "Content-Transfer-Encoding: 8bit"."\n";
$from_email = $_POST["name"] . '[' . $_POST["name"] . ']';
$headers .= "From: $from_email"."\n";
$headers .= "Reply-To: $from_email";
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
$message ="Name:&nbsp;".$_POST["txtName"]."<br />";
$message .="Email:&nbsp;".$_POST["txtEmail"]."<br />";
 
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>
 

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 4:23 pm
by koen.h
What does this say?

Code: Select all

 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
$to = "misty@you.com";
$subject = "Test Email From PHP Contact Me Form";
$headers = "MIME-Version: 1.0"."\n";
$headers .= "Content-type: text/html; charset=utf-8"."\n";
$headers .= "Content-Transfer-Encoding: 8bit"."\n";
$headers .= "From: " . $_POST['txtName'] . "<" . $_POST['txtEmail'] . ">"."\n";
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
$message ="Name:&nbsp;".$_POST["txtName"]."<br />";
$message .="Email:&nbsp;".$_POST["txtEmail"]."<br />";
 
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>
 
I assume the names of your input fields are txtName, txtEmail.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 4:33 pm
by aceconcepts
Show your form from which the data is submitted.

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 4:38 pm
by Misty
aceconcepts wrote:Show your form from which the data is submitted.
It's long. But I am only concentrating on getting two fields submitted to Contact.php at this time.

Code: Select all

 
<html>
<head>
 
<title>Contact Information</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" /> 
<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
 
  
function InputIsValid() 
 {  
 
 //Check for missing Name   
    if (document.frmContact.txtName.value == "")
    {
        alert("Please include your name.");
        document.frmContact.txtName.focus();
        return false;
    }       
    
    
    //Check for missing Address
    if (document.frmContact.txtAddress.value == "")
    {
        alert("Please include your mailing address.");
        document.frmContact.txtAddress.focus();
        return false;
    }   
    
        //Check for missing City
    if (document.frmContact.txtCity.value == "")
    {
        alert("Please include your city.");
        document.frmContact.txtCity.focus();
        return false;
    }   
    
        //Check for missing State
    if (document.frmContact.SelState.value == "")
    {
        alert("Please include your state.");
        document.frmContact.SelState.focus();
        return false;
    }
    
        //Check for missing Zip Code
    if (document.frmContact.txtZip.value == "")
    {
        alert("Please include your zip code.");
        document.frmContact.txtZip.focus();
        return false;
    }   
    
        //Check for missing Phone Number
    if (document.frmContact.txtPhone.value == "")
    {
        alert("Please include your phone number.");
        document.frmContact.txtPhone.focus();
        return false;
    }       
    
    //Check for missing Email address
    if (document.frmContact.txtEmail.value == "")
    {
        alert("Please include your email address.");
        document.frmContact.txtEmail.focus();
        return false;
    }   
    
        //Check for missing Subject
    if (document.frmContact.txtSubject.value == "")
    {
        alert("Please include your subject.");
        document.frmContact.txtSubject.focus();
        return false;
    }   
    
    //Check for missing message
    if (document.frmContact.txtaProjectType.value == "")
    {
        alert("Please include your Message.");
        document.frmContact.txtaProjectType.focus();
        return false;
    }   
    
    
    
        
    //If it made it here, everything looks ok
    return true;
}
 
     
 
// -->
</SCRIPT>
 
<SCRIPT LANGUAGE="JavaScript">
 
 
 
<!-- This script and many more are available free online at -->
 
<!-- The JavaScript Source!! http://javascript.internet.com -->
 
<!-- Original:  William Rozell Jr (elranzer@nospam.com ) -->
 
<!-- Web Site:  http://www.elranzer.com -->
 
<!-- Begin
 
var user;
 
var domain;
 
var suffix;
 
 
 
function jemail(user, domain, suffix){
 
 
 
document.write('<a href="' + 'mailto:' + user + '@' + domain + '.' + suffix + '">' + user + '@' + domain + '.' + suffix + '</a>');
 
 
 
}
 
 
 
//-->
 
//  End -->
 
</script>
 
 
</head>
 
<body bgcolor="#99ccff" leftmargin="0">
 <table width=100% border=0 cellspacing=0 cellpadding=0> 
<tr><td width=100% align=center><table width="765" border="0" cellpadding="0" cellspacing="0">
<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td width=100% align=center><table width="780" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
          <!--DWLayoutTable-->
          <tr> 
            <td width="780" height="25" valign="top"><img src="StelloLogo.jpg" alt="Stello Home Services Inc. - Serving Carolinas for over 25 years" width="780" height="341"></td>
          </tr>
        </table>
        <table width="780" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
          <!--DWLayoutTable-->
          <tr> 
            <td height="29" colspan="3" valign="top"><table width="100%" border="0"  cellpadding="0" cellspacing="0">
                <!--DWLayoutTable-->
                <tr> 
                  <td width="77" height="8"></td>
                  <td width="703"></td>
                </tr>
                <tr> 
                  <td height="21"></td>
                  <td valign="top"><strong><font face="Times New Roman, Times, serif"><a class=TOC  href="index.htm">Hom</a></font><a class=TOC  href="index.htm">e</a> 
                    </strong> &nbsp;|&nbsp; <strong><a class=TOC  href="aboutus.htm">About 
                    Us</a> &nbsp; | &nbsp; <a class=TOC  href="links.htm">Links</a> 
                    &nbsp; | &nbsp; <a class=TOC  href="ourwork.htm">Our Work</a> 
                    &nbsp;| &nbsp; <a class=TOC  href="contact.htm">Contact Information</a> 
                    &nbsp; | &nbsp;<a class=TOC  href="references.htm">What Our 
                    Customers Say</a> </strong></font></td>
                </tr>
              </table></td>
          </tr>
          <tr> 
            <td width="79" height="31">&nbsp;</td>
            <td width="677">&nbsp;</td>
            <td width="24">&nbsp;</td>
          </tr>
          <tr> 
            <td height="402">&nbsp;</td>
            <td valign="top"> <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                <!--DWLayoutTable-->
                <tr> 
                  <td width="677" height="305" valign="top"><div align="justify"> 
                      <p align="justify"><font color="#000000" size="7" face="Freestyle Script"> 
                        <strong>Contact Us...</strong></font><br>
                      </p>
                      <form name="frmContact" 
    action="Contact.php" method="post"
    onSubmit="return InputIsValid()">
                        <table width=570 border=0 cellpadding=4 cellspacing=1>
                          <!--DWLayoutTable-->
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1> Name </font></td>
                            <td width="401"><input name="txtName" size=25> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1 
      >Mailing Address</font></td>
                            <td><input name="txtAddress" size=25> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1 
      >City</font></td>
                            <td><input name="txtCity" size=25> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1>State</font></td>
                            <td><select name="SelState">
                                <option value="">Choose a state</option>
                                <option value="AL">Alabama</option>
                                <option value="AK">Alaska</option>
                                <option value="AZ">Arizona</option>
                                <option value="AR">Arkansas</option>
                                <option value="CA">California</option>
                                <option value="CO">Colorado</option>
                                <option value="CT">Connecticut</option>
                                <option value="DE">Delaware</option>
                                <option value="FL">Florida</option>
                                <option value="GA">Georgia</option>
                                <option value="HI">Hawaii</option>
                                <option value="ID">Idaho</option>
                                <option value="IL">Illinois</option>
                                <option value="IN">Indiana</option>
                                <option value="IA">Iowa</option>
                                <option value="KS">Kansas</option>
                                <option value="KY">Kentucky</option>
                                <option value="LA">Louisiana</option>
                                <option value="ME">Maine</option>
                                <option value="MD">Maryland</option>
                                <option value="MA">Massachusetts</option>
                                <option value="MI">Michigan</option>
                                <option value="MN">Minnesota</option>
                                <option value="MS">Mississippi</option>
                                <option value="MO">Missouri</option>
                                <option value="MT">Montana</option>
                                <option value="NE">Nebraska</option>
                                <option value="NV">Nevada</option>
                                <option value="NH">New Hampshire</option>
                                <option value="NJ">New Jersey</option>
                                <option value="NM">New Mexico</option>
                                <option value="NY">New York</option>
                                <option value="NC">North Carolina</option>
                                <option value="ND">North Dakota</option>
                                <option value="OH">Ohio</option>
                                <option value="OK">Oklahoma</option>
                                <option value="OR">Oregon</option>
                                <option value="PA">Pennsylvania</option>
                                <option value="RI">Rhode Island</option>
                                <option value="SC">South Carolina</option>
                                <option value="SD">South Dakota</option>
                                <option value="TN">Tennessee</option>
                                <option value="TX">Texas</option>
                                <option value="UT">Utah</option>
                                <option value="VT">Vermont</option>
                                <option value="VA">Virginia</option>
                                <option value="WA">Washington</option>
                                <option value="DC">Washington DC</option>
                                <option value="WV">West Virginia</option>
                                <option value="WI">Wisconsin</option>
                                <option value="WY">Wyoming</option>
                              </select> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1 
      >Zip</font></td>
                            <td><input name="txtZip" size=25> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1 
      >Phone</font></td>
                            <td><input name="txtPhone" size=25> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font face=arial size=-1 
      >Email Address </font></td>
                            <td><font color=blue> 
                              <input name="txtEmail" size=25 
     >
                              </font> </td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td height="27" align=left><font size="-1" face="arial">Type 
                              of Project </font></td>
                            <td valign="top"><input type="checkbox" name="checkbox1" value="NewConstruction">
                              <strong>New Construction &nbsp; 
                              <input type="checkbox" name="checkbox2" value="Renovation">
                              Renovation &nbsp; 
                              <input type="checkbox" name="checkbox3" value="Addition">
                              Addition<br>
                              <br>
                              <input type="checkbox" name="checkbox4" value="Bathroom">
                              Bathroom &nbsp; 
                              <input type="checkbox" name="checkbox5" value="Kitchen">
                              Kitchen &nbsp; 
                              <input type="checkbox" name="checkbox6" value="Other">
                              Other </strong></td>
                          </tr>
                          <tr bgcolor="#e8e8e8"> 
                            <td align=left><font size="-1" face="arial">Details 
                              on Project</font></td>
                            <td><textarea cols=40 name=txtaProjectType rows=10></textarea> 
                            </td>
                          </tr>
                          <tr> 
                            <td align=right>&nbsp;</td>
                            <td align=middle> <input
             
            type=submit
             name=subSubmit value="Submit"> &nbsp; <input id=reset1 name=reset1 style="HEIGHT: 24px; WIDTH: 74px" type=reset value="Clear form"> 
                            </td>
                          </tr>
                        </table>
                      </form>
                      <p align="justify"><br>
                        <font size="4"> </font></p>
                      </div>
                    </td>
                </tr>
                <tr> 
                  <td height="97">&nbsp;</td>
                </tr>
              </table></td>
            <td>&nbsp;</td>
          </tr>
        </table>
</body>
</html>
 

Re: Needs Help With PHP Web Page For Form

Posted: Fri Oct 10, 2008 5:41 pm
by aceconcepts
Here, take this emailer i created. It has worked perfectly for me everytime. Simply chenge the variables etc...

Code: Select all

 
//SET EMAIL VARIABLES
            $subject = "This is the email subject";
            $message='<html><body style="background-color:#F4F4F4;">';
            $message.='<h1>This is the email body</h1>';
            $message.="</body></html>";
            $from = $from_email; //set this to the "from email"
        
        //SET EMAIL HEADERS
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "Content-Transfer-Encoding: 7bit\r\n";
            $headers .= "From: " . $from . "\r\n";
                    
        //SEND EMAIL
            mail($toEmail,$subject,$message,$headers,"-f $from")
 
Hope this works for you. At the top simply get all the $_POST fields you need e.g. TO, FROM, SUBJECT etc...