skip to Main Content

In my project im getting such kind of error when im trying to redirect to another page when the person submit the contact form then it should redirect to some other page but im getting such kind of error. the error what im getting is this

warning: Cannot modify header information – headers already sent by
(output started at C:xampphtdocsgradeheader.php:89) in
C:xampphtdocsgradecontact.php on line 64

header.php

        <!DOCTYPE html>
        <html lang="en">
        <head>
        <title>SAMSON SOLAR ENERGY</title>

            <!-- Meta tag Keywords -->
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta charset="utf-8">
            <meta name="keywords" content="Grade Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template, 
            Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
            <script type="application/x-javascript">
                addEventListener("load", function () {
                    setTimeout(hideURLbar, 0);
                }, false);

                function hideURLbar() {
                    window.scrollTo(0, 1);
                }
            </script>
            <!--// Meta tag Keywords -->

            <!-- css files -->
            <link rel="stylesheet" href="css/bootstrap.css"> <!-- Bootstrap-Core-CSS -->
            <link rel="stylesheet" href="css/style.css" type="text/css" media="all" /> <!-- Style-CSS --> 
            <link rel="stylesheet" href="css/fontawesome-all.css"> <!-- Font-Awesome-Icons-CSS -->
            <!-- //css files -->
                <!----->
            <link rel="stylesheet" href="css/flexslider.css" type="text/css" media="all" /><!-- for testimonials -->
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
            <!--web font-->
            <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:200,200i,300,300i,400,400i,600,600i,700,700i,900,900i&amp;subset=cyrillic,cyrillic-ext,greek,greek-ext,latin-ext,vietnamese" rel="stylesheet">
            <!--//web font-->

        </head>

        <body>

        <!-- header -->
        <header>
            <div class="container">
            <nav class="navbar navbar-expand-lg navbar-light">
                    <a class="navbar-brand" href="index.html">
                <span class="fa fa-strikethrough"></span> SAMSON SOLAR
                    </a>

                    <button class="navbar-toggler ml-md-auto" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
                    </button>

                    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mx-auto text-center">
                            <li class="nav-item active  mr-lg-3">
                                <a class="nav-link" href="index.php">Home<span class="sr-only">(current)</span></a>
                            </li>
                            <li class="nav-item  mr-lg-3">
                                <a class="nav-link" href="about.php">about</a>
                            </li>
                            <li class="nav-item  mr-lg-3">
                                <a class="nav-link" href="gallery.php">Gallery</a>
                            </li>
                            <li class="nav-item dropdown mr-lg-3">
                                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Products</a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="domestic.php">Domestic</a>
                                    <a class="dropdown-item" href="commercial.php">Commercial</a>
                                    <a class="dropdown-item" href="other.php">Other Products</a>
                    </div>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="contact.php">contact</a>
                            </li>
                        </ul>
                        <div class="buttons">
                            <p><i class="fas mr-1 fa-phone"></i> +91 99161 67190</p>
                </div>
                    </div>
            </nav>
            </div>

            <div class="icon-bar">
                <a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
                <a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
                <a href="#" class="google"><i class="fa fa-google"></i></a> 
                <a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
                <a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
            </div>

        </header>
        <!-- //header -->

