php sending two emails

Hi there,

I have this code I put together from other scripts and stuff I found on the internet,

Somehow it is sending me two emails, one email when I just load the page, the second ofcourse when I submit.

Also, the header() is not sending me to the page I want it to…it just stays on the same form page, if anyone can help me find out what is going on, it would be much appreciated :slight_smile:

Thank you,


<style>
.error {color: #FF0000;}
h6
{
  font-family: bookman old style;
  font-size:20px;
  text-align: center;
  font-weight: normal;
}
h5
{
  font-family: bookman old style;
  font-size:15px;
  text-align: center;
  font-weight: normal;
}
</style>

<?php


// define variables and set to empty values
$nameErr = $emailErr  = $websiteErr = $categoryErr = "";
$name = $email = $comment  = $website = $category = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }
   
   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format"; 
     }
   }
     
   if (empty($_POST["website"])) {
     $websiteErr = "URL is required";
   } else {
     $website = test_input($_POST["website"]);
     // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
     if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
       $websiteErr = "Invalid URL"; 
     }
   }

   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }
   if (empty($_POST["category"])) {
     $categoryErr = "Category is required";
   } else {
     $category = test_input($_POST["category"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<?php include'header.php'?>
<h6>Link Submission</h6>
<h5><p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   URL: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error">* <?php echo $websiteErr;?></span>
   <br><br>
   Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
Category Of Site: <select size="1" name="category"> 
   <option value="<?php echo $category;?>"> -- Please select -- </option>    
   <option>Arts</option>     
   <option>Business</option>     
   <option>Computers</option>     
   <option>Games</option>     
   <option>Health</option>     
   <option>Home</option>     
   <option>Kids and Teens</option>     
   <option>News</option>     
   <option>Recreation</option>     
   <option>Reference</option>     
   <option>Science</option>     
   <option>Shopping</option>     
   <option>Society</option>     
   <option>Sports</option>     
   <option>World</option>

  </select><span class="error">* <?php echo $categoryErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
</h5><?php include'footer.php'?>
<?php
$myemail = "[email protected]";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');



?>

You dont have the mail code within the POST so it sends mail on every page load.

tried it, still sending email twice :’(

Post your updated code.

[code]
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}

<?php $nameErr = $emailErr = $websiteErr = $categoryErr = ""; $name = $email = $comment = $website = $category = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["website"])) { $websiteErr = "URL is required"; } else { $website = test_input($_POST["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["category"])) { $categoryErr = "Category is required"; } else { $category = test_input($_POST["category"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $myemail = "[email protected]"; $subject = "Link Submission"; $message = "Your Link Submission form has been submitted by: Website Name: $name E-mail: $email URL: $website Category: $category Description: $comment"; mail($myemail, $subject, $message); header('Location: submitthanks.php'); ?> <?php include'header.php'?>
Link Submission

* required field.

"> Name Of Site: * <?php echo $nameErr;?>

E-mail: * <?php echo $emailErr;?>

URL: * <?php echo $websiteErr;?>

Description: <?php echo $comment;?>

Category Of Site: -- Please select -- Arts Business Computers Games Health Home Kids and Teens News Recreation Reference Science Shopping Society Sports World

* <?php echo $categoryErr;?>



<?php include'footer.php'?>

[/code]

Its still not within the POST code

ok I figured it out It is in the post section, and now yes you get on the page and it doesn’t send the two emails, but now as per the validation, it validates if you fill out the whole form and miss only one , but if you miss all of them and hit submit it sends me a copy of the blank form, it pulls up the notification next to the boxes telling the user what they are missing, but sends me the part finished form

That’s because you have nothing in place to stop it from sending if there is an error.

Pseudo Code:
Submit form
If errors, show errors ELSE send email

ok would you be able to show me the code to do that?

Does Anyone know how to do that?

There are a few ways to prevent sending blank emails.

  1. Use required in your html input so users cannot submit the form unless it has a value.
<input name="abc" type="text" required>
  1. in your PHP code check the value has been set.
    [php]
    // check the form has been submitted.
    if(isset($_POST[‘submit’])) {

    // check the abc is set and has a value.
    if(isset($_POST[‘abc’]) && !empty($_POST[‘abc’])) {
    // remove whitespace.
    $abc= trim($_POST[‘abc’]);
    }
    else {
    $abc = false;
    echo ‘Please enter a value!’;
    }

    // do the rest of your php processing and send the mail…
    }
    [/php]

Make sense?

Red :wink:

thank you for your time :slight_smile:

I fixed it I added

if (!empty($_POST[‘name’]) && !empty($_POST[‘email’]) && !
empty($_POST[‘website’]) && !empty($_POST[‘category’]))

before the mail function :slight_smile:

You’re welcome :slight_smile:

Whilst this indeed will work, it’s not the best practice as you are sending raw data in the email.

You should clean up the data (remove excess whitespace etc.) using the code i supplied below.
Another product of this ‘cody tidying’ is you get variables to use making the code easier to read.

Example:
[php]
mail($_POST[‘email_address’], $_POST[‘subject’], $_POST[‘comment’]);

// or

mail($email, $subject, $comment);
[/php]

They both do the same thing only number two has been cleaned of excess whitespace (and any other processing you wish to do) it also looks ‘cleaner’ too, much easier to read - don’t you think?

Expanding on the code i posted below:
[php]
// check the form has been submitted.
if(isset($_POST[‘submit’])) {

 // create an array to hold any errors that may occur.
 $errors = array();
  
  // check the abc is set and has a value.
  if(isset($_POST['abc']) && !empty($_POST['abc'])) {
      // remove whitespace.
      $abc= trim($_POST['abc']);
  }
 else {
     $abc = false;
     $errors[] =  'Please enter a value in the abc box!';
 }

 // do the rest of your php processing and send the mail..

// check the errors array is still empty!
if(empty($errors)) {
    $headers = "From:noreply@".$_SERVER['HTTP_HOST'];
    mail($email, $subject, $message, $headers);
 }
 else {
     // there has been an error..
     echo '<p>Please fix the following errors:</p>';
     foreach($errors as $error) {
        echo "<p>$error</p>";
     }
 }

}
[/php]

well thank you so much :slight_smile: that was nice of you!

;D

You’re very welcome.
Red :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service