Page 1 of 1

Message from contact form arrives twice

Posted: Fri Sep 05, 2008 4:11 am
by Sindarin
I have a contact form but the problem is the message arrives two times not one as intended, here is my code, am I doing something wrong?

Code: Select all

<?php
 
//Contact Form
 
//the sending address [From]
$from = "info@removedforspamreasons.com";
 
//the destination address [To]
$to = "info@removedforspamreasons.com";
 
 
//the message subject
$subject = "New Contact [from Site]";
 
//ip list filename
$ip_file="ip-list.txt";
 
//function email_validate
function email_validate ($email) 
{
    if (strlen (trim ($email)))
        return (eregi("^[a-z0-9]([_\\.\\-]?[a-z0-9]+)*@((([a-z0-9]+[a-z0-9\\-]*[a\-z0-9]+)|[a-z0-9])+\\.)+[a-z]{2,10}$", $email));
    return false;
}   
 
//get variables from form
 
$first = $_POST['first'];
$last = $_POST['last'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$address = $_POST['address'];
$number = $_POST['arithm'];
$city = $_POST['city'];
$tk = $_POST['tk'];
$email = $_POST['email'];
$url = $_POST['url'];
$message = $_POST['text'];
 
//get IP address
$ip=$_SERVER['REMOTE_ADDR'];
 
//read file and check the IPs from the array
 
$ip_id=fopen($ip_file,"r");
$ip_array=fread($ip_id,9999);
fclose($ip_id);
$ip_array = explode("\n", $ip_array);
 
//check the IP
if(in_array($_SERVER['REMOTE_ADDR'], $ip_array)) 
{
//blocked IP returned, display error and stop submission
echo "<center><font family='Verdana'><font >We are sorry but your IP address <font color='red'><b>$ip</b></font> has been blocked from using the contact form.<br /></font></center>";
}
else
{
 
//IP not in blocked IP list, allow contact
 
//check the required fields and validate email
$email_is_valid=email_validate($email);
if ($first=="" || $last=="" || $message=="" || $email=="" || $email_is_valid==false)
{
//some of the required fields are not filled
echo "<center>The following problems occured while sending the form:<br /><br />";
if ($first==""){echo "<font color='red'><br/>Name is a required field.<br />";}
if ($last==""){echo "Surname is a required field.<br />";}
if ($message==""){echo "The message cannot be empty.<br />";}
if ($email_is_valid==false){echo "The e-mail address "; if ($email==""){echo "is not filled in ";} echo "and it's not valid.<br /></font color>";}
echo "<br /><br /><a href='javascript&#058;history.back(-1);'><b>[ Retry ]</b></a></center>";
}
else
{
//all required fields are completed
 
//wrap characters so the message shows shorter
$wrappedmessage =wordwrap($message, 50, "<br />\n");
    
//set the message content
$form_message = "
 
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Contact Form Message</title>
<style type='text/css'>
<!--
.style2 {font-size: 10px}
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #999999;
}
body {
    background-color: #FFFFFF;
    margin-left: 32px;
    margin-top: 32px;
    margin-right: 32px;
    margin-bottom: 32px;
}
.style3 {font-size: 12px; }
.style4 {
    color: #000000;
    font-weight: bold;
}
.style5 {font-size: 12px; color: #000000; }
-->
</style>
</head>
 
<body>
<table width='349' height='508' border='1' bordercolor='#666666'>
  <tr>
    <td height='26' colspan='2'><div align='center'><strong>Contact Form | New message </strong></div></td>
  </tr>
  <tr>
    <td height='19' colspan='2'>&nbsp;</td>
  </tr>
  <tr>
    <td height='28' align='left' valign='middle'>IP Address</td>
    <td align='left' valign='middle'><span class='style4'>$ip</span></td>
  </tr>
  <tr>
    <td width='145' height='30' align='left' valign='middle'>Name</td>
    <td width='188' align='left' valign='middle'><span class='style4'>$first</span></td>
  </tr>
  <tr>
    <td height='30' align='left' valign='middle'>Surname</td>
    <td align='left' valign='middle'><span class='style4'>$last</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>Birth Date</td>
    <td align='left' valign='middle'><span class='style4'>$day $month $year</span></td>
  </tr>
  <tr>
    <td height='30' align='left' valign='middle'>Address</td>
    <td align='left' valign='middle'><span class='style4'>$address $number $city</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>Zip</td>
    <td align='left' valign='middle'><span class='style4'> $tk</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>e-mail</td>
    <td align='left' valign='middle'><span class='style4'>$email</span></td>
  </tr>
  <tr>
    <td height='31' align='left' valign='middle'>Referrer </td>
    <td align='left' valign='middle'><span class='style4'>$url</span></td>
  </tr>
  <tr>
    <td height='29' align='left' valign='middle' class='style3'>Message</td>
    <td align='left' valign='middle' class='style2'>&nbsp;</td>
  </tr>
  <tr>
    <td height='111' colspan='2' align='left' valign='top' class='style5'>$message</td>
  </tr>
  <tr>
    <td height='51' colspan='2' class='style2'><div align='justify'>iso-greek encoded message</div></td>
  </tr>
</table>
</body>
</html>
 
 
";
    
//set the mail headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-7\r\n";
$headers .= "To: <$from> \r\n";
$headers .= "From: <$to>\r\n";
    
//send the message
mail($to, $subject, $form_message, $headers);
 
    //output success html to the user
echo "<p style='a:link {color: #840E64;text-decoration: none;}'><font color='black'><center><br/>Your message was successfully sent!</font><br /><a href='javascript&#058;history.back(-1);'><b>[ OK ]</b></a></p></center>";
 
}
}
 
?>
 

Re: Message from contact form arrives twice

Posted: Fri Sep 05, 2008 4:22 am
by greyhoundcode
Could it be that the submit button is being hit (perhaps accidentally) more than once? If so, you help to prevent multiple submission by using a piece of Javascript that disables the submit button after being clicked, just add an onsubmit attribute to your opening form tag along the lines of-

<form action="action_url.php" method="get"
onsubmit="document.getElementById('submit').disabled = true;">

Might help.

Re: Message from contact form arrives twice

Posted: Fri Sep 05, 2008 4:54 am
by Sindarin
I've already implemented such thing in the new contact form I was making and I was reported the same thing.