5

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

I display value as before

{{myobj.session}}

I would like to pass whatever stored value into php variable. Shown below is my imaginary logic and I know thats not gonna work. My question is how to assign such value into PHP variable in a correct manner?

<?php 
    $name = {{myobj.session}}
?>
4
  • 1
    I believe you need to post it via AJAX Commented Jul 26, 2015 at 13:06
  • As I mentioned above, I am new to this technology. Can you show me how? Commented Jul 26, 2015 at 13:13
  • you can use $http service to pass data Commented Jul 26, 2015 at 13:20
  • Suggest you get a better understand of the difference between server side code and client side (browser) and various methods of communication between the 2 Commented Jul 26, 2015 at 13:25

4 Answers 4

8

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

<?php
    $value = "{{ students.tiffen_type }}";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I used this , and it works, but how ? it should not work at all. Angular is front code where as php is server code. Can you please explain how it works?
It works for me too. But please explain how it is working?
It take the value due to {{ }} in text
3

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.

2 Comments

I learned this tutorial youtube.com/watch?v=g4vLTauq5j8 and everything is fine. But I couldn't assign outcome into PHP variable in php page.
You can't just assign the value to a php variable because once the page is loaded all the php is gone, php is server side code, in order to access / set php variables you have to communicate with the server via ajax requests.
0

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.