Greatym.com Greatym.com - 3 months ago 9
Javascript Question

Angular js form submit and send prepared json data to server

I want to prepare data on the form submit event and send it to the server.

I have an form in step1.php file

<form name="DTOstep1" >
<input type="text" name="save_quote_email" id="save_quote_email" />
<br><br>
<input type="submit" value="Save" />
</form>


When I submit this form, I want to call Controller function from app/ctrl/autoQuoteCtrl.js

Please let me know how to make an api call here to send data(O) to the server?

Answer

html

<form role="form" name="form1">
      <div class="form-group">
        <p>Welcome : <span>{{user.mail}}</span></p>
        <label for="exampleInputEmail1">Mail</label>
        <input type="text" placeholder="Enter name"  class="form-control" ng-model="user.mail" required>
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" placeholder="Password" id="exampleInputPassword1" class="form-control" ng-model="user.pass" required>
      </div>
      <button class="btn btn-default" type="submit" ng-click="login(user)" ng-disabled="form1.$invalid">Submit</button>
      <p>{{msgtxt}}</p>
</form>

Controller.js

'use strict';

app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
    $scope.msgtxt='';
    $scope.login=function(data){
        alert(JSON.stringify(user));
    };
}]);
Comments