Jump to content

PHP form mail problem


Bavilo

Recommended Posts

Hello everyone,

I recently wrote a mail script. I am having a few problems with it though. For example, when i submit information from the form page, and leave out a field, the script will then process and echo back the form fields saying that something was missing...when i then try to submit the missing info from that page, it works sometimes...seriously...it works one time and doesn't another. I dont know why it would do this. Another problem i have is that when someone wants to enter a street address such as 123 first str. it will say some field was missing because it doens't like the ".", how could i fix this just for this one field?

Here are the 2 codes, i hope someone can look over them and see if anything else is wrong with it.
Also, the actual site where you can try for yourself is here htttp://spunge.org/~bavilo/EuroDogCrates/index.php?id=id/contact

Btw, I haven't worked on the wrong email function yet, im gonna make it echo the same form like the missing field function. But i would like to get this sorted out first.

Contant.php
[code]
<div class="inside">
  <h4>For a free estimate, please supply us with the following information.</h4>
<br /><br /><br />
  <form id="form" method="post" action="index.php?id=id/test" enctype="multipart/form-data">
<table>
  <tr>
    <td>
      First Name:
    </td>
    <td>
      <input id="field" type="text" name="FirstName" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      Last Name:
    </td>
    <td>
      <input id="field" type="text" name="LastName" value=""/>
    </td>
  </tr>

  <tr>
    <td>
      Address:
    </td>
    <td>
      <input id="field" type="text" name="Address" value=""/>
    </td>
  </tr>
  <tr>
  <tr>
    <td>
      City:
    </td>
    <td>
      <input id="field" type="text" name="City" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      State:
    </td>
    <td>
      <select name="State">
        <option value='' >- - - - - -Please Select- - - - - -</option>
      <option value='AK' >ALASKA</option>
        <option value='AL' >ALABAMA</option>
        <option value='AR' >ARKANSAS</option>
        <option value='AZ' >ARIZONA</option>
        <option value='CA' >CALIFORNIA</option>
        <option value='CO' >COLORADO</option>
        <option value='CT' >CONNECTICUT</option>
        <option value='DC' >DISTRICT OF COLUMBIA</option>
        <option value='DE' >DELAWARE</option>
        <option value='FL' >FLORIDA</option>
        <option value='GA' >GEORGIA</option>
        <option value='HI' >HAWAII</option>
        <option value='IA' >IOWA</option>
        <option value='ID' >IDAHO</option>
        <option value='IL' >ILLINOIS</option>
        <option value='IN' >INDIANA</option>
        <option value='KS' >KANSAS</option>
        <option value='KY' >KENTUCKY</option>
        <option value='LA' >LOUISIANA</option>
        <option value='MA' >MASSACHUSETTS</option>
        <option value='MD' >MARYLAND</option>
        <option value='ME' >MAINE</option>
        <option value='MI' >MICHIGAN</option>
        <option value='MN' >MINNESOTA</option>
        <option value='MO' >MISSOURI</option>
        <option value='MS' >MISSISSIPPI</option>
        <option value='MT' >MONTANA</option>
        <option value='NC' >NORTH CAROLINA</option>
        <option value='ND' >NORTH DAKOTA</option>
        <option value='NE' >NEBRASKA</option>
        <option value='NH' >NEW HAMPSHIRE</option>
        <option value='NJ' >NEW JERSEY</option>
        <option value='NM' >NEW MEXICO</option>
        <option value='NV' >NEVADA</option>
        <option value='NY' >NEW YORK</option>
        <option value='OH' >OHIO</option>
        <option value='OK' >OKLAHOMA</option>
        <option value='OR' >OREGON</option>
        <option value='PA' >PENNSYLVANIA</option>
        <option value='PR' >PUERTO RICO</option>
        <option value='RI' >RHODE ISLAND</option>
        <option value='SC' >SOUTH CAROLINA</option>
        <option value='SD' >SOUTH DAKOTA</option>
        <option value='TN' >TENNESSEE</option>
        <option value='TX' >TEXAS</option>
        <option value='UT' >UTAH</option>
        <option value='VA' >VIRGINIA</option>
        <option value='VT' >VERMONT</option>
        <option value='WA' >WASHINGTON</option>
        <option value='WI' >WISCONSIN</option>
        <option value='WV' >WEST VIRGINIA</option>
        <option value='WY' >WYOMING</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Zip Code:
    </td>
    <td>
      <input id="field" type="text" name="Zip" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      Phone:
    </td>
    <td>
      (<input id="field" type="text" name="Area" size="3" maxlength="3" value=""/>) <input id="field" type="text" name="Pre" size="3" maxlength="3" value=""/> - <input id="field" type="text" name="Number" size="4" maxlength="4" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      Email:
    </td>
    <td>
      <input id="field" type="text" name="Email" value="" />
    </td>
  </tr>
  <tr>
    <td>
      Car Make:
    </td>
    <td>
      <input id="field" type="text" name="Make" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      Model:
    </td>
    <td>
      <input id="field" type="text" name="Model" value=""/>
    </td>
  </tr>
  <tr>
    <td>
      Year:
    </td>
    <td>
      <select name="Year">
        <option value='' >- -Please Select- -</option>
      <option value="1980">1980</option>
      <option value="1981">1981</option>
      <option value="1982">1982</option>
      <option value="1983">1983</option>
      <option value="1984">1984</option>
      <option value="1985">1985</option>
      <option value="1986">1986</option>
      <option value="1987">1987</option>
      <option value="1988">1988</option>
      <option value="1989">1989</option>
      <option value="1990">1990</option>
      <option value="1991">1991</option>
      <option value="1992">1992</option>
      <option value="1993">1993</option>
      <option value="1994">1994</option>
      <option value="1995">1995</option>
      <option value="1996">1996</option>
      <option value="1997">1997</option>
      <option value="1998">1998</option>
      <option value="1999">1999</option>
      <option value="2000">2000</option>
      <option value="2001">2001</option>
      <option value="2002">2002</option>
      <option value="2003">2003</option>
      <option value="2004">2004</option>
      <option value="2005">2005</option>
      <option value="2006">2006</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Single Crate:
    </td>
    <td>
      <select name="SingleQuantity">
        <option value='' >- -Please Select- -</option>
        <option value='0' >0</option>
        <option value='1' >1</option>
        <option value='2' >2</option>
      <option value='3' >3</option>
      <option value='4' >4</option>
      <option value='5' >5</option>
      <option value='6' >6</option>
      <option value='7' >7</option>
      <option value='8' >8</option>
      <option value='9' >9</option>
      <option value='10' >10</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Single Crate with Drawer:
    </td>
    <td>
      <select name="SingleDrawerQuantity">
        <option value='' >- -Please Select- -</option>
        <option value='0' >0</option>
        <option value='1' >1</option>
        <option value='2' >2</option>
      <option value='3' >3</option>
      <option value='4' >4</option>
      <option value='5' >5</option>
      <option value='6' >6</option>
      <option value='7' >7</option>
      <option value='8' >8</option>
      <option value='9' >9</option>
      <option value='10' >10</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Double Crate:
    </td>
    <td>
      <select name="DoubleQuantity">
        <option value='' >- -Please Select- -</option>
        <option value='0' >0</option>
        <option value='1' >1</option>
        <option value='2' >2</option>
      <option value='3' >3</option>
      <option value='4' >4</option>
      <option value='5' >5</option>
      <option value='6' >6</option>
      <option value='7' >7</option>
      <option value='8' >8</option>
      <option value='9' >9</option>
      <option value='10' >10</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Double Crate with Drawer:
    </td>
    <td>
      <select name="DoubleDrawerQuantity">
        <option value='' >- -Please Select- -</option>
        <option value='0' >0</option>
        <option value='1' >1</option>
        <option value='2' >2</option>
      <option value='3' >3</option>
      <option value='4' >4</option>
      <option value='5' >5</option>
      <option value='6' >6</option>
      <option value='7' >7</option>
      <option value='8' >8</option>
      <option value='9' >9</option>
      <option value='10' >10</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      Breed:
    </td>
    <td>
      <input id="field" type="text" name="Breed" value=""/>
    </td>
  </tr>  
  <tr>
    <td>
      Comments:
    </td>
    <td>
      <textarea id="field" name="Comments" rows="5" cols="40"></textarea>
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <div align="center">
        <input class="formbutton" type="submit" name="submit" value="Submit" />
        <input class="formbutton" type="reset" name="Reset" value="Reset" />
      </div>
    </td>
  </tr>    
