PHP file load twice in Chrome and safari
<p>I'm having a problem when I try to insert mydata with my php file, then I check on Network tab, It's showing that my insertt.php running 2 times then it insert blank row to my database which I don't want here are my Codes.</p>

<p>index.php</p>

<pre><code>&lt;!DOCTYPE html&gt;
 &lt;html lang="en" ng-app="myApp" &gt; 
&lt;head&gt;
  &lt;link rel="icon" 
      type="image/png" 
      href="icon128-2x.png"&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;!--&lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;

  &lt;meta name="description" content=""&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;--&gt;
  &lt;title&gt;My AngularJS App&lt;/title&gt;
  &lt;!--&lt;link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css"&gt;
  &lt;link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css"&gt;--&gt;
  &lt;link rel="stylesheet" href="app.css"&gt;
  &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;link href="css/bootstrap-responsive.min.css" rel="stylesheet"&gt;
  &lt;script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"&gt;&lt;/script&gt;
  &lt;style&gt;
    input { 
    text-align: center;    
}
  &lt;/style&gt;
&lt;/head&gt;
  &lt;body style="background-color:lightgreen;"&gt;
  &lt;!--div class="jumbotron" style="background-color:lightblue;"&gt;
  &lt;h1&gt;Bootstrap Tutorial&lt;/h1&gt;
  &lt;p&gt;Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile-first projects on the web.&lt;/p&gt;
&lt;/div--&gt;
&lt;nav class="navbar navbar-default" role="navigation"&gt;
  &lt;div class="container"&gt;
    &lt;!-- Brand and toggle get grouped for better mobile display --&gt;
    &lt;div class="navbar-header"&gt;
      &lt;button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"&gt;
        &lt;span class="sr-only"&gt;Toggle navigation&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
      &lt;/button&gt;
      &lt;a class="navbar-brand" href="#"&gt;Thaishop Online&lt;/a&gt;
    &lt;/div&gt;

    &lt;!-- Collect the nav links, forms, and other content for toggling --&gt;
    &lt;div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"&gt;
      &lt;ul class="nav navbar-nav"&gt;
        &lt;li class="active"&gt;&lt;a href="#!/view1"&gt;หน้าหลัก &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#!/view2"&gt;วิธีการใช้งาน&lt;/a&gt;&lt;/li&gt;
       &lt;!-- &lt;li class="dropdown"&gt;
          &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"&gt;Dropdown &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
          &lt;ul class="dropdown-menu"&gt;
            &lt;li&gt;&lt;a href="#"&gt;Action&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;Another action&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;Something else here&lt;/a&gt;&lt;/li&gt;
            &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;Separated link&lt;/a&gt;&lt;/li&gt;
            &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;One more separated link&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/li&gt;--&gt;
      &lt;/ul&gt;
      &lt;form class="navbar-form navbar-left" role="search"&gt;
        &lt;div class="form-group"&gt;
          &lt;input type="text" class="form-control" placeholder="Search"&gt;
        &lt;/div&gt;
        &lt;button type="submit" class="btn btn-default"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
      &lt;ul class="nav navbar-nav navbar-right"&gt;
        &lt;li&gt;&lt;a href="#!/view2"&gt;สมัครสมาชิก&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;ul class="nav navbar-nav navbar-right"&gt;
        &lt;li&gt;&lt;a href="#!/viewLogin"&gt;เข้าสู่ระบบ&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;&lt;!-- /.navbar-collapse --&gt;
  &lt;/div&gt;&lt;!-- /.container-fluid --&gt;
&lt;/nav&gt;
&lt;img src="img\banner.jpg" class="img-responsive" alt="Cinque Terre"style="width: 100%; height: 350px;" &gt;








  &lt;div ng-view&gt;&lt;/div&gt;




  &lt;script src="js/jquery-2.2.3.js"&gt;&lt;/script&gt;
&lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;script src="bower_components/angular/angular.js"&gt;&lt;/script&gt;

  &lt;script src="bower_components/angular-route/angular-route.js"&gt;&lt;/script&gt;
  &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;script src="view1/view1.js"&gt;&lt;/script&gt;
  &lt;script src="view2/view2.js"&gt;&lt;/script&gt;
  &lt;script src="viewLogin/viewLogin.js"&gt;&lt;/script&gt;
  &lt;script src="viewSaveCafeDetail/viewSaveCafeDetail.js"&gt;&lt;/script&gt;
  &lt;script src="components/version/version.js"&gt;&lt;/script&gt;
  &lt;script src="components/version/version-directive.js"&gt;&lt;/script&gt;
  &lt;script src="components/version/interpolate-filter.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>view2.js</p>

