March 10, 2013

Get only one array from the $_SESSION variable

Question by Nadia Shiwdin

I have an array that stores in a session variable. However I want to send only one part of the array in the SESSION variable to another page depending on which button is clicked.

The code for the button

foreach($name as  $bookname => $name){
        echo "<div class='control-group' align='center'><h4>$bookname</h4><a href='/handler' class='btn btn-large btn-block btn-primary' type='button'>follow $name</a></ul></div>";    
        $_SESSION['name'] = $name;
        $_SESSION['bookname'] = $bookname
        print_r($_SESSION['name']);

        }

The print_r gives me each in the array, what I want to do is when the button is clicked only the $name for that button is set as the $_SESSION. The way I have it now it sets the SESSION for only the last part of the array.

Answer by Starx

It works for only last part, because of the loop overrides every preceding value.

If you want to set the value in SESSION after it is clicked, you have to send the request to a different page (HTTP GET or POST request) or an AJAX request to set the values.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!