</table>
  </form>
</div>
[/code]


Mail Script
[code]
<?php
if (!isset($_POST['submit'])) {
   echo "<div class=\"inside\">";
   echo "<h4>Error</h4>\n
      <h4><p>Accessing this page directly is not allowed.</p></h4>";
   echo "</div>";
   echo "</div>";
   echo "<div id=\"footer\">";
   echo "</div>";
   echo "</div>";
   echo "</body>";
   echo "</html>";
   exit;
}

function cleanUp($data) {
   $data = trim(strip_tags(htmlspecialchars($data)));
   return $data;
}

$first      = cleanUp($_POST['FirstName']);
$last       = cleanUp($_POST['LastName']);
$address    = cleanUp($_POST['Address']);
$city       = cleanUp($_POST['City']);
$state      = cleanUp($_POST['State']);
$zip        = cleanUp($_POST['Zip']);
$area       = cleanUp($_POST['Area']);
$pre        = cleanUp($_POST['Pre']);
$number     = cleanUp($_POST['Number']);
$email      = cleanUp($_POST['Email']);
$make       = cleanUp($_POST['Make']);
$model      = cleanUp($_POST['Model']);
$year       = cleanUp($_POST['Year']);
$single     = cleanUp($_POST['SingleQuantity']);
$singledr   = cleanUp($_POST['SingleDrawerQuantity']);
$double     = cleanUp($_POST['DoubleQuantity']);
$doubledr   = cleanUp($_POST['DoubleDrawerQuantity']);
$breed      = cleanUp($_POST['Breed']);
$comments   = cleanUp($_POST['Comments']);
$phone      = $area . '-' . $pre . '-' . $number;


