$0.26

After submitting a form to email I get 2 email instead 1

Ask a question+
3

After submitting a form to email I get 2 email instead 1. How can i fix it? I need that only 1 letter come to email.

js:

app.controller('threeCtrl',function($scope){
    $("#subBusinessOne").click(function() {
    var url = "businessFormOne.php"; 
    $.ajax({
               type: "POST",
               url: url,
               data: $("form#businessFormOne").serialize(), 
               success: function(data)
               {
                   var name = $("input[name=name]").val("");
                   var rel= $("input[name=phone]").val("");
               }
             });
    return false; // avoid to execute the actual submit of the form.
        });
    });

php:

<?php 
$ToEmail = 'myemail.com'; 
$EmailSubject = 'Ամսեկան հաշվետվություն'; 
$mailheader = "From: ".$_POST["email"]."\r\n";
$MESSAGE_BODY = "Անուն: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Հեռախոսահամար: ".$_POST["phone"].""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>
add comment

1 Answer

1

I guess that #subBusinessOne is a form submit button. You’re sending an AJAX request and then submitting the form again as a normal HTTP request.

Instead of detecting button click event, you should check if the form has been submitted, then prevent default action and send the AJAX request. Your JS code would then look like this:

    app.controller('threeCtrl',function($scope){
    $("#businessFormOne").submit(function(e) {

    e.preventDefault(); // this is to avoid the actual submit

    var url = "businessFormOne.php"; 
    $.ajax({
               type: "POST",
               url: url,
               data: $("form#businessFormOne").serialize(), 
               success: function(data)
               {
                   var name = $("input[name=name]").val("");
                   var rel= $("input[name=phone]").val("");
               }
             });
        });
    });
Yusuf Tazim 20
add comment

Your Answer