contact.php

        <?php include 'header.php'; ?>

        <!-- banner -->
        <section class="inner-banner">
            <div class="banner-layer">
                <h1 class="text-center">Contact Page</h1>
            </div>
        </section>
        <!-- //banner -->

        <?php 
            include_once 'connect.php';
            $nameErr = $emailErr = $contactErr = "";
            $name = $email = $contact = $subject = "";

            if (isset($_POST['submit'])) 
            {
                //Name 
                if (empty($_POST["name"])) {
                    $nameErr = "Please enter your Name";
                } 
                    else{
                        $name = test_input($_POST["name"]);
                        // check if name only contains letters and whitespace
                        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                            $nameErr = "Sorry Only letters are allowed"; 
                        }
                    }

                    //E-mail ID
                    if (empty($_POST["email"])) {
                        $emailErr = "Please enter your E-mail ID";
                        } 
                    else{
                        $email = test_input($_POST["email"]);
                        // check if the entered Email-id is valid or not
                            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                                $emailErr = "Please enter a valid E-mail ID"; 
                            }
                        }

                    //Contact Number
                    if (empty($_POST["phone"])) {
                        $contactErr = "Please enter your Contact Number";
                    } 
                    else{
                        $contact = test_input($_POST["phone"]);
                        // check weather the contact number is valid or not
                        if (!preg_match("/^[0-9]+$/",$contact)) {
                            $contactErr = "Please enter Your 10 Digit mobile Number"; 
                        }
                    }

                    //Reason For contacting
                    if (!empty($_POST["subject"])) {
                        $subject = test_input($_POST["subject"]);
                    } 

                    if($nameErr == "" && $emailErr == "" && $contactErr == ""){
                    $sql = "INSERT INTO contact(`ID`,`Name`, `Email`, `Contact_no` , `Reason`)
                                VALUES ('','".$name."', '".$email."', '".$contact."' , '".$subject."')";

                if (mysqli_query($mysqli, $sql)) {
                            header("location:contact_success.php");
                            exit();
                        }
                else{
                                echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
                            } 
                            mysqli_close($mysqli);
                    }

            }

            function test_input($data) {
                    $data = trim($data);
                    $data = stripslashes($data);
                    $data = htmlspecialchars($data);
                    return $data;
                }

        ?>

    <!-- contact -->
    <section class="contact py-5">
        <div class="container">
        <h2 class="heading mb-lg-5 mb-4">Contact Us</h2>
        <div class="row contact-grids w3-agile-grid">
                <div class="row col-md-6 col-sm-6 contact-grid1 w3-agile-grid">
            <div class="col-3 text-center">
                        <i class="fa fa-commenting-o" aria-hidden="true"></i>
            </div>
                    <div class="col-9 p-0">
                        <h4>Get In Touch</h4><br>
                        <p><b>Mobile:</b> <a href="tel:+91 9916167190">+91 9916167190</a></p>
                        <p><b>E-mail:</b> <a href="mailto:[email protected]">[email protected]</a></p>
                    </div>
                </div>
                <div class="row col-md-6 col-sm-6 mt-sm-0 mt-4 contact-grid1 w3-agile-grid">
            <div class="col-3 text-center">
                        <i class="fa fa-map-marker" aria-hidden="true"></i>
            </div>
            <div class="col-9 p-0">
                        <h4>Visit Our Location</h4><br>
                        <p><b>SAMSON SOLAR ENERGY</b></p>
                        <p>#02, K.E.Board Complex, Navalgund Road, Shivaji Circle</p>
                        <p>DHARWAD-580008, KARNATAKA, INDIA</p>
            </div>
                </div>
        </div><br><br>
            <div class="row">
                <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
                    <div class="gmap_canvas">
                        <iframe width="100%" height="475" id="gmap_canvas" src="https://maps.google.com/maps?q=SAMSON%20SOLAR%20ENERGY%20%20%2302%2C%20K.E.Board%20Complex%2C%20Navalgund%20Road%2C%20Shivaji%20Circle%20%20DHARWAD-580008%2C%20KARNATAKA%2C%20INDIA&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
                    </div>
                </div>
                <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
                    <h2 style="margin-bottom:15px">Write To Us</h2>
                    <form action="" method="post">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Enter Name" name="name" value="<?php if (isset($name)) echo $name; ?>">
                            <span style="color:red;"> <?php echo $nameErr;?></span>
                        </div>
                        <div class="form-group">
                            <input type="email" class="form-control" placeholder="Enter E-mail" name="email" value="<?php if (isset($email)) echo $email; ?>">
                            <span style="color:red;"> <?php echo $emailErr;?></span>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Enter Contact Number" name="phone" value="<?php if (isset($contact)) echo $contact; ?>">
                            <span style="color:red;"> <?php echo $contactErr;?></span>
                        </div>
                        <div class="form-group">
                            <textarea class="form-control" rows="4" placeholder="Enter Reason For Contacting" name="subject"><?php if (isset($subject)) echo $subject; ?></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary pull-right" name="submit">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </section>
    <!-- //contact -->

    <?php include 'footer.php'; ?>

please can any one help me to solve my issue

2

