Page 1 of 1

[SOLVED] struggling with "Contact Form"

Posted: Tue Oct 04, 2005 4:39 pm
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. :?

Posted: Tue Oct 04, 2005 4:43 pm
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

still not working :-(

Posted: Wed Oct 05, 2005 9:27 am
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).

Posted: Wed Oct 05, 2005 9:33 am
by chrys
I can see already that you have html within <?php ?> tags, and that's a parse error.

Posted: Wed Oct 05, 2005 9:52 am
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';

still see a blank screen again....

Posted: Wed Oct 05, 2005 11:31 am
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" );
  }
?>

Posted: Wed Oct 05, 2005 11:34 am
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" );
  }
?>

so.. here's modified PHP code

Posted: Wed Oct 05, 2005 12:26 pm
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" ); 
  } 
?>

1 out of 2 works...

Posted: Wed Oct 05, 2005 2:07 pm
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" );  
  } 
?>

Posted: Wed Oct 05, 2005 2:17 pm
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" );
  }
?>

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

Posted: Wed Oct 05, 2005 4:20 pm
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>';
  }
?>

Posted: Thu Oct 06, 2005 8:40 am
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!!

Posted: Thu Oct 06, 2005 9:18 am
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)