Skip to content
Advertisement

How to use $_SESSION variable to retrieve data and display it in another page [closed]

Below I have a php script where it displays a “Course” drop down menu and a “Module” Drop down menu. What is suppose to happen is the user first selects a course from the “Course” drop down menu and then a list of Modules which belongs to the selected course will appear in the “Modules” drop down menu. Below is the code for this:

create_session.php

JavaScript

So lets say I select the Course INFO101 - Information Communication Technology in the “Course” drop down menu, it displays the following modules in the Module drop down menu below which corresponds with that course:

JavaScript

Now what I want to do is that I want the user to select one of the modules from the drop down menu and then submit the form below to navigate to the QandATable.php page. But I want to know how do I retrieve the module number and name from the drop down menu and display it in the QandATable.php page? I know I should use $_SESSION variable how and where do I write the $_SESSION variables in both pages? Also do I need to use isset so I don’t get an undefined variable?

Below is form which navigates to QandATable.php:

JavaScript

Advertisement

Answer

In answer to your questions. Yes, it is good practice to use an isset or is_null on the variable being posted initially to ensure you have a valid value. In the situation of writing it to a session variable though it would not matter. You can assign a null value to a session variable, but it could end up causing unintended results later if you try to use the session variable expecting a valid value.

To set a session variable you can do the following. You can set this anytime you need to in order to update the variable. I usually assign these on a post page somewhere after collecting the data from the user.

JavaScript

Remember that you also need the following code at the very top of every page that will use a session or could be hit with session variables established. If you do not have this code set on a page and the user visits it, you will lose your session tracking.

JavaScript

To later reference a stored session variable you can do so like this:

JavaScript

Hope I understood your question correctly.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement