如何將產生的按鈕的值從一個 PHP 檔案傳遞到另一個 PHP 檔案?
P粉155832941
P粉155832941 2023-09-14 21:00:35
0
1
577

我想將 PHP 產生的按鈕的 id 值從 theory.php 檔案傳遞到 theory1.php 檔案。 程式碼如下:

//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
}

該程式碼產生帶有按鈕的卡片。我希望每個按鈕都儲存 MySQL 資料庫中課程的相應 ID。該 ID 需要根據單擊的按鈕(卡)傳遞到另一個頁面,以便將來可以從資料庫中檢索正確的資料。

P粉155832941
P粉155832941

全部回覆(1)
P粉043432210

解決方案

理論.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;
?>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板