Answers


  1. There are lots of reason for the error: Cannot modify header information - headers already sent below are some of them:

    1. print and echo statements will terminate the opportunity to send HTTP headers
    2. <html> before any header() call.
    3. Whitespace before <?php
    4. Whitespace after ?>
    5. Preceding error messages can also be a cause of this error.

    If You want to check all reasons in detail then here is referenced stackoveflow answer

    But for you the reason of this error is that, you have included Raw sections prior
    to <?php code, you can not include html before php code.

    In your code you are including header.php and <section> which is html, before php code therefor you are getting this error, so for solving it change your

    contact.php file as below

    <?php 
            include_once 'connect.php';
            $nameErr = $emailErr = $contactErr = "";
            $name = $email = $contact = $subject = "";
    
            if (isset($_POST['submit'])) 
            {
                //Name 
                if (empty($_POST["name"])) {
                    $nameErr = "Please enter your Name";
                } 
                    else{
                        $name = test_input($_POST["name"]);
                        // check if name only contains letters and whitespace
                        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                            $nameErr = "Sorry Only letters are allowed"; 
                        }
                    }
    
                    //E-mail ID
                    if (empty($_POST["email"])) {
                        $emailErr = "Please enter your E-mail ID";
                        } 
                    else{
                        $email = test_input($_POST["email"]);
                        // check if the entered Email-id is valid or not
                            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                                $emailErr = "Please enter a valid E-mail ID"; 
                            }
                        }
    
                    //Contact Number
                    if (empty($_POST["phone"])) {
                        $contactErr = "Please enter your Contact Number";
                    } 
                    else{
                        $contact = test_input($_POST["phone"]);
                        // check weather the contact number is valid or not
                        if (!preg_match("/^[0-9]+$/",$contact)) {
                            $contactErr = "Please enter Your 10 Digit mobile Number"; 
                        }
                    }
    
                    //Reason For contacting
                    if (!empty($_POST["subject"])) {
                        $subject = test_input($_POST["subject"]);
                    } 
    
                    if($nameErr == "" && $emailErr == "" && $contactErr == ""){
                    $sql = "INSERT INTO contact(`ID`,`Name`, `Email`, `Contact_no` , `Reason`)
                                VALUES ('','".$name."', '".$email."', '".$contact."' , '".$subject."')";
    
                if (mysqli_query($mysqli, $sql)) {
                            header("location:contact_success.php");
                            exit();
                        }
                else{
                                echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
                            } 
                            mysqli_close($mysqli);
                    }
    
            }
    
            function test_input($data) {
                    $data = trim($data);
                    $data = stripslashes($data);
                    $data = htmlspecialchars($data);
                    return $data;
                }
    
        ?>
        <?php include 'header.php'; ?>
    
        <!-- banner -->
        <section class="inner-banner">
            <div class="banner-layer">
                <h1 class="text-center">Contact Page</h1>
            </div>
        </section>
        <!-- //banner -->
    
     <!-- contact -->
    <section class="contact py-5">
        <div class="container">
        <h2 class="heading mb-lg-5 mb-4">Contact Us</h2>
        <div class="row contact-grids w3-agile-grid">
                <div class="row col-md-6 col-sm-6 contact-grid1 w3-agile-grid">
            <div class="col-3 text-center">
                        <i class="fa fa-commenting-o" aria-hidden="true"></i>
            </div>
                    <div class="col-9 p-0">
                        <h4>Get In Touch</h4><br>
                        <p><b>Mobile:</b> <a href="tel:+91 9916167190">+91 9916167190</a></p>
                        <p><b>E-mail:</b> <a href="mailto:[email protected]">[email protected]</a></p>
                    </div>
                </div>
                <div class="row col-md-6 col-sm-6 mt-sm-0 mt-4 contact-grid1 w3-agile-grid">
            <div class="col-3 text-center">
                        <i class="fa fa-map-marker" aria-hidden="true"></i>
            </div>
            <div class="col-9 p-0">
                        <h4>Visit Our Location</h4><br>
                        <p><b>SAMSON SOLAR ENERGY</b></p>
                        <p>#02, K.E.Board Complex, Navalgund Road, Shivaji Circle</p>
                        <p>DHARWAD-580008, KARNATAKA, INDIA</p>
            </div>
                </div>
        </div><br><br>
            <div class="row">
                <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
                    <div class="gmap_canvas">
                        <iframe width="100%" height="475" id="gmap_canvas" src="https://maps.google.com/maps?q=SAMSON%20SOLAR%20ENERGY%20%20%2302%2C%20K.E.Board%20Complex%2C%20Navalgund%20Road%2C%20Shivaji%20Circle%20%20DHARWAD-580008%2C%20KARNATAKA%2C%20INDIA&t=&z=13&ie=UTF8&iwloc=&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
                    </div>
                </div>
                <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
                    <h2 style="margin-bottom:15px">Write To Us</h2>
                    <form action="" method="post">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Enter Name" name="name" value="<?php if (isset($name)) echo $name; ?>">
                            <span style="color:red;"> <?php echo $nameErr;?></span>
                        </div>
                        <div class="form-group">
                            <input type="email" class="form-control" placeholder="Enter E-mail" name="email" value="<?php if (isset($email)) echo $email; ?>">
                            <span style="color:red;"> <?php echo $emailErr;?></span>
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Enter Contact Number" name="phone" value="<?php if (isset($contact)) echo $contact; ?>">
                            <span style="color:red;"> <?php echo $contactErr;?></span>
                        </div>
                        <div class="form-group">
                            <textarea class="form-control" rows="4" placeholder="Enter Reason For Contacting" name="subject"><?php if (isset($subject)) echo $subject; ?></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary pull-right" name="submit">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </section>
    <!-- //contact -->
    
    <?php include 'footer.php'; ?>
    

    And if problem remains the same after changing file as above then try to separate html file contact.php and make new php process file like contact_process.php and add php processing code only in contact_process.php.

    Login or Signup to reply.
  2. Instead of

    if (mysqli_query($mysqli, $sql)) {
        header("location:contact_success.php");
        exit();
    }
    

    You could do this:

    if (mysqli_query($mysqli, $sql)) {
        echo "<script>window.location.href = 'contact_success.php';</script>";
        exit();
    }
    

    May not be conventional but it should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search