<pre><code>'use strict';

angular.module('myApp.view2', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/view2', {
            templateUrl: 'view2/view2.html',
            controller: 'FrmController'
        });
    }])

    .controller('FrmController', function ($scope, $http, $location) {

        $scope.table = "";

        $scope.reset = function () {
            $scope.Username = "";
            $scope.Password = "";
            $scope.Firstname = "";
            $scope.Lastname = "";
            $scope.Email = "";
            $scope.Tel = "";
            $scope.AccountStat = "";
            $scope.VerifyCode = "";
            $scope.Verifystat = "";
            $scope.cafe_id = "";
            $scope.CafeName = "";
            $scope.CafeUrl = "";
            $scope.ajouter = "";
        }
        $scope.auth = function () {
            $location.url('/view1');
        };


        $scope.path = 'http://localhost/middlework/administration/dash.php';

        $scope.deleteData = function (cafe_id) {
            alert('ddd');


            var path = $scope.path + '?n=' + $scope.table + '&amp;id="' + cafe_id + '"&amp;action=delete' + '&amp;app=true';

            $http.get(path)
                .then(function (response) {
                    //First function handles success
                    $scope.getData();
                }, function (response) {
                    //Second function handles error
                    $scope.content = "Something went wrong";
                });
        }


        $scope.editData = function (id) {


            var path = $scope.path + '?n=' + $scope.table + '&amp;id="' + id + '"&amp;action=edit' + '&amp;app=true';

            $http.get(path)
                .then(function (response) {
                    //First function handles success
                    console.log(response);
                    $scope.id = response.data[0].id;
                    $scope.CafeName = response.data[0].CafeName;
                    $scope.update = true;


                }, function (response) {
                    //Second function handles error
                    $scope.content = "Something went wrong";
                });
        }

        $scope.getData = function () {



            var path = $scope.path + '?n=' + $scope.table + '&amp;app=true';

            $http.get(path)
                .then(function (response) {
                    //First function handles success
                    $scope.content = response.data;
                    $scope.update = false;
                    $scope.reset();
                }, function (response) {
                    //Second function handles error
                    $scope.content = "Something went wrong";
                });
        }



        $scope.sendEmail = function () {


            $http.post('http://localhost/sendmail.php', $scope.formdata
            ).success(function (data, status, headers, config) {
                if (data) {
                    console.log(data);


                }

            }).error(function (data, status) { // called asynchronously if an error occurs
                // or server returns response with an error status.
                $location.url('/view1');
                console.log(data);
            });

        }




        $scope.SignUp = function () {


            var Username = $scope.formdata.Username;
            var session_id = /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
            $scope.AccountStat = "USER";
            $scope.Verifystat = "Verified";
            $scope.table = "Users";
            $scope.ajouter = 1;
            $scope.formdata;
            var path = 'http://localhost/insertt.php';
            var Password = $scope.formdata.Password;
            var ConPass = $scope.formdata.ConfirmPassword;
            var Firstname = $scope.formdata.Firstname;
            var Lastname = $scope.formdata.Lastname;
            var Email = $scope.formdata.Email;
            var Tel = $scope.formdata.Tel;
            var CafeName = $scope.formdata.CafeName;
            var postdata = {

                'Username': $scope.formdata.Username,
                'Password': $scope.formdata.Password,
                'Firstname': $scope.formdata.Firstname,
                'Lastname': $scope.formdata.Lastname,
                'Email': $scope.formdata.Email,
                'Tel': $scope.formdata.Tel,
                'CafeName': $scope.formdata.CafeName,
                'AccountStat': $scope.AccountStat,
                'Verifystat': $scope.Verifystat,
                'ajouter': $scope.ajouter,
                'table': $scope.table,
                'app': true
            };
            var headers = {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS",
                "Content-Type": "undefined",
                "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
            }
            if (Password == ConPass) {

                $http.post(path, postdata, headers
                ).success(function (data, status, headers, config) {
                    if (data) {
                        console.log(data);
                       // alert("สมัครสมาชิกสำเร็จ");
                       //  $location.url('/view1');
                        //$scope.insertcafe();
                        //$scope.sendEmail();
                        //  $scope.reset();
                        //  $scope.getData();


                    }

                }).error(function (data, status) { // called asynchronously if an error occurs
                    // or server returns response with an error status.
                    console.log(data);
                });

            } else {
                alert('กรุณากรอก Password ให้ตรงกัน');
            }

        }
        $scope.insertcafe = function () {
            var Username = $scope.formdata.Username;
            var CafeName = $scope.formdata.CafeName;
            $scope.table = "cafe";
            $scope.ajouter = 1;
            $scope.formdata;
            var path = $scope.path;
            var postdata = {

                'Username': $scope.formdata.Username,
                'CafeName': $scope.formdata.CafeName,
                'ajouter': $scope.ajouter,
                'table': $scope.table,
                'app': true
            };
            var headers = {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS",
                "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
            }
            $http.post(path, postdata, headers
            ).success(function (data, status, headers, config) {
                if (data) {
                    console.log(data);




                }

            }).error(function (data, status) { // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.log("mai sum red");
            });


        }
    });
