简体   繁体   中英

Angular -multi step form

How I can set val1 to $_POST variable ? Because in step 2 val1 is null.

I try to use $scope, $rootScope, angular.copy() and .val().

This is my html code:

<html ng-app="myApp"><body>
  <form action="url.php" method="POST" ng-controller="FormController as vmForm">
  <div ng-switch="vmForm.step">
  <div class="stepone" ng-switch-when="one">

    <label for="val1">Val1</label>
    <input type="text" name="val1" ng-model="val1">

  <button type="button" ng-click="vmForm.stepTwo()"></button>
  </div>

  <div class="steptwo" ng-switch-when="two">
  <label for="val2">Val2</label>
  <input type="text" name="val2" ng-model="val2">

  <input type="submit" value="Submit">
  </body>

JS

<script>
 angular.module('myApp', ['ngAnimate'])
 .controller('FormController', FormController);

  function FormController($scope) {
  var vm = this;
  vm.step = "one";
  vm.stepTwo = stepTwo;

  function stepTwo() {
    vm.step = "two";
}
}</script>

$_POST is a PHP variable that's accessible on the server. It contains the payload or request body that's sent in a HTTP POST request as an associative array. It cannot be set directly using Javascript / AngularJS.

What you can do is construct your request data and make a $http POST request to your form action endpoint. Here's a working example based on the code you posted: http://plnkr.co/edit/C1FGvoDrmzYEFMCwymIG?p=preview

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script src="script.js"></script>
  </head>
  <body ng-app="myApp">
    <h1>Hello Plunker!</h1>
    <form ng-submit="formSubmit()" ng-controller="FormController">
      <p>Val1: {{val1}}</p>
      <p>Val2: {{val2}}</p>
      <div ng-switch="vm.step">
        <div class="stepone" ng-switch-when="one">
          <label for="val1">Val1</label>
          <input type="text" name="val1" ng-model="$parent.val1">
          <button type="button" ng-click="stepTwo()">Go to Step two</button>
        </div>

        <div class="steptwo" ng-switch-when="two">
          <label for="val2">Val2</label>
          <input type="text" name="val2" ng-model="$parent.val2">
          <input type="submit" value="Submit">
        </div>
      </div>
    </form>
  </body>
</html>

script.js

var app = angular.module('myApp', []);

app.controller('FormController', ['$scope','$http',function ($scope,$http) {
    $scope.vm = {
      step : "one"
    };
    $scope.val1 = "";
    $scope.val2 = "";

    $scope.stepTwo = function () {
      $scope.vm.step = "two";
    }

    $scope.formSubmit = function () {
      console.log($scope.val1, $scope.val2);
      var req = {
        url : 'url.php',
        method: 'POST',
        data : {
          val1 : $scope.val1,
          val2 : $scope.val2
        }
      };

      $http(req).then(function(response) {
        console.log('success', response);
      }, function(errorResponse){
        console.log('error', errorResponse);
      });
    }
}]);

Just leave everything as is in your controller and tweak your HTML like this:

<form action="url.php" method="POST" ng-controller="FormController as vmForm">
  <div ng-switch="vmForm.step">
    <div class="stepone" ng-switch-when="one">
      <label for="val1">Val1</label>
      <input type="text" name="val1" ng-model="vmForm.val1">
      <button type="button" ng-click="vmForm.stepTwo()">Goto Step 2</button>
    </div>
    <div class="steptwo" ng-switch-when="two">
      <input type="hidden" name="val1" value="{{vmForm.val1}}">
      <label for="val2">Val2</label>
      <input type="text" name="val2" ng-model="val2">
      <input type="submit" value="Submit">
    </div>
  </div>
</form>

The tweak to your code is simply raising the scope of "val1" to "vmForm.val1" so in Step 2 "vmForm.val1" can be assigned to a hidden input field for posting.

Here's the form fields being posted in the browsers debugger:

在此处输入图片说明

Here's a Plunker, http://plnkr.co/edit/3VaFMjZuH09A4dIr1afg?p=preview

Open your browsers debugger and view network traffic to see form fields being posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM