Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dynamic button, when clicked (Course Outline), retrieves mysql db record ID and redirects to a page.

I want it to fetch all data associated the clicked ID and display on same page though different course outlines.
which is not working.

What I have tried:

//button_clicked.php:
<?php 
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
include('connect.php');

// Fetch course data from database
$query = "SELECT id, btn_label, title FROM course";
$result = mysqli_query($conn, $query);
// Generate buttons with course IDs as identifiers
while ($row = mysqli_fetch_assoc($result)) {
    $courseID = $row['id'];
    $courseName = $row['btn_label'];
 
    echo "<button class='courseButton' onclick='redirectToCourse($courseID)'>$courseName</button>";

}
mysqli_free_result($result);

?>
<script src="bttn_clicked.js"></script>
</body>
</html>

//button_clicked.js

function redirectToCourse(courseID) {
    window.location.href = "course_display.php?id=" + courseID;
 }
Posted
Comments
Chris Copeland 19-Dec-23 5:03am    
Well it seems you've already built the code to redirect to a page with the course ID present, so what's stopping you from building the rest? You have $_GET['id'] containing the course ID.
Ojazy Moi 19-Dec-23 7:40am    
How can fetch all DB records associated to this ID and display on the new page?

1 solution

Your best and safest way is to make use of your session that you already declared in your 'button_clicked.php' page -
Quote:
Session variables works by storing user information to be used across multiple pages (e.g. username, course names, etc). By default, session variables last until the user closes the browser or the session is closed.


To set your variables -
PHP
<?php
//Set session variables...
$_SESSION["courseID"] = $courseID;
$_SESSION["courseName"] = $courseName;
?>


To use these values in your new page 'course_display.php' -
PHP
<?php
session_start(); // Always start the page with 'session_start()...

//Echo session variables that were set on your previous page...
echo "User ID is - " . $_SESSION["courseID"] . ".<br>";
echo "Course Name is - " . $_SESSION["courseName"] . ".";
?>


You can use these values on any page for as long as the session is open and valid.

To view all of the variables contained in a session -
PHP
<?php
print_r($_SESSION);
?>


ALWAYS destroy your session when you are done with it -
PHP
<?php
session_start();

//Remove all session variables...
session_unset();

//Destroy the session...
session_destroy();
?>


This is teh basics of session handling, there are much more to it which you will find by searching Google, it will however point yuou in the right direction. Also read more on this -
PHP Sessions[^]
 
Share this answer
 
Comments
Ojazy Moi 20-Dec-23 8:25am    
@Andre, I still can't get the DB ID which is passed as parameter to URL to get only records of same ID.
All am getting is the Session ID
Andre Oosthuizen 20-Dec-23 16:39pm    
If you are getting the session ID you are good to go, just use it in your 'redirectToCourse(courseID)' function.
Ojazy Moi 22-Dec-23 2:52am    
With this code, it's displaying DB ID in url when redirected to new page but echoing Only same DB ID for every course clicked. Please let me elaborate on what i wanted to achieve: I have slides echoing all uploaded courses which includes course ID, btn_label, title, description,price and image.The buttons for ENROL below the modal generated dynamically with Course ID used as element ID. All good, but i want two results to be achieved on button clicked: 1- I want it to select from course DB table and compare with HTML element ID of separate course, then redirect to new page. 2- In that new page, let it select(fetch) only record of the clicked course ID.

Please if there's a good way to do this, feel free to recommend. Thanks

I wa

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900