重新访问页面时恢复单选按钮选择

发布于 2024-12-27 02:17:14 字数 2737 浏览 1 评论 0原文

我有一个 PHP 脚本,允许用户通过选择与学生成绩相对应的单选按钮来输入成绩。它允许他们在最终提交之前查看所选成绩。我还希望页面能够返回到选择页面并记住所选择的单选按钮,以便用户在返回时不必再次设置所有这些按钮。这是我到目前为止编写的代码,它将用户带回选择页面,但不会恢复单选按钮选择。

<?php

session_start();

$script_name = $_SERVER["PHP_SELF"];

if(!isset($_SESSION["course"]) || !isset($_SESSION["course"])) {
    $_SESSION["course"] = $_POST["coursename"];
    $_SESSION["section"] = $_POST["section"];
}

if(($_SESSION["authenticated"] == true || isset($_POST["back"])) && !isset($_POST["continue"])) {

    $course = $_SESSION["course"];
    $section = $_SESSION["section"];
    $file_name = $course.$section.".txt";
    $_SESSION["filename"] = $file_name;

    // Open file containing student names.
    $fp = fopen($_SESSION["filename"], "r") or die("Could not open file");
    $students = array();
    $i = 0;

    echo "<h2>Grades Submission Form</h2>";
    echo "<h2>Course: $course, Section: $section</h2>";
    echo "<form action=\"$script_name\" method='post'>";
    echo "<table border='1'>";
    while (!feof($fp)) {
        $line = trim(fgets($fp));
        $students[$i++] = $line;

        echo "<tr><td>$line</td>";
        echo "<td><input type='radio' name=\"$line\" value='A'/>A</td>";
        echo "<td><input type='radio' name=\"$line\" value='B'/>B</td>";
        echo "<td><input type='radio' name=\"$line\" value='C'/>C</td>";
        echo "<td><input type='radio' name=\"$line\" value='D'/>D</td>";
        echo "<td><input type='radio' name=\"$line\" value='F'/>F</td>";
        echo "</tr>";
    }
    echo "</table><br>";
    echo "<input type='submit' name='continue'/>";
    echo "</form>";

} elseif($_SESSION["authenticated"] == true && isset($_POST["continue"]) && !isset($_POST["back"])) {

    unset($_POST["continue"]);
    $keys = array_keys($_POST);
    $values = array_values($_POST);

    echo "<h2>Grades to Submit</h2>";
    echo "<table border='1'>";
    echo "<tr><th>Name</th><th>Grade</th></tr>";
    for($i = 0; $i < count($keys); $i++) {
        echo "<tr><td>{$keys[$i]}</td><td>{$values[$i]}</td></tr>";
    }
    echo "</table><br>";

    echo "<form action='confirmation.php' method='post'>";
    echo "<input type='submit' value='Submit Grades'/>";
    echo "</form>";

    echo "<form action=\"$script_name\" method='post'>";
    echo "<input type='submit' value='Back'/>";
    echo "</form>";

} else {
    header("Location: main.php");
}
?>

I have a PHP script that allows users to enter grades by selecting a radio button that corresponds to the students grade. It allows them to view the selected grades before they can be finally submitted. I also want the page to have the ability to go back to the selection page and remember the radio buttons that were selected so that the user doesn't have to set all of them again when going back. Here is what I have coded so far, it takes the user back to selection page but doesn't restore the radio button selection.

<?php

session_start();

$script_name = $_SERVER["PHP_SELF"];

if(!isset($_SESSION["course"]) || !isset($_SESSION["course"])) {
    $_SESSION["course"] = $_POST["coursename"];
    $_SESSION["section"] = $_POST["section"];
}

if(($_SESSION["authenticated"] == true || isset($_POST["back"])) && !isset($_POST["continue"])) {

    $course = $_SESSION["course"];
    $section = $_SESSION["section"];
    $file_name = $course.$section.".txt";
    $_SESSION["filename"] = $file_name;

    // Open file containing student names.
    $fp = fopen($_SESSION["filename"], "r") or die("Could not open file");
    $students = array();
    $i = 0;

    echo "<h2>Grades Submission Form</h2>";
    echo "<h2>Course: $course, Section: $section</h2>";
    echo "<form action=\"$script_name\" method='post'>";
    echo "<table border='1'>";
    while (!feof($fp)) {
        $line = trim(fgets($fp));
        $students[$i++] = $line;

        echo "<tr><td>$line</td>";
        echo "<td><input type='radio' name=\"$line\" value='A'/>A</td>";
        echo "<td><input type='radio' name=\"$line\" value='B'/>B</td>";
        echo "<td><input type='radio' name=\"$line\" value='C'/>C</td>";
        echo "<td><input type='radio' name=\"$line\" value='D'/>D</td>";
        echo "<td><input type='radio' name=\"$line\" value='F'/>F</td>";
        echo "</tr>";
    }
    echo "</table><br>";
    echo "<input type='submit' name='continue'/>";
    echo "</form>";

} elseif($_SESSION["authenticated"] == true && isset($_POST["continue"]) && !isset($_POST["back"])) {

    unset($_POST["continue"]);
    $keys = array_keys($_POST);
    $values = array_values($_POST);

    echo "<h2>Grades to Submit</h2>";
    echo "<table border='1'>";
    echo "<tr><th>Name</th><th>Grade</th></tr>";
    for($i = 0; $i < count($keys); $i++) {
        echo "<tr><td>{$keys[$i]}</td><td>{$values[$i]}</td></tr>";
    }
    echo "</table><br>";

    echo "<form action='confirmation.php' method='post'>";
    echo "<input type='submit' value='Submit Grades'/>";
    echo "</form>";

    echo "<form action=\"$script_name\" method='post'>";
    echo "<input type='submit' value='Back'/>";
    echo "</form>";

} else {
    header("Location: main.php");
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2025-01-03 02:17:14

您可以 serialize() 包含单选按钮状态的数组并将其存储在您的会议。当你返回时,你所要做的就是将其反序列化并重新设置数据。

You could serialize() an array containing the radio button states and store it in your session. When you go back, all you have to do is unserialize it and set the data again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文