简体   繁体   中英

angular js json.stringify to php

Along with the form values, I need additional data to be passed to the PHP script- I have a javascript function "cart.items()" which returns the stringified items that have been selected in the cart. How do I pass this value to the PHP function?

form.html

            <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");
?>

To make http call use like this. Change according to your ajax call

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(){
}

Send your fields in data object.

And your php code should be like this.

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

To get the input from request.

while returning from php use json_encode()

Ex:

echo json_encode($cities);

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