if ((empty($first))    || (empty($last))     || (empty($address))  ||
    (empty($city))     || (empty($state))    || (empty($zip))      ||
    (empty($make))     || (empty($model))    || (empty($year))     ||
    (empty($single))   || (empty($singledr)) || (empty($double))   ||
    (empty($doubledr)) || (empty($breed))    || (empty($area))     ||
    (empty($pre))      || (empty($number)))
    
{
echo "<div class=\"inside\">";
echo "<h4>Error</h4>";
echo "<h4><p>You have missed one or more fields. Please fill them in and try again</p></h4>";
echo "<form method=\"post\" action=\"index.php?id=id/test\" enctype=\"multipart/form-data\">";
echo "<table>";
echo "<tr>";
echo "<td>First Name:</td>";
echo "<td><input type=\"text\" name=\"FirstName\" value=\"$first\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Last Name:</td>";
echo "<td><input type=\"text\" name=\"LastName\" value=\"$last\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address:</td>";
echo "<td><input type=\"text\" name=\"Address\" value=\"$address\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>City:</td>";
echo "<td><input type=\"text\" name=\"City\" value=\"$city\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>State:</td>";
echo "<td><select name=\"State\">
        <option value=\"\" >- - - - - -Please Select- - - - - -</option>
      <option value=\"AK\" >ALASKA</option>
        <option value=\"AL\" >ALABAMA</option>
        <option value=\"AR\" >ARKANSAS</option>
        <option value=\"AZ\" >ARIZONA</option>
        <option value=\"CA\" >CALIFORNIA</option>
        <option value=\"CO\" >COLORADO</option>
        <option value=\"CT\" >CONNECTICUT</option>
        <option value=\"DC\" >DISTRICT OF COLUMBIA</option>
        <option value=\"DE\" >DELAWARE</option>
        <option value=\"FL\" >FLORIDA</option>
        <option value=\"GA\" >GEORGIA</option>
        <option value=\"HI\" >HAWAII</option>
        <option value=\"IA\" >IOWA</option>
        <option value=\"ID\" >IDAHO</option>
        <option value=\"IL\" >ILLINOIS</option>
        <option value=\"IN\" >INDIANA</option>
        <option value=\"KS\" >KANSAS</option>
        <option value=\"KY\" >KENTUCKY</option>
        <option value=\"LA\" >LOUISIANA</option>
        <option value=\"MA\" >MASSACHUSETTS</option>
        <option value=\"MD\" >MARYLAND</option>
        <option value=\"ME\" >MAINE</option>
        <option value=\"MI\" >MICHIGAN</option>
        <option value=\"MN\" >MINNESOTA</option>
        <option value=\"MO\" >MISSOURI</option>
        <option value=\"MS\" >MISSISSIPPI</option>
        <option value=\"MT\" >MONTANA</option>
        <option value=\"NC\" >NORTH CAROLINA</option>
        <option value=\"ND\" >NORTH DAKOTA</option>
        <option value=\"NE\" >NEBRASKA</option>
        <option value=\"NH\" >NEW HAMPSHIRE</option>
        <option value=\"NJ\" >NEW JERSEY</option>
        <option value=\"NM\" >NEW MEXICO</option>
        <option value=\"NV\" >NEVADA</option>
        <option value=\"NY\" >NEW YORK</option>
        <option value=\"OH\" >OHIO</option>
        <option value=\"OK\" >OKLAHOMA</option>
        <option value=\"OR\" >OREGON</option>
        <option value=\"PA\" >PENNSYLVANIA</option>
        <option value=\"PR\" >PUERTO RICO</option>
        <option value=\"RI\" >RHODE ISLAND</option>
        <option value=\"SC\" >SOUTH CAROLINA</option>
        <option value=\"SD\" >SOUTH DAKOTA</option>
        <option value=\"TN\" >TENNESSEE</option>
        <option value=\"TX\" >TEXAS</option>
        <option value=\"UT\" >UTAH</option>
        <option value=\"VA\" >VIRGINIA</option>
        <option value=\"VT\" >VERMONT</option>
        <option value=\"WA\" >WASHINGTON</option>
        <option value=\"WI\" >WISCONSIN</option>
        <option value=\"WV\" >WEST VIRGINIA</option>
        <option value=\"WY\" >WYOMING</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Zip Code:</td>";
echo "<td><input type=\"text\" name=\"Zip\" value=\"$zip\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone:</td>";
echo "<td>(<input id=\"field\" type=\"text\" name=\"Area\" size=\"3\" maxlength=\"3\" value=\"$area\" />) <input id=\"field\" type=\"text\" name=\"Pre\" size=\"3\" maxlength=\"3\" value=\"$pre\" /> - <input id=\"field\" type=\"text\" name=\"Number\" size=\"4\" maxlength=\"4\" value=\"$number\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email:</td>";
echo "<td><input type=\"text\" name=\"Email\" value=\"$email\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Car Make:</td>";
echo "<td><input type=\"text\" name=\"Make\" value=\"$make\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Model:</td>";
echo "<td><input type=\"text\" name=\"Model\" value=\"$model\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Year:</td>";
echo "<td><select name=\"Year\">
        <option value=\"\" >- -Please Select- -</option>
      <option value=\"1980\">1980</option>
      <option value=\"1981\">1981</option>
      <option value=\"1982\">1982</option>
      <option value=\"1983\">1983</option>
      <option value=\"1984\">1984</option>
      <option value=\"1985\">1985</option>
      <option value=\"1986\">1986</option>
      <option value=\"1987\">1987</option>
      <option value=\"1988\">1988</option>
      <option value=\"1989\">1989</option>
      <option value=\"1990\">1990</option>
      <option value=\"1991\">1991</option>
      <option value=\"1992\">1992</option>
      <option value=\"1993\">1993</option>
      <option value=\"1994\">1994</option>
      <option value=\"1995\">1995</option>
      <option value=\"1996\">1996</option>
      <option value=\"1997\">1997</option>
      <option value=\"1998\">1998</option>
      <option value=\"1999\">1999</option>
      <option value=\"2000\">2000</option>
      <option value=\"2001\">2001</option>
      <option value=\"2002\">2002</option>
      <option value=\"2003\">2003</option>
      <option value=\"2004\">2004</option>
      <option value=\"2005\">2005</option>
      <option value=\"2006\">2006</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Single Crate:</td>";
echo "<td><select name=\"SingleQuantity\">
        <option value=\"\" >- -Please Select- -</option>
        <option value=\"0\" >0</option>
        <option value=\"1\" >1</option>
        <option value=\"2\" >2</option>
      <option value=\"3\" >3</option>
      <option value=\"4\" >4</option>
      <option value=\"5\" >5</option>
      <option value=\"6\" >6</option>
      <option value=\"7\" >7</option>
      <option value=\"8\" >8</option>
      <option value=\"9\" >9</option>
      <option value=\"10\" >10</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Single Crate with Drawer:</td>";
echo "<td><select name=\"SingleDrawerQuantity\">
        <option value=\"\" >- -Please Select- -</option>
        <option value=\"0\" >0</option>
        <option value=\"1\" >1</option>
        <option value=\"2\" >2</option>
      <option value=\"3\" >3</option>
      <option value=\"4\" >4</option>
      <option value=\"5\" >5</option>
      <option value=\"6\" >6</option>
      <option value=\"7\" >7</option>
      <option value=\"8\" >8</option>
      <option value=\"9\" >9</option>
      <option value=\"10\" >10</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Double Crate:</td>";
echo "<td><select name=\"DoubleQuantity\">
        <option value=\"\" >- -Please Select- -</option>
        <option value=\"0\" >0</option>
        <option value=\"1\" >1</option>
        <option value=\"2\" >2</option>
      <option value=\"3\" >3</option>
      <option value=\"4\" >4</option>
      <option value=\"5\" >5</option>
      <option value=\"6\" >6</option>
      <option value=\"7\" >7</option>
      <option value=\"8\" >8</option>
      <option value=\"9\" >9</option>
      <option value=\"10\" >10</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Double Crate with Drawer:</td>";
echo "<td><select name=\"DoubleDrawerQuantity\">
        <option value=\"\" >- -Please Select- -</option>
        <option value=\"0\" >0</option>
        <option value=\"1\" >1</option>
        <option value=\"2\" >2</option>
      <option value=\"3\" >3</option>
      <option value=\"4\" >4</option>
      <option value=\"5\" >5</option>
      <option value=\"6\" >6</option>
      <option value=\"7\" >7</option>
      <option value=\"8\" >8</option>
      <option value=\"9\" >9</option>
      <option value=\"10\" >10</option>
      </select>
    </td>";
echo "</tr>";
echo "<tr>";
echo "<td>Breed:</td>";
echo "<td><input type=\"text\" name=\"Breed\" value=\"$breed\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Comments:</td>";
echo "<td><textarea name=\"Comments\" rows=\"5\" cols=\"40\">$comments</textarea></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"2\">";
echo "<div align=\"center\">";
echo "<input class=\"formbutton\" type=\"submit\" name=\"submit\" value=\"Submit\" /> <input class=\"formbutton\" type=\"reset\" name=\"Reset\" value=\"Reset\" />";
echo "</div>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
echo "</div>";
echo "<div id=\"footer\">";
echo "</div>";
echo "</body>";
echo "</html>";  
exit;
}

