我有一个基本表单,试图验证是否选中了一个复选框,此时它不允许它通过(预期)但是我的错误信息没有显示在同一页面上,它可能是非常小的东西不见了。
它没有显示错误,可以在下面的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>
答案 0 :(得分:1)
您的错误未显示的原因是因为您已经在PHP中执行回显并拥有PHP标记。
echo '<span class="error"><?php echo $error;?></span>';
您可以这样做:(转义CSS类名称的双引号)
echo "<span class=\"error\">$error</span>";
或连接$error
变量:
echo '<span class="error">'.$error.'</span>';
另外,除非连接,否则变量不会在单引号内解析。