duterte
duterte

Reputation: 39

Php session needs to be refresh to get the latest session value from angularjs

I created a session in php and the value of that session is from angularjs. I created a function that has a parameter. And every time I click on a button, the function will execute and the parameter of the function will send to php. What my problem is when I click the first button my php page will display the value of 18 and it is correct. Now when I go back to the buttons and click another then redirect to php page it doesn't echo the new value which is 20 and it still echo the last value which is 18.

$scope.sendval = function(id){
    $http.post('page',{
        'param': id
     }).then(function(response){
         //some codes...
     });
};

page.php

session_start();
$data = json_decode(file_get_contents("php://input"));
$_SESSION['param'] = $data->param;

Display session

session_start();

echo $_SESSION['param'];

My problem is the value of the session remains the same. But when I refresh page, it updates. How to do it without page refresh? TIA!

What I've already tried and still not working:

session_destroy($_SESSION['param']);
// and
unset($_SESSION['param']);
$_SESSION['param'];

Upvotes: 0

Views: 144

Answers (1)

nairda29
nairda29

Reputation: 25

You must have placed the unset in the wrong place.

<?php 
session_start();
$data = json_decode(file_get_contents("php://input"));
if (isset($_SESSION['param'])) {
    unset($_SESSION['param']);
    $_SESSION['param'] = $data->param; 
}
else{
    $_SESSION['param'] = $data->param; 
}
?>

Upvotes: 0

Related Questions