Change value of session variable by clicking href

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sunnyboy@europe.com

    #1

    Change value of session variable by clicking href

    Hi

    Like to change the value of a session variable by clicking a link

    ------------------------------
    <?php
    session_start() ;
    session_registe r("cat");
    ?>
    <a href="index.php " onMousedown=<? $_SESSION['cat']=1; ?>Set cat to
    1</a>
    <a href="index.php " onMousedown=<? $_SESSION['cat']=2; ?>Set cat to
    1</a>
    -----------------------------

    Any ideas why this is not working and how I could do it?

    Cheers
    Sunnyboy

  • Janwillem Borleffs

    #2
    Re: Change value of session variable by clicking href

    sunnyboy@europe .com wrote:[color=blue]
    > Any ideas why this is not working and how I could do it?
    >[/color]

    You are making the mistake to mix client side (javascript) and server side
    (PHP). Consider the following revision:

    <?php
    session_start() ;

    if (isset($_GET['cat'])) {
    $_SESSION['cat'] = $_GET['cat'];
    }
    print isset($_SESSION['cat']) ? "cat is {$_SESSION['cat']}" : 'cat is not
    set<br />';
    ?>
    <a href="index.php ?cat=1">Set cat to1</a>
    <a href="index.php ?cat=2">Set cat to 2</a>


    JW



    Comment

    • Drakazz

      #3
      Re: Change value of session variable by clicking href

      Hmm, what you are trying to do is to control php directly through
      javascript. it doesn't work that way...
      Here is an example that would work:

      <?php
      session_start() ;
      if ( isset($_GET['cat']) ) { $_SESSION['cat'] = $_GET['cat'];
      }
      else {
      $_SESSION['cat']=0;
      }
      ?>
      <a href="index.php ?cat=1">et cat to 1</a>
      <a href="index.php ?cat=2">et cat to 2</a>
      Cat is : <?php echo $_SESSION['cat']; ?>

      Comment

      Working...