Home  >  Q&A  >  body text

How to pass the value of a generated button from one PHP file to another?

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粉155832941P粉155832941424 days ago552

reply all(1)I'll reply

  • P粉043432210

    P粉0434322102023-09-15 09:16:36

    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;
    ?>

    reply
    0
  • Cancelreply