[SOLVED] struggling with "Contact Form"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

[SOLVED] struggling with "Contact Form"

Post by tgalofre »

Hello! I'm new to this forum. Since I took an advice to make dynamic webpages by adding PHP to them, I've gone through some PHP tutorials. But I'm currently puzzled with this PHP project: contact form.

Here's the portion of contactus.html:

Code: Select all

<FORM METHOD="post" ACTION="sendmail.php"> 
<input type=hidden name="required" value="Email,Name,Phone,Message ">		  
            <table width="542" border="0" cellspacing="0" cellpadding="2">
              <tr valign="top"> 
                <td width="57" height="26" valign="middle"> 
                  <div align="left">Name:</div></td>
                <td width="194"> <div align="left"> 
                    <label for="Name"></label>
                    <input name="Name" type="text" id="name2" size="25" />
                  </div></td>
                <td width="77" rowspan="5"><div align="center">Message<br />
                  or Question: 
                  </div>
                  <label for="label"></label></td>
                <td width="300" rowspan="5"><textarea input name="Message" id="message2" cols="60" rows="5">
                </textarea></td>
              </tr>
              <tr valign="top"> 
                <td height="26" valign="middle"> 
                  <div align="left"> 
                    <label for="Phone">Phone:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Phone" type="text" id="phone2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td valign="middle"> 
                  <div align="left"> 
                    <label for="Fax">Fax:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Fax" type="text" id="fax2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td valign="middle"> 
                  <div align="left"> 
                    <label for="Email">Email:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Email" type="text" id="email2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td colspan="2" rowspan="2"> <div align="left"></div>
                  <div align="left"><br />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td>&nbsp;</td>
                <td halign="left"><input type="submit" name="Submit" value="Submit" id="Submit2" /> 
                  <input type="reset" name="Reset" value="Reset" id="Reset2" />                </td>
              </tr>
      </table>
</form></div>
and here's the sendmail.php code:

Code: Select all

<?php
  $Name = $_REQUEST['Name'] ;
  $Email = $_REQUEST['Email'] ;
  $Message = $_REQUEST['Message'] ;
  $Phone = $_REQUEST['Phone'] ;
  $Fax = $_REQUEST['Fax'] ;

  if (!isset($_REQUEST['Email'])) {
    header( "Location: http://www.cacvrs.org/contactus.html" );
  }
    elseif (empty($Email) || empty($Message)) {

    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );

    <html>
    <head><title>Error Message</title></head>
    <body>
    <h1>Error!</h1>
    <p>
    Oops, it appears you forgot to enter your
    email address and your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>

  else {
    mail( "tgalofre@cacdhh.org", "Email Form Submission",
      $Message, "From: $Email" );
    header( "Location: http://www.cacvrs.org/thankyou.html" );
  }
?>
when I filled in the contact form and hit "submit" button, the screen went blank with "done" text in the status portion of the web browser. What have I done wrong? :?: Please enlighten me. :?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

the below is purely speculation...

Code: Select all

<html> 
    <head><title>Error Message</title></head> 
    <body> 
    <h1>Error!</h1> 
    <p> 
    Oops, it appears you forgot to enter your 
    email address and your message. Please press the BACK 
    button in your browser and try again. 
    </p> 
    </body>
that seems to be within your php blocks..which means it should be causing some sort of error if you hit that situation because you didn't echo or break out of PHP... Also..you didn't end that elseif block (which I write else if just for the record:) ) not ending that else if is the biggest error I see. see if that fixes anything by closing that else if block with a "}" on the line below </html> :-D
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

