Jump to content

[SOLVED] PHP form problem. Need some urgent help if poss please


TrevMac

Recommended Posts

Hi all,

 

Need some help here please.

 

I have a PHP form that inserts the submited data into a MySQL db and also sends the same data to the client as an email.

 

The form carries out both functions fine but the problem is that it sends an email with no info as soon as the page loads.

 

I have included the section of the code that deals with the sending of the email. I have not created this code myself but have modified it.

 

I suspect that the problem lies around the

 

// SEND MAIL

       

    @mail($to, $subject, $message, $headers);

 

section of the code but of course being a newby I am very possibly wrong!

 

If you need me to post the full form code I will.

 

If anyone could please point me in the right direction I would really appreciate it.

 

Thanx in advance.

 

----------------------------------------------------------------------

 

// MAIL SUBJECT

 

    $subject = "Land Required Form";

   

// TO MAIL ADDRESS

     

    $to="[email protected]";

 

// MAIL HEADERS

                       

    $headers  = "MIME-Version: 1.0\n";

    $headers .= "Content-type: text/html; charset=iso-8859-1\n";

    $headers .= "From: ".$strfirstName." ".$strlastName."<".$stremail.">\n";

 

// This two steps to help avoid spam   

 

    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";

    $headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

 

// With message

       

    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

    $headers .= "Content-Transfer-Encoding: 8bit\r\n";

    $headers .= "".$message."\n";

    $headers .= "--".$num."\n"; 

     

// SEND MAIL

       

    @mail($to, $subject, $message, $headers);

}

 

 

------------------------------------------------------------------------------------

Thanks for the reply Orio.

 

The problem that I am experiencing is that when the web page loads it automatically sends a blank email.

 

The visitor can then fill in the form and submit. The code is working as it should from this point.

 

What I need to do is stop the form sending a blank email when the page is visited.

 

Hope I am making myself clear.

 

Here is the code that I have used for the email section.

<?php
    $strfirstName=ucfirst($_REQUEST["firstName"]);
$strlastName=ucfirst($_REQUEST["lastName"]); 
    $straddress1=ucfirst($_REQUEST["address1"]); 
    $straddress2=ucfirst($_REQUEST["address2"]);
    $strpostcode=ucfirst($_REQUEST["postcode"]);
//$strtelWork=ucfirst($_REQUEST["telWork"]); 
    //$strtelHome=ucfirst($_REQUEST["telHome"]); 
    //$strtelMobile=ucfirst($_REQUEST["telMobile"]);
$strlocation=ucfirst($_REQUEST["location"]);
$strapproxSize=ucfirst($_REQUEST["approxSize"]);
$strbrowngreen=ucfirst($_REQUEST["browngreen"]);
    $strcurrentUse=ucfirst($_REQUEST["currentUse"]); 
    $strlocalAuth=ucfirst($_REQUEST["localAuth"]);
$strplanPerm=ucfirst($_REQUEST["planPerm"]);	      
$strapproached=ucfirst($_REQUEST["approached"]);        
   
    $phone=$_REQUEST["telWork"]; 
    if($phone != ""){ $strno=$phone; } else { $strno="-"; } 

$phone2=$_REQUEST["telHome"]; 
    if($phone2 != ""){ $strno2=$phone2; } else { $strno2="-"; }

$phone3=$_REQUEST["telMobile"]; 
    if($phone3 != ""){ $strno3=$phone3; } else { $strno3="-"; }   
        
    $stremail=$_REQUEST["email"];    
    $strcomments=ucfirst($_REQUEST["comments"]); 

    { 
    
        $message= ' 
    
    
            <table cellspacing="0" cellpadding="4" border="0" width="400"> 
            <tr> 
                <td colspan="2"></td> 
            </tr> 
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>First Name:</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strfirstName.' '.$strlastName.'</td> 
            </tr>
		  
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> 

           	<tr bgcolor="#eeeeee"> 
            <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Address:</strong></td> 
            <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$straddress1.'</td> 
            </tr> 
		<tr bgcolor="#eeeeee"> 
            <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"></td> 
            <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$straddress2.'</td> 
            </tr>
		 <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Postcode:</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strpostcode.'</td> 
              </tr>
		  
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
					  
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Telephone (Work):</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strno.'</td> 
              </tr>
		   <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Telephone (Home):</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strno2.'</td> 
              </tr>
		   <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Telephone (Mobile):</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strno3.'</td> 
              </tr>
		  
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>			  
		  			   
            <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Email:</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$stremail.'</td> 
              </tr> 
		  
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>

		  <tr bgcolor="#eeeeee"> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Location:</strong></td> 
              <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strlocation.'</td> 
              </tr>
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Size of Site (Approx)</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strapproxSize.'</td> 
            </tr>
            <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Brownfield or Greenfield?</strong></td>
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strbrowngreen.'</td> 
            </tr>
		<tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Current use of site:</strong></td>
		 </tr>
		 <tr>
                <td colspan="2" style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strcurrentUse.'</td> 
            </tr>
		  <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Local Authority Area:</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strlocalAuth.'</td> 
            </tr>
		  <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Planning Permission?:</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strplanPerm.'</td> 
            </tr>
		  <tr bgcolor="#eeeeee"> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Approached before?:</strong></td> 
                <td style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strapproached.'</td> 
            </tr> 			 	 			    
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:12px; color:#333333;"><strong>Comments</strong></td> 
            </tr>                
            <tr bgcolor="#eeeeee"> 
                <td colspan="2" style="font-family:Verdana, Arial; font-size:12px; color:#333333;">'.$strcomments.'</td> 
            </tr>                 
         </table> 
            

'; 


// MAIL SUBJECT 

    $subject = "Land Required Form"; 
    
// TO MAIL ADDRESS 
       
    $to="[email protected]"; 

// MAIL HEADERS 
                        
    $headers  = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "From: ".$strfirstName." ".$strlastName."<".$stremail.">\n";

// This two steps to help avoid spam    

    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
    $headers .= "X-Mailer: PHP v".phpversion()."\r\n";          

// With message 
        
    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
    $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
    $headers .= "".$message."\n"; 
    $headers .= "--".$num."\n";  
       
// SEND MAIL 
        
    @mail($to, $subject, $message, $headers);
}
?>

 

(edited to insert


tags)

Assuming your form is using method="post" you can simply replace the call to mail with this:

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     mail($to, $subject, $message, $headers);
}

 

That way it will only send the mail when the page is being displayed after submitting the form. 

 

I find that putting this super simple function in a library somewhere for the site saves a bit of typing:

 

function is_post() {
    return $_SERVER['REQUEST_METHOD'] == 'POST';
}

 

Then whenever you want a block of code to ONLY be executed after a form post you just include the file with this function in it and

 

if  (is_post()) {
    do_some_cool_stuff();
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.