错误消息未使用php显示html表单


Error message not displaying html forms using php

我有一个基本表单,试图验证是否选中了一个复选框,目前它不允许它通过(预期),但我的错误消息没有显示在同一页面上,可能是缺少了一些非常小的东西。

它没有显示错误,可以在下面的html中看到一个span类。

问题出在哪里?

php:

     <?php
    require_once 'db/connect.php';
    $error='';  
    $success='';
        if (isset($_POST['submit'])){   
            if ( (isset($_POST['checkbox'])) && (isset($_POST['competitorDelete'])) ) {
                $checkbox = $_POST['checkbox'];
                for ($i=0;$i<count($checkbox);$i++) {
                    $delete_comp = $checkbox[$i];
                    $query = $con->query("SELECT Forename, Surname FROM student WHERE Student_ID = '" . $delete_comp . "'");
                    $row = mysqli_fetch_assoc($query);
                    $success = $row['Forename'] . ' ' . $row['Surname'] . ' has been deleted as a competitor from any events they were submitted for <br>';             
                    $query= $con->query("DELETE FROM competitors WHERE Student_ID = '" . $delete_comp . "'");
                }
            }   
            elseif (isset($_POST['checkbox']))  {
                $checkbox = $_POST['checkbox'];
                for ($i=0;$i<count($checkbox);$i++) { 
                    $delete_student = $checkbox[$i];
                    $query = $con->query("SELECT Forename, Surname FROM student WHERE Student_ID = '" . $delete_student . "'");
                    $row = mysqli_fetch_assoc($query);
                    $success = $row['Forename'] . ' ' . $row['Surname'] . ' has been deleted as a student <br>';                    
                    $query= $con->query("DELETE FROM student WHERE Student_ID = '" . $delete_student . "'");
                }
            }
            else {
                $error = 'A student must be selected';
            }
        }   
?>

html:

 <?php  
    session_start();    
    require_once 'db/checkuserloggedin.php';
    include 'db/header.php';
    include 'deletestudent.php'; 

    echo '<h3> Delete students </h3>';                                                                      
        echo "<form method ='"POST'">";
            if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Forename, Surname, Student_ID " .
                                                "FROM student, teacher " .
                                                    "WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {

                if ($student_result->num_rows) {                                                                                                                                        
                    echo '<table>'; 
                    while ($row1 = $student_result->fetch_assoc()) {
                        echo '<tr>';                            
                            echo '<td>';        
                                echo $row1['Forename'] . ' ' . $row1['Surname'];
                            echo '</td>';
                            echo '<td>';        
                                echo '<input type="checkbox" name="checkbox[]" value="' . $row1['Student_ID'] . '">';
                            echo '</td>';       
                        echo '</tr>';       
                    }                       
                    echo '</table>';                    
                }                                                       
            }                       
            echo 'Delete competitor data only<input type="checkbox" name="competitorDelete">' . '<br>';
            echo '<input type="submit" name="submit" value ="Delete">';                                     
            echo '<input type="reset" value ="Reset">';
            echo '<span class="error"><?php echo $error;?></span>';
        echo "</form>"; 
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Entry form</title>
    </head>
    <body>
        <div id="logoutbutton">
        <button class="btn" onclick="location.href='logout.php'">Logout</button>
        </div>
        <link rel="stylesheet" type="text/css" href="styles.css">
    </body>
</html>

错误没有显示的原因是因为您已经在PHP内部执行echo,并且有PHP标记。

echo '<span class="error"><?php echo $error;?></span>';

您可以执行以下操作之一:(转义CSS类名的双引号)

echo "<span class='"error'">$error</span>";

或连接$error变量:

echo '<span class="error">'.$error.'</span>';

另外,除非连接起来,否则变量不会在单引号内解析。