Pass angularjs value to PHP variable

前端 未结 4 489
故里飘歌
故里飘歌 2021-01-18 14:06

I am starting with angularjs with ngStorage. I can save and display data successfully.

I display value as before

{{myobj.session}}

相关标签:
4条回答
  • 2021-01-18 14:50

    You can use this angular variable: students.tiffen_type
    PHP variable : $value

    <?php
        $value = "{{ students.tiffen_type }}";
    ?>
    
    0 讨论(0)
  • 2021-01-18 14:57

    send.js

    $http({
        method: "post",
        url: "ajax/request.php",
        data: {
            angular_var: $scope.angular_var // $scope.angular_var is angular variable e.g. ng-model="angular_var"
        }, headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data) {
        console.log(data)
    });
    

    ajax/request.php

    $request_arr = json_decode( file_get_contents('php://input') );
    $angular_var = $request_arr->angular_var;
    
    0 讨论(0)
  • 2021-01-18 15:01

    You can't assign directly angularjs value to php variable, use the following method it will help you

         $http({
          method: "POST", 
          url: "test.php",  
          data: {
    	   data: postvariable 
          }
          }).success(function(response) {
               console.log(response); //get the echo value from php page
          }).error(function(response) {
                console.log(response);
          });

    test.php

    <?php
        $data = json_decode(file_get_contents("php://input"));
        echo $data->data;
    ?>

    0 讨论(0)
  • 2021-01-18 15:06

    You can do a ajax request like this:

    $http({
            url: "urltopost.php",
            method: "POST",
            data: {
                data: variable
            }
        }).success(function(response) {
        console.log(response);
    });
    

    And on the backend you can get the variable like this

    <?php
    $request = json_decode( file_get_contents('php://input') );
    $variable = $request->data
    

    From there you can do everything you want with that variable, still I'm not sure what are you trying to achieve.

    0 讨论(0)
提交回复
热议问题