-1

I have a basic form trying to validate that a checkbox is selected, at the moment it is not allowing it to go through (expected) however my error message is not displaying on the same page, it is probably something really small that is missing.

It not displaying the error, a span class can be seen in the html below.

Where's the problem?

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>

1 Answers1

1

The reason why your error is not showing is because you're already inside PHP in doing an echo and have PHP tags.

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

You can either do: (escaping double quotes for the CSS class name)

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

or concatenate the $error variable:

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

Plus, variables do not get parsed inside single quotes, unless concatenated.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141