How to pass the value of a generated button from one PHP file to another?
P粉155832941
P粉155832941 2023-09-14 21:00:35
0
1
567

I want to pass the id value of the PHP generated button from the theory.php file to the theory1.php file. code show as below:

//theory.php file
require('components/db.php');

$query = "SELECT * FROM `courses`";
$result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$numrows = mysqli_num_rows($result);

for ($i = 0; $i < $numrows; $i++) {

    $query = "SELECT * FROM `courses` WHERE courseID = '$i'";
    $result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
    $rowQuery = mysqli_fetch_assoc($result);

    $_SESSION['course_ID'] = $i;

    echo '
    <div class="card">
            <img class = "cardImage" src="';
    echo $rowQuery['imageLink'];
    echo '" alt="Course 1">
            <h3>';
    echo $rowQuery['courseName'];
    echo '</h3>
            <p>';
    echo $rowQuery['courseTextOne'];
    echo '</p>
            <a href="theory1.php?course_ID=$i" class="button">Proceed</a>
        </div>'; //a - is a button which needs to have an ID to pass to theory1.php
}

This code generates a card with a button. I want each button to store the corresponding ID of the course in the MySQL database. This ID needs to be passed to another page based on the button (card) clicked so that the correct data can be retrieved from the database in the future.

P粉155832941
P粉155832941

reply all(1)
P粉043432210

solution

Theory.php:

require('components/db.php');
$query = "SELECT * FROM `courses`";
$result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$numrows = mysqli_num_rows($result);

for ($i = 0; $i < $numrows; $i++) {

    $query = "SELECT * FROM `courses` WHERE courseID = '$i'";
    $result   = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
    $rowQuery = mysqli_fetch_assoc($result);

    $_SESSION['course_ID'] = $i;

    echo '
    <div class="card">
            <img class = "cardImage" src="';
    echo $rowQuery['imageLink'];
    echo '" alt="Course 1">
            <h3>';
    echo $rowQuery['courseName'];
    echo '</h3>
            <p>';
    echo $rowQuery['courseTextOne'];
    echo '</p>
            <a href="theory1.php?courseID=';
    echo $i;
    echo '"class="button">Перейти</a>
        </div>'; //a - is a button which needs to have an ID to pass to theory1.php
}

theory1.php:

<?php
$courseID = $_GET['courseID'];
echo $courseID;
?>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template