still not working :-(

Post by tgalofre »

here's a portion of contactus.html:

Code: Select all

<div align="center" id="contactForm"> 
		  
<FORM METHOD="post" ACTION="http://www.cacvrs.org/sendmail.php"> 
<input type=hidden name="required" value="Email,Name,Phone,Message ">		  
            <table width="542" border="0" cellspacing="0" cellpadding="2">
              <tr valign="top"> 
                <td width="57" height="26" valign="middle"> 
                  <div align="left">Name:</div></td>
                <td width="194"> <div align="left"> 
                    <label for="Name"></label>
                    <input name="Name" type="text" id="name2" size="25" />
                  </div></td>
                <td width="77" rowspan="5"><div align="center">Message<br />
                  or Question: 
                  </div>
                  <label for="label"></label></td>
                <td width="300" rowspan="5"><textarea input name="Message" id="message2" cols="60" rows="5">
                </textarea></td>
              </tr>
              <tr valign="top"> 
                <td height="26" valign="middle"> 
                  <div align="left"> 
                    <label for="Phone">Phone:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Phone" type="text" id="phone2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td valign="middle"> 
                  <div align="left"> 
                    <label for="Fax">Fax:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Fax" type="text" id="fax2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td valign="middle"> 
                  <div align="left"> 
                    <label for="Email">Email:</label>
                  </div></td>
                <td> <div align="left"> 
                    <input name="Email" type="text" id="email2" size="25" />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td colspan="2" rowspan="2"> <div align="left"></div>
                  <div align="left"><br />
                  </div></td>
              </tr>
              <tr valign="top"> 
                <td>&nbsp;</td>
                <td halign="left"><input type="submit" name="Submit" value="Submit" id="Submit2" /> 
                  <input type="reset" name="Reset" value="Reset" id="Reset2" />                </td>
              </tr>
      </table>
</form></div>
I closed the "loose end" as you suggested:

Code: Select all

<?php
  $Name = $_REQUEST['Name'] ;
  $Email = $_REQUEST['Email'] ;
  $Message = $_REQUEST['Message'] ;
  $Phone = $_REQUEST['Phone'] ;
  $Fax = $_REQUEST['Fax'] ;

  if (!isset($_REQUEST['Email'])) {
    header( "Location: http://www.cacvrs.org/contactus.html" );
  }
    elseif (empty($Email) || empty($Message)) {

    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );

    <html>
    <head><title>Error Message</title></head>
    <body>
    <h1>Error!</h1>
    <p>
    Oops, it appears you forgot to enter your
    email address and your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>
	}
  else {
    mail( "tgalofre@cacdhh.org", "Email Form Submission",
      $Message, "From: $Email" );
    header( "Location: http://www.cacvrs.org/thankyou.html" );
  }
?>
I somehow wish that PHP would show me what's wrong (just like Macromedia Flash have with its debugging complier).
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

I can see already that you have html within <?php ?> tags, and that's a parse error.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

echo 'hello gentlemen';
<table> 
  <tr>
     <td> I\'ll cause a parse error!! </td>
  </tr>
</table>
echo 'im here';
You have a couple options to fix your problem

1. using echo() or print()

Code: Select all

echo 'hello gentlemen';
echo '<table> 
  <tr>
     <td> I\'ll NOT cause a parse error!! </td>
  </tr>
</table>';
echo 'im here';
2. Escaping php mode

Code: Select all

<?
echo 'hello gentlemen';
?>
<table> 
  <tr>
     <td> I\'ll  NOT cause a parse error!! </td>
  </tr>
</table>
<?
echo 'im here';
3. heredoc

Code: Select all

echo 'hello gentlemen';
echo <<<HTML 
<table> 
  <tr>
     <td> I\'ll NOT cause a parse error!! </td>
  </tr>
</table> 
HTML;
echo 'im here';
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

still see a blank screen again....

Post by tgalofre »

with some changes from the advices I got recently..here it is:

Code: Select all

<?php
  mail( "tgalofre@cacdhh.org", "Contact Form Submission", print_r($_POST,true) );
  $Name = $_POST['Name'] ;
  $Email = $_POST['Email'] ;
  $Message = $_POST['Message'] ;
  $Phone = $_POST['Phone'] ;
  $Fax = $_POST['Fax'] ;

  if (!isset($_POST['Email'])) {
    header( "Location: http://www.cacvrs.org/contactus.html" );
  }
    elseif (empty($Email) || empty($Message)) {

    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );

   echo ' <html>
    <head><title>Error Message</title></head>
    <body>
    <h1>Error!</h1>
    <p>
    Oops, it appears you forgot to enter your
    email address and your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>';
	}
  else {
    mail( "tgalofre@cacdhh.org", "Contact Form Submission",
	"From: $Email", "$Name", "$Message", "$Phone", "$Fax" );
    header( "Location: http://www.cacvrs.org/thankyou.html" );
  }
?>
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Here:

Code: Select all

<?php
  //mail( "tgalofre@cacdhh.org", "Contact Form Submission", print_r($_POST,true) );
  extract($_POST)

  if ( !$Email  || !$Message ) 
  {
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );

   echo ' <html>
    <head><title>Error Message</title></head>
    <body>
    <h1>Error!</h1>
    <p>
    Oops, it appears you forgot to enter your
    email address or your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>';
  }
  else {
    mail( "tgalofre@cacdhh.org", "Contact Form Submission",
    "From: $Email\n\nName: $Name\n\nMessage: $Message\n\nPhone: $Phone\n\nFax: $Fax" );

    header( "Location: http://www.cacvrs.org/thankyou.html" );
  }
?>
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

so.. here's modified PHP code

Post by tgalofre »

Code: Select all

