将angularjs值传递给PHP变量

时间:2015-07-26 13:00:10

标签: php angularjs

我从使用ngStorage的angularjs开始。我可以成功保存和显示数据。

我像以前一样显示价值

{{myobj.session}}

我想将任何存储的值传递给php变量。下面显示的是我想象的逻辑,我知道这不会起作用。我的问题是如何以正确的方式将这样的值分配给PHP变量?

<?php 
    $name = {{myobj.session}}
?>

3 个答案:

答案 0 :(得分:7)

您可以使用此角度变量:students.tiffen_type
PHP变量:$value

<?php
    $value = "{{ students.tiffen_type }}";
?>

答案 1 :(得分:3)

你可以这样做一个ajax请求:

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

在后端你可以得到像这样的变量

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

从那里你可以用变量做你想做的一切,但我还不确定你想要达到的目的。

答案 2 :(得分:0)

你不能直接将angularjs值赋值给php变量,使用下面的方法它会帮助你

     $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;
?>