在Angularjs验证为真后,做一个正常的表单提交

时间:2022-11-24 19:34:55

I am implementing AngularJS on an existing web application that requires a common HTTP POST like you would do without AngularJS.

我正在一个现有的web应用程序上实现AngularJS,这需要一个普通的HTTP POST,就像您不需要AngularJS一样。

Does any one have a work around to do that? i have tried setting action="#" and action="." and just action and then do some jquery to inject a action like this. but nothing works

有人有工作要做吗?我试过设置action="#"和action=".",然后用一些jquery来注入这样的动作。但是没有工作

<script type="text/javascript">
    $("form").get(0).setAttribute( "action", "test.html" );
</script>

HERE IS MY FORM AND CODE //MY FORM

这是我的表格和代码

<form name="userForm" ng-submit="submitForm(userForm.$valid)" xt-form novalidate>
                        <div class="form-group">

                            <div class="col-sm-6" ng-class="{ 'has-error' : userForm.fornavn.$invalid && !userForm.fornavn.$pristine }">
                                <label class="control-label" for="textinput">Fornavn <span class="star-color">*</span></label>
                                <input autocomplete="off" type="text" value="<?php echo set_value('fornavn'); ?>" name="fornavn" ng-model="userFormData.fornavn" class="form-control" xt-validate msg-required="Du skal udfylde dit Fornavn" required>
                            </div>

                            <div class="col-sm-6" ng-class="{ 'has-error' : userForm.efternavn.$invalid && !userForm.efternavn.$pristine }">
                                <label class=" control-label" for="textinput">Efternavn <span class="star-color">*</span></label>
                                <input autocomplete="off" type="text" value="<?php echo set_value('efternavn'); ?>" name="efternavn" ng-model="userFormData.efternavn" class="form-control" xt-validate msg-required="Du skal udfylde dit Efternavn" required>
                            </div>
                        </div>

            <button  id="membership-box__payBtn" type="submit" ng-model="userFormData.betaling" name="betaling" class="btn btn-success text-uppercase">Gå til betaling</button>


</form>



//CODEIGNITER CONTROLLER

if (isset($_POST['betaling'])) {


$data['tilBetaling'] = array(
'oprettelse'        => $this->input->post('oprettelse'),
//                'medlemskab'        => $this->input->post('medlemskab'),
'tilBetaling'       => str_replace(',', '.', $this->input->post('tilBetaling')),
'pr_maaned'         => $this->input->post('pr_maaned'),
'start_date'        => $this->input->post('start_date'),
'til_dato'          => $this->input->post('til_dato'),
'pakke'             => $this->input->post('pakke'),
'type'              => $this->input->post('medlemskabTypeFinal'),
'getCenter'         => $this->input->post('getCenter'),
'medlemskabPakkePID'=> $this->input->post('medlemskabPakkePID'),
'ekstraInput'       => $this->input->post('ekstraInput'),
'periode_price'     => $this->input->post('periode_price'),
'kampagneTag'       => $this->input->post('kampagneTag'),
//                'header'            => $this->input->post('header'),
);



//Gem array til session
$_SESSION['betaling'] = $data['tilBetaling'];

}

3 个解决方案

#1


0  

Even if you do an AngularJs submit, the request does not go to server. The control still remains on client side. Then you can post the data/form etc through $http service. You can inject $http service in your controller. as

即使您使用AngularJs提交,请求也不会发送到服务器。控件仍然在客户端。然后您可以通过$http服务发布数据/表单等。可以在控制器中注入$http服务。作为

app.controller('reviewCtrl', ['$http', function($http){
        $scope.addReview = function(product){
        //use $http service here
        // Simple POST request example (passing data) :
        $http.post(' / someUrl ', {msg:' hello word!'}).
          success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
          }).
          error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });
        }
        }]);

note: please review the syntax before running the code.

注意:请在运行代码之前检查语法。

#2


2  

If you want to do a normal submit, you can handle ngClick in your controller:

如果你想做一个正常的提交,你可以在你的控制器中处理ngClick:

HTML

HTML

<div ng-controller="ctrl">
    <form name="userForm" action="user/add" method="post">
         Name: <input name="name" ng-model="user.name" />
         ...
         <button ng-click="onSubmit(userForm)">Submit</button>
    </form>
</div>

JS

JS

