please, help 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
afallingpanda
Forum Newbie
Posts: 3
Joined: Wed Nov 06, 2013 7:10 am

please, help with contact form

Post by afallingpanda »

hello guys, today i have a problem i cant seem to solve ;/

so basically i got a page with a contact form, once the form has been submited, it sends (action="sent.php") to page "sent.php", this page has the php script i made to actually send the email. now my problem is, once someones clicks send, and gets redirected to sent.php, the email gets sent ( as intended) but then when the user is on that page all they have to do is just refresh the page and it keeps sending the email. now of course as you can imagine this can be a problem. how do i work my way around this guys?
here is my code for contactus.php:

Code: Select all

print"<table cellspacing=0 cellpadding=0 border=0 class=\"contacttable\">";
print"<form method=\"post\" action=\"sent.php\">";

print"	<tr>";
print"		<td class=\"contactlabel\"><label for=\"fname\">* First Name</label></td>";
print"		<td class=\"contactlabel\"><label for=\"lname\">* Last Name<label></td>";
print"	</tr>";

print"	<tr>";
print"		<td><input class=\"contactbox\" type=\"text\"  id=\"fname\" name=\"fname\" maxlength=\"30\" required></td>";
print"		<td><input class=\"contactbox\" type=\"text\" id=\"lname\" name=\"lname\" maxlength=\"30\" required></td>";
print"	</tr>";

print"	<tr>";
print"		<td class=\"contactlabel\"><label for=\"email\">* Email</label></td>";
print"		<td class=\"contactlabel\"><label for=\"groupname\">Company/Group Name<label></td>";
print"	</tr>";

print"	<tr>";
print"		<td><input class=\"contactbox\" type=\"text\"  id=\"email\" name=\"email\" maxlength=\"50\" required></td>";
print"		<td><input class=\"contactbox\" type=\"text\" id=\"groupname\" name=\"groupname\" maxlength=\"50\"></td>";
print"	</tr>";

print"	<tr>";
print"		<td class=\"contactlabel\"><label for=\"subject\">* Subject</label></td>";
print"		<td class=\"contactlabel\"><label for=\"reference\">Reference<label></td>";
print"	</tr>";

print"	<tr>";
print"		<td><input class=\"contactbox\" type=\"text\"  id=\"subject\" name=\"subject\" maxlength=\"100\" required></td>";
print"		<td><input class=\"contactbox\" type=\"text\" id=\"reference\" name=\"reference\" maxlength=\"50\"></td>";
print"	</tr>";

print"	<tr>";
print"		<td colspan=\"2\" class=\"contactlabel\"><label for=\"message\">* Message:</label></td>";
print"	</tr>";

print"	<tr>";
print"		<td colspan=\"2\"><textarea class=\"textarecontact\" name=\"message\" id=\"message\" maxlength=\"2000\" required></textarea></td>";
print"	</tr>";

print"	<tr>";
print"		<td colspan=\"2\"><input class=\"submitbutton\" type=\"submit\" value=\"Send An Email\"></td>";
print"	</tr>";

print"</table>";
print"</form>";
and here is my code for sent.php:

Code: Select all

<?php
include("../inc/header.php");
// php for contact form


	// here we put in an if statement to check against missing variables (empty values)
		if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])){
			
			$fname=$_POST['fname'];
			$lname=$_POST['lname'];
			$groupname=$_POST['groupname'];
			$reference=$_POST['reference'];
			$email=$_POST['email'];
			$subject=$_POST['subject'];
			$message=$_POST['message'];

			// here we are checking to see if that value anything and not just black.

			if (!empty($fname) && !empty($lname) && !empty($email) && !empty($subject) && !empty($message) ){
				
				// this is doing a check for max length, its doing it in php just in case the user
				// cheats and bypasses the html check.
			if (strlen($fname>30) || strlen($lname>30) || strlen($groupname>50) || strlen($email>50) || strlen($subject>100) || strlen($reference>100) || strlen($message>2000)){
				echo"sorry, that max length for a field has been exceeded.";
			}

			$to='support@eclipse-developers.com';
			$emailsubject=$subject;
			$body=$fname." ".$lname."\nCompany Name: ".$groupname."\nRef: ".$reference."\nMessage: ".$message;
			$headers= 'From: '.$email;

			// mails, if statement so if its true (mail did send)
				if	(mail($to,$emailsubject,$body,$headers)){

					echo'Thanks for contacting us.';					

				}else{

					echo'Sorry, an error occurred. Try again later.';

				}
			}
			
			} else{
			
			
			
			}
			


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please, help with contact form

Post by Celauran »

After sending the email

Code: Select all

$_POST = array();
Or send the mail, redirect the user to another page, and display the success/error messages there.
Post Reply