0

I hope this isn't stupidly simple. I am completely new to web dev.

I have list items that I styled as buttons. I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable. How do I accomplish this/ is there a better way to accomplish the same thing?

sawmilld
  • 13
  • 4
  • You can use ajax to post to some script with the session variable you want, or store it on page load using `$_SESSION['variable'] = "some page";` or make it an array and adding items into it that way. – Kaboom Mar 09 '17 at 22:57
  • I thought about doing it with AJAX because I dont want to have to create new pages for each of the links. They all lead to the same page but I just want to store which list item was clicked. Im currently opening the new page with onclick. can I do both this and update session info with onclick (probably super obvious answer but im learning as I do my project) – sawmilld Mar 10 '17 at 05:36
  • If i understood your comment correctly, Upon using AJAX each time you will update a div with content? if your goal is just find which item was clicked you can use simple JS snippet, Pseudo code for that would be like this, For loop All anchor on the page, upon clicking of that anchor index run some function with returning "this" value. Now you have that click anchor identity. All you need is to pass variable from JS to php, please check on internet how you can pass JS variable to php, eg http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable – Danish Mar 10 '17 at 07:03
  • That's not exactly what I'm trying to do. I basically just have list elements with borders that all link to the same page (eg nextpage.html) when clicked. I just want to be able to store which element (eg list item 1 or list item 2) was clicked while also linking to new page. I guess my main problem is what is the code to both link to a new page when i click a list element and take care of ajax? can i do this all in the onclick method? – sawmilld Mar 10 '17 at 07:46

1 Answers1

0

Sessions Step By Step

1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this

<?php
session_start();
?>

2 - Set your session inside a page and then you have access in that page. For example this is page 1.php

<?php
   //This is page 1 and then we will use session that defined from this page:
    session_start();
    $_SESSION['danish']='danish';
?>

3- Using and Getting session in 2.php

<?php

//In this page I am going to use session:

  session_start();
  if($_SESSION['name']){
  echo 'Your name variable Is Here!  :) ';
  }
 ?>

In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this

First Page

<?php
   session_start();
   $_SESSION['myvar']='myvalue';
?>

Second page

<?php
   session_start();
   echo $_SESSION['myvar'];
 ?>
Danish
  • 1,467
  • 19
  • 28