首页  >  文章  >  数据库  >  为什么我的 `if(isset($_POST['submit']))` 没有隐藏页面加载时的回显和表格?

为什么我的 `if(isset($_POST['submit']))` 没有隐藏页面加载时的回显和表格?

Susan Sarandon
Susan Sarandon原创
2024-11-06 20:17:03328浏览

Why is my `if(isset($_POST['submit']))` Not Hiding the Echo and Table on Page Load?

为什么 if(isset($_POST['submit'])) 无法在页面加载时隐藏回显和表格

问题描述

if(isset($_POST['submit'])) 语句旨在在最初打开脚本时隐藏指定的回显和表格。然而,在实现此代码后,用户报告即使单击提交按钮后,回显和表格仍然可见。

解决方案

出现问题的原因是提交按钮没有分配唯一的提供的代码中的名称。为了解决这个问题,应该给按钮一个名称,如下面更正的代码所示:

<p><input type="submit" value="Submit" name="submit" /></p>

通过此修改, if(isset($_POST['submit'])) 语句将正确检测何时单击提交按钮并执行预期操作以隐藏所需元素。

此外,原始代码缺少连接到数据库的数据库连接代码。为了更好的理解,更正后的完整代码如下:

<?php
$username = "xxx";
$password = "xxx";
$database = "mobile_app";

mysql_connect('localhost', $username, $password);

@mysql_select_db($database) or die("Unable to select database");

$sessionid = isset($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset($_POST['grade']) ? $_POST['grade'] : "";
$orderfield = isset($_POST['order']) ? $_POST['order'] : "";

$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);

switch ($orderfield) {
    case 'ordersessionid':
        $orderfield = 'gr.SessionId';
        break;
    case 'ordermoduleid':
        $orderfield = 'm.ModuleId';
        break;
    case 'orderteacherid':
        $orderfield = 's.TeacherId';
        break;
    case 'orderstudentid':
        $orderfield = 'gr.StudentId';
        break;
    case 'ordergrade':
        $orderfield = 'gr.Grade';
        break;
}

$ordertable = $orderfield;

$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade') ORDER BY $ordertable ASC");

$num = mysql_numrows($result);

if (isset($_POST['submit'])) {
    echo "<p>Your Search: <strong>Session ID:</strong> ";
    if (empty($sessionid)) echo "'All Sessions'";
    else echo "'$sessionid'";
    echo ", <strong>Module ID:</strong> ";
    if (empty($moduleid)) echo "'All Modules'";
    else echo "'$moduleid'";
    echo ", <strong>Teacher Username:</strong> ";
    if (empty($teacherid)) echo "'All Teachers'";
    else echo "'$teacherid'";
    echo ", <strong>Student Username:</strong> ";
    if (empty($studentid)) echo "'All Students'";
    else echo "'$studentid'";
    echo ", <strong>Grade:</strong> ";
    if (empty($grade)) echo "'All Grades'";
    else echo "'$grade'";
    "</p>";

    echo "<p>Number of Records Shown in Result of the Search: <strong>$num</strong></p>";

    echo "<table border='1'>";
    echo "<tr>";
    echo "<th>Student Id</th>";
    echo "<th>Forename</th>";
    echo "<th>Session Id</th>";
    echo "<th>Grade</th>";
    echo "<th>Mark</th>";
    echo "<th>Module</th>";
    echo "<th>Teacher</th>";
    echo "</tr>";
    while ($row = mysql_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['StudentId'] . "</td>";
        echo "<td>" . $row['Forename'] . "</td>";
        echo "<td>" . $row['SessionId'] . "</td>";
        echo "<td>" . $row['Grade'] . "</td>";
        echo "<td>" . $row['Mark'] . "</td>";
        echo "<td>" . $row['ModuleName'] . "</td>";
        echo "<td>" . $row['TeacherId'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

mysql_close();
?>

以上是为什么我的 `if(isset($_POST['submit']))` 没有隐藏页面加载时的回显和表格?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn