2

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"); 
?>
dmitriy
  • 488
  • 6
  • 25

4 Answers4

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("");
               }
             });
        });
    });
Jakub Krawczyk
  • 950
  • 8
  • 16
0
app.controller('threeCtrl',function($scope){
    $("#subBusinessOne").submit(function(e) {
    e.preventDefault();
    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("");
               }
             });
        });
    });
Karol Klepacki
  • 2,070
  • 1
  • 18
  • 30
0

Use .preventDefault()

 app.controller('threeCtrl',function($scope){
        $("#subBusinessOne").click(function(e) {
        e.preventDefault();
        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.
            });
        });
James
  • 1,436
  • 1
  • 13
  • 25
0

I guess you are trying to make cross AJAX request, and it's reason why you got 2 email instead 1.
Because first request with method OPTIONS (to check available to send requests from other domains) and second request with method POST

Michail M.
  • 735
  • 5
  • 11
  • how can i fix it? It's always work well but i use angularRouting at first and i think the problem because i don't understand how angular work with form – dmitriy Aug 12 '16 at 10:45
  • Take a look on http://stackoverflow.com/questions/22968406/how-to-skip-the-options-preflight-request-in-angularjs – Michail M. Aug 12 '16 at 11:28