2016-12-07 18 views
1

フォームの値に加えて、私はPHPスクリプトに渡す追加データが必要です - 私は選択された文字列化された項目を返すjavascript関数 "cart.items()"を持っていますカート。この値をPHP関数に渡すにはどうすればよいですか?このようなHTTP呼び出しを使用するようにするにはangle js json.stringify php

  <div ng-controller="formCtrl"> 

      <input type="text" ng-model="name" class="input-medium search-query" placeholder="Name" required > 
      <input type="email" ng-model="email" class="input-medium search-query" placeholder="Email" required > 
      <input type="text" ng-model="message" class="input-medium search-query" placeholder="Message" required > 
      <button type="submit" class="btn" ng-click="formsubmit(userForm.$valid)" ng-disabled="userForm.$invalid">Submit </button> 
      </form> 

App.js

controller("formCtrl", ['$scope', '$http', function ($scope, $http) { 
    $scope.url = 'http://localhost/ShoppingCart/ShoppingCart/email.php'; 
    $scope.formsubmit = function (isValid) { 

     if (isValid) { 

      $http.post($scope.url, { 'name': $scope.name, "email": $scope.email, "message": $scope.message, "items": $scope.items }). 
        success(function (data, status) { 
         //       console.log(data); 
         $scope.items = "this is scope"; 
         $scope.status = status; 
         $scope.data = data; 
         $scope.result = data; 
         //window.location.assign("email.php"); 
         //alert($scope.items); 
        }) 
     } else { 

      alert('Form is not valid'); 
     } 

のindex.php

<?php 
$post_date = file_get_contents("php://input"); 
$echo("I want the result of the cart.items() function here"); 
?> 
+0

私が正しく理解していれば、PHPにjavascript変数を割り当てようとしていますが、これを見る必要がありますhttp://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php/ 1917626#1917626 –

答えて

0

form.html

。あなたのajaxコールに応じて変更してください

var requestSignin = { 
     method: 'POST', 
     url: your url, 
     headers: { 
      "Content-Type" : "text/plain" 
     }, 
     data: { 
    field 1: $scope.signinData.email, 
    field 2: $scope.signinData.password, 
     . 
     . 
    field n . 
     }, 
     timeout: 30000 
    }; 

    $http(requestSignin) 
    .success(function(data) { console.log(data); 
}).errror(){ 
} 

あなたのフィールドをデータオブジェクトで送信してください。

あなたのPHPコードはこのようにする必要があります。

$ post_date = json_decode(file_get_contents( "php:// input")));

リクエストから入力を取得する。


PHPの使用json_encode()

例から戻るとき:

エコーjson_encode($都市)。