Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
This is my contact form that I made. I am new to making contact forms and I am curious to know if I have done this correctly and if there are any errors within it that i should consider revising.

PHP
        <?php 
                  	$first_name = $_POST["first_name"];
                  	$last_name = $_POST["last_name"];
					$email = $_POST["email"];
					$telephone = $_POST["telephone"];
					$reason = $_POST["reason"];
					$time = $_POST["time"];
					$comment = $_POST["comment"];
					$to      = 'email@gmail.com';
					$subject = 'Parent Contact Form Submission';
					

 					$v1 = "
							<html> 
  							<body> 
							<style>
								h1 {color:#000066;}
						 		table {border:1px solid black; background: #e3f0ff;}
						 	</style>
						 	<h1>Hello, this form has been submitted!</h1>
						 	<img src= 'logo.png' />
						 	<table rules='all' style='border-color: #ffb300;' cellpadding='10' width='500px'>
							 	<tr style='background: #ffb300;'><td>First Name: </td>$first_name
							 	<tr style='background: #ffb300;'><td>Last Name: </td>$last_name
							 	<tr style='background: #fafafa;'><td>Email: </td>$email
							 	<tr style='background: #fafafa;'><td>Phone: </td>$telephone
							 	<tr style='background: #fafafa;'><td>Reason for Contact: </td>$reason
							 	<tr style='background: #fafafa;'><td>Best Time to Contact: </td>$time	 
							 	<tr style='background: #fafafa;'><td>Comments: </td>$comment
 							</table>   
  							</body> 
							</html> ";
					$message = $v1; 
    				$headers  = "From: $from\r\n"; 
    				$headers .= "Content-type: text/html\r\n"; 
    				mail($to, $subject, $message, $headers); 
    				echo "Message has been sent..."; //Page RE DIRECT 
//******************************************************************************************************************************//
                   
				    $first_name = $_POST["first_name"];
                  	$last_name = $_POST["last_name"];
					$email = $_POST["email"];
					$telephone = $_POST["telephone"];
					$reason = $_POST["reason"];
					$time = $_POST["time"];
					$comment = $_POST["comment"];
					$subject = 'Parent Contact Form Submission';
 					$v1 = "
							<html> 
  							<body> 
							<style>
								#disclosure {font-size: 8px; color: #333;}
								h1 {color:#000066;}
						 		table {border:1px solid black;}
						 	</style>
						 	<img src= 'logo.png' />
						 	<table rules='all' style='border-color: #ffb300;' cellpadding='10' width='500px'>
							 	<tr style='background: #ffb300;'><td>Email Confirmation
							 	<tr style='background: #fafafa;'><td>Hello  $first_name, your message has been recieved! We will contact you shortly! <br><br>Best, <br>Admin <br><br>Follow Us On:<br><a href='http://www.facebook.com'><img src='lfacebook_hover.png' width='18' height='18'></a><a href='http://twitter.com'><img src='twitter_hover.png' width='18' height='18'></a><a href='http://google.com'><img src='gplus_hover.png' width='18' height='18'></a><br><div id='disclosure' align='right'>©THE AWESOME TEAM™ All Rights Reserved 2012 </div>
 							</table>   
  							</body> 
							</html> ";
					$message = $v1; 
    				$headers  = "From: $from\r\n"; 
    				$headers .= "Content-type: text/html\r\n"; 
    				mail($email, $subject, $message, $headers);    
						
						$count= count(file("main_form.csv"));						
						$today = date("d M Y h:i A");
						echo $today;



$cvsData = "\n" . $count . "," . $today . "," . $first_name . "," . $last_name . "," . $email . "," . $telephone . "," . $reason . "," . $time . "," . $comment;

$fp = fopen("main_form.csv", "a" );
if($fp){
    fwrite($fp, $cvsData);
    fclose($fp);
    }					

?>
Posted

1 solution

From the first glance: HTML it wrong. The <style> elements goes inside <head> element, not <body>. Aha, #fafafa… remove all stylistic attributes except class and use CSS everywhere.

More importantly $cvsData serves no purpose. You could have it all having the element <form>. This is the standard HTML way of posting; all the HTML post keys being the values of the name attributes.

And now, the most terrifying thing: you made your message-posting page an easy target of a trivial exploit which can turn your HTTP server host computer into a zombie sending spam or something like that in no time. They really do such things, with minimal effort. This is explained in my past answer: unable to send mail , it showing the error in below code .[^].

—SA
 
Share this answer
 
Comments
Wombooo 28-Dec-14 0:06am    
@SA, i am storing all the data in a csv file. That is why i have $cvsData.

I put the styling there because it worked before (2 years ago) and for some odd reason, idk why it doesn't. Based on my current situation, can you help me rewrite my code? I am beating my head against the wall on this but i do see the issue being everything that $v1 is equal to.
Sergey Alexandrovich Kryukov 28-Dec-14 0:10am    
In this forum, we help only by answering questions. What are you follow-up questions?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900