1

I want to send article id using session, Source page contains many links to open one webpage but id is there in ID attribute of a href. like

<br/>
< a id="13" class="openpage" href="test.html">open test form< /a><br />

I have used javascript to pass the variable

$(".openpage").on('click', function() {
    var artid = $(this).attr("id");
    <?php $_SESSION['artid'] = "<script>document.write(artid)</script>"?>
    });


var artid is having the correct value before loading the new page and $_SESSION['artid'] is not getting the value of artid.

Azhar A
  • 11
  • 5

1 Answers1

0

You'll have to update your index file and the test.html becomes test.php

jQuery part of the code takes the value of your ID attribute on the link Stores it in a cookie.

In your test.php The value of the COOKIE is assigned to your SESSION and the COOKIE is cleared using the in-built php set_cookie() function to set the cookie with an empty value (For security reasons);

The cookie expires in 1 minute. you can update it if you like.

-

//index.php file

<a id="13" class="openpage" href="test.php" >open test form</a>

<!-- JS HERE ---> 

<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){

 function setCookie(key, value) {
            var expires = new Date();
            expires.setTime(expires.getTime() + (60 * 1000));
            document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();

}

  $(".openpage").on('click', function() {
      var artid = $('.openpage').attr("id");
        setCookie('cookie_session', artid);
});

});
</script>

//test.php file

 <?php
      if(isset($_COOKIE['cookie_session'])):
      $_SESSION['artid'] =  $_COOKIE['cookie_session'];
      setcookie("cookie_session", '');
      echo $_SESSION['artid'];
      endif;
   ?>

Here's a screenshot of the COOKIE in the browser's Dev tool > Application > Cookies

enter image description here

As you can see the COOKIE is no more in your browser and the SESSION now holds the data.

Hope this helps :)

Oluwaseye
  • 685
  • 8
  • 20