</code></pre>

<p>view2.html</p>

<pre><code> &lt;div  &gt;
    &lt;form name="form1"  method="post" style="text-align:center;" align="center"&gt;

            &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3" &gt;
        &lt;label for="InputName" &gt;&lt;h1&gt;กรอกข้อมูลผู้ใช้&lt;/h1&gt;&lt;/label&gt;
          &lt;input type="text" class="form-control" ng-model="formdata.Firstname"  name="inputFirstname" placeholder="ชื่อจริง" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="text" class="form-control" ng-model="formdata.Lastname" name="inputLastname" placeholder="นามสกุล" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="text" class="form-control" ng-model="formdata.Username"  placeholder="Username" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="password" class="form-control" ng-model="formdata.Password"  placeholder="Password" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="password" class="form-control" ng-model="formdata.ConfirmPassword"  placeholder="Password อีกครั้ง" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="text" class="form-control"  ng-model="formdata.Email" name="inputEmail" placeholder="E-Mail" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="text" class="form-control" ng-model="formdata.Tel" name="inputTel" placeholder="โทรศัพท์"&gt;
        &lt;/div&gt;
                &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
          &lt;input type="text" class="form-control" ng-model="formdata.CafeName" name="inputCafeName" placeholder="ชื่อร้าน"&gt;
        &lt;/div&gt;

                &lt;div class="form-group col-md-6 col-xs-12 col-md-offset-3"&gt;
            &lt;input type="hidden" ng-model="table" value="cafe"&gt;
          &lt;input type="hidden" ng-model="ajouter" value="1" &gt;
          &lt;input ng-model="insertuser" ng-show="formdata.Firstname.length &amp;&amp; formdata.Lastname.length &amp;&amp; formdata.Username.length &amp;&amp; formdata.Password.length &amp;&amp;
          formdata.ConfirmPassword.length &amp;&amp; formdata.Email.length &amp;&amp; formdata.Tel.length &amp;&amp; formdata.CafeName.length" ng-click="SignUp();" type="submit" value="insert" class="btn btn-primary"&gt;
       &lt;!--&lt;input ng-model="insertuser" ng-click="SignUp();" type="submit" value="insertt" class="btn btn-primary"&gt;--&gt;
        &lt;/div&gt;
&lt;/form&gt;



                        &lt;!--&lt;input type="text" ng-model="cafe_id" value="" placeholder="CafeId"&gt;&lt;/br&gt;
                        &lt;input type="text" ng-model="CafeName" value="" placeholder="CafeName"&gt;&lt;/br&gt;
            &lt;input type="text" ng-model="Username" value="" placeholder="Username"&gt;&lt;/br&gt;
                        &lt;input type="text" ng-model="CafeUrl" value="" placeholder="CafeUrl"&gt;&lt;/br&gt;
                    &lt;input type="hidden" ng-model="table" value="cafe"&gt;
          &lt;input type="hidden" ng-model="ajouter" value="1" &gt;
          &lt;input ng-hide="update" ng-click="SignUp();" type="submit" value="insert" class="btn btn-primary"&gt;
          &lt;input ng-show="update" ng-click="update();" type="submit" value="update" class="btn btn-primary"&gt;

          &lt;input ng-click="getData();" type="submit" value="get" class="btn btn-primary"&gt;--&gt;

&lt;table  border="1"&gt;
&lt;tr ng-repeat="x in content"&gt;
  &lt;td&gt;{{x.id}}&lt;/td&gt;
  &lt;td&gt;{{x.CafeName}}&lt;/td&gt;
  &lt;td&gt;&lt;button ng-click="editData(x.id)" &gt;Edit&lt;/button&gt;&lt;/td&gt;
  &lt;td&gt;&lt;button ng-click="deleteData(x.id)" &gt;Del&lt;/button&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;



    &lt;/form&gt;
&lt;/div&gt;
</code></pre>

<p>insertt.php</p>

<pre><code>&lt;?php
header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods : GET,POST,PUT,DELETE,OPTIONS");
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With, X-YOUR-CUSTOM-HEADER');
    header("Content-Type : application/json");/*or whatever type is relevant */
        header("Accept : application/json");