app.controller('ctrl', function($scope) {
   $scope.onSubmit = function(form) {
       if (form.$valid) {
           var e = document.getElementsByName(form.$name);
           e[0].submit();
       }
   }
});

An Alternative Solution

As an alternative, consider leveraging services, and redirecting after a successful POST in AngularJS.

另一种选择是考虑利用服务,并在AngularJS中获得成功后进行重定向。

HTML

HTML

<div ng-controller="ctrl">
    <form name="userForm" ng-submit="onSubmit(user)">
         Name: <input name="name" ng-model="user.name" />
         ...
         <button type="submit">Submit</button>
    </form>
</div>

JS

JS

app.factory('UserService', function($http) {
     return {
        addUser: function(user) {
           return $http({method:'POST', url:'api/user/add', data: user });
        }
     }
});
app.controller('ctrl', function($scope, $location, UserService) {
   $scope.onSubmit = function(user) {
       if ($scope.userForm.$valid) {
            UserService.addUser(user).success(function() {                    
                $location.path('/user/addSuccessful')
            });
       }
   }
});

#3


0  

To submit the Angular Js form ONLY after Angular validations are through or passed, you must use reviewForm.$valid flag chack in ng-submit.

若要仅在通过或通过角验证后提交角Js表单,必须使用reviewForm。$有效旗帜chack在ng-submit。

With this flag check, the form will not get submitted until all validations are passed.

使用此标志检查,在通过所有验证之前不会提交表单。

<form name="reviewForm" ng-controller="reviewCtrl" ng-submit="reviewForm.$valid && reviewCtrl.addReview(product)" novalidate>
....
....
</form>

#1


0  

Even if you do an AngularJs submit, the request does not go to server. The control still remains on client side. Then you can post the data/form etc through $http service. You can inject $http service in your controller. as

即使您使用AngularJs提交,请求也不会发送到服务器。控件仍然在客户端。然后您可以通过$http服务发布数据/表单等。可以在控制器中注入$http服务。作为

app.controller('reviewCtrl', ['$http', function($http){
        $scope.addReview = function(product){
        //use $http service here
        // Simple POST request example (passing data) :
        $http.post(' / someUrl ', {msg:' hello word!'}).
          success(function(data, status, headers, config) {
            // this callback will be called asynchronously
            // when the response is available
          }).
          error(function(data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });
        }
        }]);

note: please review the syntax before running the code.

注意:请在运行代码之前检查语法。

#2


2  

If you want to do a normal submit, you can handle ngClick in your controller:

如果你想做一个正常的提交,你可以在你的控制器中处理ngClick:

HTML

HTML

<div ng-controller="ctrl">
    <form name="userForm" action="user/add" method="post">
         Name: <input name="name" ng-model="user.name" />
         ...
         <button ng-click="onSubmit(userForm)">Submit</button>
    </form>
</div>

JS

JS

app.controller('ctrl', function($scope) {
   $scope.onSubmit = function(form) {
       if (form.$valid) {
           var e = document.getElementsByName(form.$name);
           e[0].submit();
       }
   }
});

An Alternative Solution

As an alternative, consider leveraging services, and redirecting after a successful POST in AngularJS.

另一种选择是考虑利用服务,并在AngularJS中获得成功后进行重定向。

HTML

HTML

<div ng-controller="ctrl">
    <form name="userForm" ng-submit="onSubmit(user)">
         Name: <input name="name" ng-model="user.name" />
         ...
         <button type="submit">Submit</button>
    </form>
</div>

JS

JS

app.factory('UserService', function($http) {
     return {
        addUser: function(user) {
           return $http({method:'POST', url:'api/user/add', data: user });
        }
     }
});
app.controller('ctrl', function($scope, $location, UserService) {
   $scope.onSubmit = function(user) {
       if ($scope.userForm.$valid) {
            UserService.addUser(user).success(function() {                    
                $location.path('/user/addSuccessful')
            });
       }
   }
});

#3


0  

To submit the Angular Js form ONLY after Angular validations are through or passed, you must use reviewForm.$valid flag chack in ng-submit.

若要仅在通过或通过角验证后提交角Js表单,必须使用reviewForm。$有效旗帜chack在ng-submit。

With this flag check, the form will not get submitted until all validations are passed.

使用此标志检查,在通过所有验证之前不会提交表单。

<form name="reviewForm" ng-controller="reviewCtrl" ng-submit="reviewForm.$valid && reviewCtrl.addReview(product)" novalidate>
....
....
</form>