Madhusudan Mishra Madhusudan Mishra - 4 months ago 25
PHP Question

How to run PHP Mail Form Script only when someone Successfully Submit the Form?

I have the following code for my HTML Form with PHP on the same page. Actually, the problem is that when anybody opens the website it automatically runs the PHP

Mail()
script and send me an empty mail.

I want it to work only when somebody successfully submits the form. Please help.

<!--HTML FORM--><form method="post" name="contactform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" onsubmit="return validateform()">
<input type="text" name="fullname" class="fullname" placeholder="Full Name*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Full Name*';}">

<input type="text" name="email" class="email" placeholder="Email Address*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Email Address*';}">

<textarea placeholder="Your Message*:" name="message" class="message"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Your Message*';}"></textarea>

<input type="submit" name="submit" value="Submit" />
</form><!--/HTML FORM> <!--Contact Form PHP-->
<?php
// define variables and set to empty values
$fullname = $email = $message = "";
$subject="Message from Website Visitor:".test_input($_POST["fullname"]);
$to="camadhusudanmishra@gmail.com";
$headers = "From:".test_input($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = test_input($_POST["fullname"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;}
$message = str_replace("\n.", "\n..", $message);

mail($to,$subject,$message,$headers);

?>
<!--/Contact Form PHP-->


JS Code for Validation:

function formValidation() {
var fullname = document.contactform.fullname;
var email = document.contactform.email;
var message = document.contactform.message;

if(validateName(fullname))
{
if(validateEmail(email))
{
if(validateMessage(message))
{
}
}
}
return false;
}

function validateName(fullname)
{
var letters = /^[A-Za-z]+$/;
if(fullname.value.match(letters) && fullname.value.length >= 3 && fullname.value.length <= 30)
{
return true;
}
else {
alert('Please enter a valid name');
return false;
}
}

function validateEmail(email)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(email.value.match(mailformat))
{
return true;
}
else
{
alert("You have entered an invalid email address!");
return false;
}
}

function validateMessage(message)
{
if(message.value.length >=5 && message.value.length <=300)
{
alert('Form Succesfully Submitted');
window.location.reload();
return true;
}
else
{
alert("Please type a valid message.");
return false;
}
}

Answer

You can use following code.

<?php
if(isset($_POST['submit']))
{

  // Add here function
 function test_input($data) {
 $data = trim($data);
  $data = stripslashes($data);
 $data = htmlspecialchars($data);
 return $data;}


// define variables and set to empty values
$fullname = $email = $message = "";
$subject="Message from Website Visitor:".test_input($_POST["fullname"]);
$to="camadhusudanmishra@gmail.com";
$headers = "From:".test_input($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = test_input($_POST["fullname"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);}


 $message = str_replace("\n.", "\n..", $message);

mail($to,$subject,$message,$headers);
 }
 ?>
  <!--/Contact Form PHP-->

 <!--HTML FORM-->
 <form method="post" name="contactform" action="<?php echo    htmlspecialchars($_SERVER['PHP_SELF']); ?>" onsubmit="return validateform()">
            <input type="text" name="fullname" class="fullname" placeholder="Full Name*"
   onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Full Name*';}">

            <input type="text" name="email" class="email" placeholder="Email Address*"
   onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Email Address*';}">

            <textarea placeholder="Your Message*:" name="message" class="message"
   onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Your Message*';}"></textarea>

   <input type="submit" name="submit" value="Submit" />
    </form><!--/HTML FORM> <!--Contact Form PHP-->