$serverName = "localhost";
    $userName = "root";
    $userPassword = "";
    $dbName = "middlework";

    $conn = new mysqli($serverName,$userName,$userPassword,$dbName);
    mysqli_set_charset($conn,"utf8");
session_start();

  $postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$strSQL = "INSERT INTO users ";
$strSQL .="(Username,Password,Firstname,Lastname,Email,Tel,AccountStat,VerifyCode,Verifystat) ";
$strSQL .="VALUES ";
$strSQL .="('".$request-&gt;Username."','".$request-&gt;Password."','".$request-&gt;Firstname."' ";
$strSQL .=",'".$request-&gt;Lastname."','".$request-&gt;Email."','".$request-&gt;Tel."','User','".session_id()."','None') ";


#$findmax = "SELECT MAX(cafe_id)  FROM cafe where Username = 'kkk'";

#$max = $conn-&gt;query($findmax);
#$row = mysqli_fetch_assoc($max);
#while ($row = mysqli_fetch_assoc($max)) {
   # echo $row['cafe_id'];

#}
mysqli_query($conn,$strSQL);
/*if(mysqli_query($conn,$strSQL))
{
    echo "Save User Done.[".$strSQL."]";

}
else
{
    echo "Error Save [".$strSQL."]";
}*/
    $insertcafe = "INSERT INTO cafe (cafe_id,Username,CafeName) VALUES ('1' , '".$request-&gt;Username."', '".$request-&gt;CafeName."')";
if($conn-&gt;query($insertcafe))
{
    echo "Save Cafe Done.[".$insertcafe."]";
}
else
{
    echo mysqli_error($conn);
    echo "Save Cafe Fail[".$insertcafe."]";
}
require_once('PHPMailer/PHPMailerAutoload.php');
        $mail = new PHPMailer();
$mail-&gt;IsHTML(true);
$mail-&gt;CharSet = "utf-8";
$mail-&gt;IsSMTP();
$mail-&gt;SMTPAuth = true; // enable SMTP authentication
$mail-&gt;SMTPSecure = "ssl"; // sets the prefix to the servier
$mail-&gt;Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail-&gt;Port = 465; // set the SMTP port for the GMAIL server
$mail-&gt;Username = "kitsakorn.p55@rsu.ac.th"; // GMAIL username
$mail-&gt;Password = "1100501068349"; // GMAIL password
$mail-&gt;From = "kitsakorn.p55@rsu.ac.th"; // "name@yourdomain.com";
$mail-&gt;FromName = "ThaiCoffeeShopOnline";  // set from Name
$mail-&gt;Subject = "ยืนยันการสมัครสมาชิก ThaiCoffeeShopOnline";                            
$mail-&gt;Body = "ขอขอบคุณที่สมัครเป็นสมาชิกกับเรา กรุณาคลิก Url ด้านล่างเพื่อทำการ Activate บัญชีของคุณ&lt;/br&gt;
               http://localhost/activate.php?sid=".session_id()."&amp;uid=".$request-&gt;Username."&lt;/br&gt;&lt;/br&gt; ThaiCoffeeShop.com";




$mail-&gt;AddAddress($request-&gt;Email); // to Address

if (!$mail-&gt;send()) {
    echo "Mailer Error: " . $mail-&gt;ErrorInfo;
} else {
    echo "Message sent!";
    //json_decode();
}
/*for ($i=1;$i&lt;100;$i++){
    $insertcafe = "INSERT INTO cafe (cafe_id,Username,CafeName) VALUES ('".$i."' , 'kkk', '".$request-&gt;CafeName."')";
#$queryy = $conn-&gt;query($insertcafe);
if($conn-&gt;query($insertcafe))
{
    echo "Save Cafe Done.[".$insertcafe."]";

    break;
}
else
{

    echo mysqli_error($conn);
    echo "Save Cafe Fail.".$i."[".$insertcafe."]";
}
echo $request-&gt;CafeName;
}*/
$conn-&gt;close();
?&gt;
</code></pre>

<p>app.js</p>

<pre><code>    'use strict';

// Declare app level module which depends on views, and components
angular.module('myApp', [
  'ngRoute',
  'myApp.view1',
  'myApp.view2',
  'myApp.viewLogin',
  'myApp.SaveCafeDetail',
  'myApp.version'
]).
config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
  $locationProvider.hashPrefix('!');

  $routeProvider.otherwise({redirectTo: '/view1'});
}]);
</code></pre>

<p><a href="https://i.stack.imgur.com/NzmKC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NzmKC.png" alt="enter image description here"></a></p>