-2

I am creating a project on the school management system using PHP and I am stuck as it's giving me an error: Cannot modify header information - headers already sent. If the user forgot to fill any field and click on the submit button,the server should return a message like fill this field but instead it's giving this error and I don't know why.

this is the error I am getting this is the error I am getting

this is my form this is my form This is my home page

<?php
session_start();
require_once 'header.php';
if(!isset($_SESSION['name']))
{
    die("Access Denied");
}
if(isset($_POST['logout']))
{
    header('Location: logout.php');
    return;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" 
    integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="../management/dashboard/css/admin.css">
    <link rel="stylesheet" href="../management/student/css/addstudent.css">
    <link rel="stylesheet" href="../management/student/css/allstudent.css">
    <link rel="stylesheet" href="../management/student/css/studentdetail.css">
    </head>
<body>
    <header>
        <nav>
            <div class="logo">
                <div class="img">
                    <img src="./images/logo3.png" alt="">
                </div>
            </div>
            <ul class="nav-links">
                <li id="n-1">
                    <div class="admin">
                        <p>Konark Shivam</p>
                        <i class="fas fa-user-circle"></i>
                    </div>
                </li>
                <li id="n-2">
                    <i class="fas fa-envelope"></i>
                </li>
                <li id="n-3">
                    <i class="fas fa-bell"></i>
                </li>
                <li id="n-4">
                    <i class="fas fa-sign-out-alt"></i>
                </li>
            </ul>
            <div class="burger" id="burger">
                <i class="fas fa-bars"></i>
            </div>
        </nav>
    </header>
    <!-- for logout -->
    <section>
        <div class="logout-box" id="logout-box">
            <div class="logout-content">
                    <p>Do you want to logout</p>
                        <form method="post">
                            <div class="logout-button">
                                <input type="submit" name="logout" value="Logout">
                                <input type="submit" name="cancel" value="Cancel" id="logout-cancel" onclick="return false">
                            </div>
                        </form>
                </div>
        </div>
    </section>
    <menu>
        <div class="left-menu" id="left-menu">
            <ul>
                <li class="menu-items" id="menu-item-1">
                    <a href="#">Dashboard</a>
                    <span class="arrow">></span>
                </li >
                <div class="drop-menu" id="drop-menu-1">
                    <ul>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a id="d-i-admin" href="home.php?page=dashboard/admin">> Admin</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#" id="d-i-teacher">> Teacher</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Students</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Parents</a>
                        </li>
                    </ul>
                </div>

                <li class="menu-items" id="menu-item-2">
                    <span>Student</span>
                    <span class="arrow">></span>
                </li>
                <div class="drop-menu" id="drop-menu-2">
                    <ul>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="home.php?page=student/allstudent" id="s-d-1">> All Students </a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="home.php?page=student/studentdetail">> Student Details</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="home.php?page=student/addstudent">> Admission Form</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Student Promotion</a>
                        </li>
                    </ul>
                </div>

                <li class="menu-items" id="menu-item3">
                    <span>Teacher</span>
                    <span class="arrow">></span>
                </li>
                <div class="drop-menu" id="drop-menu-3">
                    <ul>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Admin</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Teacher</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Students</a>
                        </li>
                        <li class="drop-items"> 
                            <div class="name"></div>
                            <a href="#">> Parents</a>
                        </li>
                    </ul>
                </div>


                <li class="menu-items">
                    <span>Parents</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Library</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Subjects</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Class Routines</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Attendence</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Exams</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Transport</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Hostel</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Notice</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Message</span>
                    <span class="arrow">></span>
                </li>
                <li class="menu-items">
                    <span>Accounts</span>
                    <span class="arrow">></span>
                </li>
            </ul>
        </div>
    </menu>
    <main>
       <div class="presentation">
           <div class="my-container" id="container">
               <?php
                    if(isset($_GET['page']))
                    {
                        $page=$_GET['page'];
                        include_once $page.'.php';
                    }
                    else{
                        include_once 'dashboard/admin.php';
                    }
               ?>
           </div>
       </div>
    </main>
    <script src="script.js"></script>
</body>
</html>

this is my addstudent page

<?php
ob_start();
if(!isset($_SESSION['name']))
{
    die("Access Denied");
}
require_once 'pdo.php';   
if(isset($_POST['submit']))
{
    if(isset($_POST['f_name']) && isset($_POST['l_name']) && isset($_POST['gender']) && isset($_POST['dob']) && isset($_POST['blood'])
    && isset($_POST['religion']) && isset($_POST['email']) && isset($_POST['address']) && isset($_POST['phone']) )
    {
        if(strlen($_POST['f_name'])<1)
        {
            $_SESSION['error']='Please enter first name';   
            header('Location:home.php?page=student/addstudent');
            return;
        }
        if(strlen($_POST['l_name'])<1)
        {
            $_SESSION['error']='Please enter last name';
            header('Location:addstudent.php');
            return;
        }
        if(strlen($_POST['gender'])<1)
        {
            $_SESSION['error']='Please enter gender';
            header('Location:addstudent.php');
            return; 
        }
        if(strlen($_POST['dob'])<1)
        {
            $_SESSION['error']='Please select date';
            header('Location:addstudent.php');
            return; 
        }
        if(strlen($_POST['blood'])<1)
        {
            $_SESSION['error']='Please enter blood group';
            header('Location:addstudent.php');
            return; 
        }
        if(strlen($_POST['religion'])<1)
        {
            $_SESSION['error']='Please enter religion';
            header('Location:addstudent.php');
            return; 
        }
        if(strlen($_POST['email'])<1)
        {
            $_SESSION['error']='Please enter email';
            header('Location:addstudent.php');
            return;
        }
        else if(!strpos($_POST['email'],"@")>0)
        {
            $_SESSION['error']='Please enter valid email';
            header('Location:addstudent.php');
            return;  
        }
        if(strlen($_POST['address'])<1)
        {
            $_SESSION['error']='Please Address';
            header('Location:addstudent.php');
            return; 
        }
        if(is_numeric($_POST['phone'])!=1)
        {
            $_SESSION['error']='Please enter valid phone no';
            header('Location:addstudent.php');
            return; 
        }
        else
        {
            $stmpt=$pdo->prepare('INSERT INTO student_detail (f_name ,l_name ,gender, dob, blood_grp, religion, email, phone, address) 
            VALUES ( :fn , :ln , :sx , :dob , :bld , :re , :em , :phno , :add )');
            $stmpt->execute(array(
                ':fn'=>$_POST['f_name'],
                ':ln'=>$_POST['l_name'],
                ':sx'=>$_POST['gender'],
                ':dob'=>$_POST['dob'], 
                ':bld'=>$_POST['blood'], 
                ':re'=>$_POST['religion'],
                ':em'=>$_POST['email'],
                ':phno'=>$_POST['phone'],
                ':add'=>$_POST['address']
            ));
            $_SESSION['success']='Profile added';
            header("Location: addstudent.php");
            return;
        }
    }
}
ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../student/css/addstudent.css">
</head>
<body>
    <div class="container-student-3">
        <div class="page-info">
            <h3>Student Form</h3>
            <p>Home>Student Admit form</p>
        </div>
        <div class="server-message">
            <?php
            if(isset($_SESSION['error']))
            {
                echo '<p style="color:red";>'.$_SESSION['error'].'</p>';
                unset($_SESSION['error']);
            }
            if(isset($_SESSION['success']))
            {
                echo '<p style="color:green";>'.$_SESSION['success'].'</p>';
                unset($_SESSION['success']);
            }
            ?>
        </div>
        <div class="student-form-wrap">
            <div class="student-form-content">
                <h3>Add New Student</h3>     
                    <form method="post">
                        <div class="form-inputs">
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>First Name *</label>
                                <input type="text" placeholder="First Name" name="f_name" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Last Name *</label>
                                <input type="text" placeholder="Last Name" name="l_name" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                 <label>Gender *</label>
                                 <input type="text" placeholder="Gender" name="gender" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Date Of Birth *</label>
                                <input type="date" placeholder="Date of Birth" name="dob" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Blood Group *</label>
                                <input type="text" placeholder="Blood Group" name="blood" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Religion *</label>
                                <input type="text" placeholder="Religion" name="religion" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>E-Mail *</label>
                                <input type="text" placeholder="E-male" name="email" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Address *</label>
                                <input type="text" placeholder="Address" name="address" class="form-control">
                            </div>
                            <div class="col-xl-3 col-lg-6 col-12 form-group">
                                <label>Phone *</label>
                                <input type="text" placeholder="Phone No" name="phone" class="form-control">
                            </div>
                            <div class="col-12 form-group mg-t-8">
                                <input type="submit" value="submit" name="submit">
                                <input type="reset" value="Reset" id="reset">
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
</body>
</html>
shivam
  • 33
  • 5

1 Answers1

0

This error is displayed when the "header" method is called more than once in an execution scope. Let's see there, in your code there are several "if" conditions, without the "else if" treatment and somehow a "header" may be being executed more than once.

I would recommend doing the form treatments in a "Controller" or in another file, if you prefer. But as the top friend commented, you can test by replacing "return" with "exit".

Remember that "header" must be called before any real output is sent, either by normal HTML tags, blank lines in a file, or by PHP.