if (!ereg("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$",$email)) {
echo "<h2>Error</h2>
<p>The e-mail address \"$email\" is not a valid e-mail address. Please edit it and try again</p>";
   exit;
}

$email = preg_replace("([\r\n])", "", $email);

$find = "[content-type|Content-Type|bcc:|cc:]";
if (preg_match($find, $first) || preg_match($find, $email) || preg_match($find, $last) || preg_match($find, $comments)) {
   echo "<h1>Error</h1>\n
      <p>No meta/header injections, please.</p>";
   exit;
}

$recipient = "mikew88@gmail.com";
$subject   = "EuroDogCrates.com Quote";

$message   = "First Name: $first \n";
$message  .= "Last Name: $last \n";
$message  .= "Address: $address \n";
$message  .= "City: $city \n";
$message  .= "State: $state \n";
$message  .= "Zip Code: $zip \n";
$message  .= "Phone: $phone \n";
$message  .= "Email: $email \n";
$message  .= "Car Make: $make \n";
$message  .= "Model: $model \n";
$message  .= "Year: $year \n";
$message  .= "Single Crate: $single \n";
$message  .= "Single Crate with Drawer: $singledr \n";
$message  .= "Double Crate: $double \n";
$message  .= "Double Crate with Drawer: $doubledr \n";
$message  .= "Breed: $breed \n";
$message  .= "Comments: $comments \n";

$headers   = "From: Quote \r\n";
$headers  .= "Reply-To: $email";

if (mail($recipient,$subject,$message,$headers)) {
   echo "<div class=\"inside\">";
   echo "<h4><p>Mail sent successfully.</p></h4>";
   echo "</div>";
  
} else {
   echo "<div class=\"inside\">";
   echo "<h4><p>Sorry, But the mail could not be sent. Please try again later.</p></h4>";
   echo "</div>";
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.