PHP Contact form file

Discussion in 'PHP' started by Kaleem Ullah Hashmi, Jul 17, 2012.

  1. #1
    I have devloped a contact form with PHP code. It work perfectly. when you submit the form all the information e-mail to you perfectly. After submitting the contact form it will take you back to you same contact page. Know want to make some changes in it. When some one submit the contact form a message appear "Thanks for contacting us we will reply you withing in 5 business days" and then return to you on contact page. I try to modify the code myself but can't able to do that.
    PHP CODE:



    <?php
        
        if(!$_POST) exit;
        
        $email = $_POST['email'];
        
        
        //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
        if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
        	$error.="Invalid email address entered";
        	$errors=1;
        }
        if($errors==1) echo $error;
        else{
        	$values = array ('URL','email','Keyword','Title');
        	$required = array('URL','email','Keyword','Title');
        	 
        	$your_email = "info@bannistermarketinggroup.co.uk";	
        	$email_subject = "Bannister Marketing Group: ".$_POST['subject'];
        	$email_content = "Following is the keyword report:\n";
        	
        	foreach($values as $key => $value){
        	  if(in_array($value,$required)){
        		if ($key != 'subject' && $key != 'company') {
        		  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        		}
        		$email_content .= $value.': '.$_POST[$value]."\n";
        	  }
        	}
        	 
        	if(@mail($your_email,$email_subject,$email_content)) {
        		//echo 'thanks you we receive report witing 24hr';
        		header("Location:http://bannistermarketinggroup.co.uk/site_optimization/");
                exit();
        		
        	} //else {
        		//echo 'ERROR!';
        	//}
        }
        ?>
    
    PHP:
    **HTML CODE**



    <form action="contact.php" method="post">
                        	<Div class="fieldbox">
                            	<div class="textname">URL:</div>
                                <div class="field"><input type="text" id="URL" name="URL" /></div>
                                <div class="icon22"><img src="images/URLicon.jpg" /></div>
                            </Div>
                            
                            <Div class="fieldbox">
                            	<div class="textname">Title:</div>
                                <div class="field"><input id="Title" type="text" name="Title" /></div>
                                <div class="icon22"><img src="images/titleicon.jpg" /></div>
                            </Div>
                            
                              <Div class="fieldbox">
                            	<div class="textname">Keyword:</div>
                                <div class="field"><input id="Keyword" type="text" name="Keyword" />  </div>
                                <div class="icon22"><img src="images/keywordicon.jpg" /></div>
                            </Div>
                            
                            <Div class="fieldbox">
                            	<div class="textname">E-mail:</div>
                                <div class="field"><input id="name" type="text" name="email" />  </div>
                                <div class="icon22"><img src="images/emailicon.jpg" /></div>
                            </Div>
                            
                            <div class="button">
                            <input type="image" type="text" src="images/submit.jpg" />	
                            </div>
                            </form>
    HTML:

    Any once can help me please.Need to done it as soon as possible.
     
    Kaleem Ullah Hashmi, Jul 17, 2012 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    Just keep the php within the actual contact form. I just rewrote your script for you and added some security.

    
    <?php
    
    	function clean($string) {
    		
    		$string = strip_tags(htmlspecialchars($string, ENT_QUOTES, 'UTF-8'));
    		
    		return $string;	
    		
    	}
    
    	if(isset($_POST['send'])) {
        
    		$email = clean($_POST['email']);
    		
    		$error = '';
    		
    		if(empty($email)) {
    			
    			$error .= '<li>Please enter your email address.</li>'."\r\n";	
    			
    		}else{
    			
    			//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
    			
    			if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )) {
    				
    				$error .= '<li>Invalid email address entered</li>'."\r\n";
    				
    			}
    			
    		}
    			
    		if($error == '') {
    			
    			$values = array ('URL','email','Keyword','Title');
    			$required = array('URL','email','Keyword','Title');
    			 
    			$your_email = "info@bannistermarketinggroup.co.uk"; 
    			$email_subject = "Bannister Marketing Group: ".clean($_POST['subject']);
    			$email_content = "Following is the keyword report:\n";
    			
    			foreach($values as $key => $value){
    				
    				if(in_array($value,$required)){
    				  
    					if ($key != 'subject' && $key != 'company') {
    					
    						if(empty($_POST[$value])) {
    					  		
    							if(empty($error)) {
    								
    								$error .= '<li>Please make sure all required fields are complete.</li>';
    								
    							}
    					  
    						}else{
    					
    							$email_content .= clean($value).': '.clean($_POST[$value])."\n";	
    						
    						}
    					
    					}
    				
    				}
    			}
    			
    		}
    		
    		if($error == '') {
    				 
    			if(@mail($your_email,$email_subject,$email_content)) {
    				
    				echo 'thanks you we receive report witing 24hr';
    				#header("Location:http://bannistermarketinggroup.co.uk/site_optimization/");
    				exit(0);
    					
    			}
    				
    		}else{
    				
    			echo '<h2>Error</h2>'."\r\n";
    			echo '<ul>'."\r\n";
    			echo $error."\r\n";
    			echo '</ul>'."\r\n";	
    				
    		}
    		
    	}
    
    ?>
    <form action="" name="send" method="post">
    	<div class="fieldbox">
    		<div class="textname">URL:</div>
    		<div class="field"><input type="text" id="URL" name="URL" /></div>
    		<div class="icon22"><img src="images/URLicon.jpg" /></div>
        </div>
                            
    	<div class="fieldbox">
    		<div class="textname">Title:</div>
    		<div class="field"><input id="Title" type="text" name="Title" /></div>
    		<div class="icon22"><img src="images/titleicon.jpg" /></div>
        </div>
                            
    	<div class="fieldbox">
    		<div class="textname">Keyword:</div>
    		<div class="field"><input id="Keyword" type="text" name="Keyword" />  </div>
    		<div class="icon22"><img src="images/keywordicon.jpg" /></div>
    	</div>
                            
        <div class="fieldbox">
            <div class="textname">E-mail:</div>
            <div class="field"><input id="name" type="text" name="email" />  </div>
            <div class="icon22"><img src="images/emailicon.jpg" /></div>
        </div>
                            
        <div class="button">
    	<input type="submit" name="send" value="Send Email" />  
    	</div>
    </form>
    
    PHP:
     
    HuggyEssex, Jul 18, 2012 IP
  3. Kaleem Ullah Hashmi

    Kaleem Ullah Hashmi Active Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Thanks for your help. I have found perfect solution for this. Following is the code:
    
     if(@mail($your_email,$email_subject,$email_content)) {
    
    echo "Thanks for your enquiry we will contact you shortly have a nice day" ;
    
    echo "<script type=\"text/javascript\">";
    
    echo "setTimeout(function(){window.location=\"http://bannistermarketinggroup.co.uk/contactus.html/\"},3000);";
    echo "</script>";
    }
    }
    ?>
    
    Code (markup):
     
    Kaleem Ullah Hashmi, Jul 23, 2012 IP