<?php
  //mail( "tgalofre@cacdhh.org", "Contact Form Submission", print_r($_POST,true) ); 
  extract($_POST) 

  $Name = $_POST['Name'] ;
  $Email = $_POST['Email'] ;
  $Message = $_POST['Message'] ;
  $Phone = $_POST['Phone'] ;
  $Fax = $_POST['Fax'] ;

  if ( !$Email  || !$Message ) 
  { 
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); 
    header( "Cache-Control: no-cache, must-revalidate" ); 
    header( "Pragma: no-cache" ); 

   echo ' <html> 
    <head><title>Error Message</title></head> 
    <body> 
    <h1>Error!</h1> 
    <p> 
    Oops, it appears you forgot to enter your 
    email address or your message. Please press the BACK 
    button in your browser and try again. 
    </p> 
    </body> 
    </html>'; 
  } 
  else { 
    mail( "tgalofre@cacdhh.org", "Contact Form Submission", 
    "From: $Email\n\nName: $Name\n\nMessage: $Message\n\nPhone: $Phone\n\nFax: $Fax" ); 

    header( "Location: http://www.cacvrs.org/thankyou.html" ); 
  } 
?>
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

1 out of 2 works...

Post by tgalofre »

one of two things is working....

I get email(s) from contact form.

one other thing remains...being transported to another page...this one have yet to happen. still seeing blank screen lately.

here's latest PHP code:

Code: Select all

<?php
  //mail( "tgalofre@cacdhh.org", "Contact Form Submission", print_r($_POST,true) ); 
  extract($_POST); 
  $Name = $_POST['Name'] ;
  $Email = $_POST['Email'] ;
  $Message = $_POST['Message'] ;
  $Phone = $_POST['Phone'] ;
  $Fax = $_POST['Fax'] ;
  if ( !$Email  || !$Message ) 
  { 
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); 
    header( "Cache-Control: no-cache, must-revalidate" ); 
    header( "Pragma: no-cache" ); 
   	echo '<html> 
    <head><title>Error Message</title></head> 
    <body> 
    <h1>Error!</h1> 
    <p> 
    Oops, it appears you forgot to enter your 
    email address or your message. Please press the BACK 
    button in your browser and try again. 
    </p> 
    </body> 
    </html>'; 
  } 
  else { 
    header( "Location: http://www.cacvrs.org/thankyou.html" );
	mail( "tgalofre@cacdhh.org", "Contact Form Submission", 
    "From: $Email\n\nName: $Name\n\nMessage: $Message\n\nPhone: $Phone\n\nFax: $Fax" );  
  } 
?>
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

The solution:

Code: Select all

<?php

  extract($_POST);

  if ( !$Email  || !$Message )
  {
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );
       echo '<html>
    <head><title>Error Message</title></head>
    <body>
    <h1>Error!</h1>
    <p>
    Oops, it appears you forgot to enter your
    email address or your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>';
  }
  else {
    mail( "tgalofre@cacdhh.org", "Contact Form Submission",
    "From: $Email\n\nName: $Name\n\nMessage: $Message\n\nPhone: $Phone\n\nFax: $Fax" );  
    header( "Location: http://www.cacvrs.org/thankyou.html" );
  }
?>
tgalofre
Forum Newbie
Posts: 6
Joined: Tue Oct 04, 2005 4:15 pm

finally!! It works!!!!!!!

Post by tgalofre »

here's a solution:

Code: Select all

<?php 
  extract($_POST); //mail( "tgalofre@cacdhh.org", "Contact Form Submission", print_r($_POST,true) ); 
  $Name = $_POST['Name'] ; 
  $Email = $_POST['Email'] ; 
  $Message = $_POST['Message'] ; 
  $Phone = $_POST['Phone'] ; 
  $Fax = $_POST['Fax'] ; 
  if ( !$Email  || !$Message ) 
  { 
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); 
    header( "Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0" ); 
    header( "Pragma: no-cache" ); 
    echo '<html> 
    <head><title>Error Message</title></head> 
    <body> 
    <h1>Error!</h1> 
    <p> 
    Oops, it appears that you forgot to enter your 
    email address and your message. Please press the BACK 
    button in your browser and try again. 
    </p> 
    </body> 
    </html>'; 
  } 
  else 
  { 
	mail( "tgalofre@cacdhh.org", "Contact Form Submission", 
    "From: $Email\n\nName: $Name\n\nMessage: $Message\n\nPhone: $Phone\n\nFax: $Fax" );       
   echo '<script type="text/javascript" language="javascript"> window.location.href ="http://www.cacvrs.org/thankyou.html"; </script>';
  }
?>
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Dude, extract($_POST) REPLACES

$Name = $_POST['Name'] ;
$Email = $_POST['Email'] ;
$Message = $_POST['Message'] ;
$Phone = $_POST['Phone'] ;
$Fax = $_POST['Fax'] ;


it REPLACES that code!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be extremely careful with extract() as it can make some nice security holes. It's best to at the least, filter the submission data down to the variables you are expecting first (in the least)
Post Reply