使用php

时间:2018-10-17 11:34:53

标签: php html

在我的项目中,当有人尝试提交联系人表格时,我尝试重定向到另一页时,我会遇到此类错误,然后应重定向到其他页面,但我遇到了此类错误。错误,这是我得到的

  

警告:无法修改标头信息-标头已发送   (输出从C:\ xampp \ htdocs \ grade \ header.php:89开始)   C:\ xampp \ htdocs \ grade \ contact.php,第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:abpaple@gmail.com">abpaple@gmail.com</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'; ?>

请任何人可以帮助我解决我的问题

2 个答案:

答案 0 :(得分:2)

错误的原因很多:Cannot modify header information - headers already sent如下:

  1. printecho语句将终止发送HTTP标头的机会
  2. <html>在任何header()呼叫之前。
  3. <?php之前的空白
  4. ?之后的空格>
  5. 前面的错误消息也可能是导致此错误的原因。

如果您想详细检查所有原因,那么这里是referenced stackoveflow answer

但是对您而言,此错误的原因是,您之前包含了Raw部分  到<?php代码中,则不能在php代码之前包含html。

在您的代码中,您将包含header.php和html <section>,而在php代码之前,您会收到此错误,因此为了解决此问题,请更改您的

contact.php 文件如下

<?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:abpaple@gmail.com">abpaple@gmail.com</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'; ?>

如果如上所述更改文件后问题仍然存在,请尝试分离html文件contact.php并制作新的php处理文件(如contact_process.php),并仅在contact_process.php中添加php处理代码。

答案 1 :(得分:0)

代替

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

您可以这样做:

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

可能不是常规的,但应该可以。