-2

In the header ( header.phtml ) I start the session :

<?php
if (session_status() == PHP_SESSION_NONE)
    session_start();
?>
<!DOCTYPE html>
<html>
<head>
...

Now in other page I want to set a session value :

<script type="text/javascript">

    $(document).ready(function() {

        $("#add_reservS").on("click", function(e) {
            <?php
            $_SESSION['nb_ajout_client'] = 0;
            ?>
        });
    ...

Then I increment this session value in other page :

public function ajouterExecAction($src = '', $pk_src = ''){
        $client = new Client();
        $client->ajouter($_POST);
        if ($src == '')
            $this->response->redirect('ReferentielClient');
        else {
            $_SESSION['nb_ajout_client']++;
            ...
        }
        ...
}

Now inside another page I want to get this session value :

<script type="text/javascript">

    $(document).ready(function() {
        $("#btn_retour").on("click", function() {
            var back = "<?php echo $previous; ?>";
            if (back == "list_reserv")
                window.history.back();
            else {
                var nb_back = "<?php echo $_SESSION['nb_ajout_client']; ?>";
                alert("session nb ajout client = "+nb_back); // nb_back is blank !
            }
        });

    });

</script>

The alert show a blank value ! So what is wrong in my approach ?

pheromix
  • 18,213
  • 29
  • 88
  • 158
  • do you have the session_start(); on the other pages – swidmann Sep 18 '15 at 11:22
  • no , just in the header page :) – pheromix Sep 18 '15 at 11:22
  • Remember that session_start() must be the VERY FIRST THING IN YOUR DOCUMENT – DTH Sep 18 '15 at 11:22
  • To define a variable with the value of session - ` = 0;` – Sougata Bose Sep 18 '15 at 11:23
  • Can you explain what you expect to happen in your second snippet? Assuming you understand that PHP is processed first, once the page is called and then, **only then**, your HTML/CSS/JS will come at stage to play their part. – al'ein Sep 18 '15 at 11:24
  • http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – John Conde Sep 18 '15 at 11:24
  • You do realize that your ` $("#add_reservS")` onClick function won't trigger the php? The SESSION will be set the second the page is loaded. – Epodax Sep 18 '15 at 11:24
  • you cannot run PHP along with the Javascript to like in your second snippets. PHP is a server side language while Javascript is used in the client side here. – Chatura Dilan Sep 18 '15 at 11:24
  • @AlanMachado I want to initialize the variable when user clicks the `add` button. – pheromix Sep 18 '15 at 11:26
  • 1
    @pheromix - you could achive that if you send an ajax request to the page and have a suitable piece of code to process the ajax request and thus set the session var – Professor Abronsius Sep 18 '15 at 11:27

1 Answers1

1

You must start each document with session_start();. While it is no longer required to be the very first line, it does need to be present before you do anything with a session in the file.

It is common to start the session from a common include file which is then included in every page which needs the session. Thus you only need to write it once.

William_Wilson
  • 1,244
  • 7
  • 16