Prathameshb7 Prathameshb7 - 7 months ago 15
PHP Question

Cannot modify header information - headers already sent (output started

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\FSE\template\navigation.php:15) in C:\wamp64\www\FSE\admin\reg1.php on line 71 This is the warning i am getting.it was fine two days back works well on other pc.

give below is code of reg1.php

<?php
#start session
session_start();

#database connection
include('../config/connection.php');
include('../config/setup.php');

if (!isset($_SESSION['user_name'])) {
header('location:login.php');
}


?>

<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo 'Registration | '.$site_title; ?></title>

<?php include('../config/css.php'); ?>
<?php include('../config/js.php'); ?>
</head>

<body>
<?php include('../template/navigation.php'); ?>
<h2> <center><strong>General Information </strong></center></h2>


<div id="wrap">
<div class="container">

<?php

if (isset($_POST['submitted'])==1) {

$name = mysqli_real_escape_string($dbc, $_POST['name']);
$fname = mysqli_real_escape_string($dbc, $_POST['fathername']);
$postadd = mysqli_real_escape_string($dbc, $_POST['postadd']);
$personaladd= mysqli_real_escape_string($dbc, $_POST['personaladd']);
$sex = mysqli_real_escape_string($dbc, $_POST['sex']);
$clients = mysqli_real_escape_string($dbc, $_POST['clients']);
$state = mysqli_real_escape_string($dbc, $_POST['state']);
$city = mysqli_real_escape_string($dbc, $_POST['city']);
$pincode = mysqli_real_escape_string($dbc, $_POST['pincode']);
$email = mysqli_real_escape_string($dbc, $_POST['email']);
$date = mysqli_real_escape_string($dbc, $_POST['date']);
$mobile = mysqli_real_escape_string($dbc, $_POST['mobile']);


$q = "INSERT INTO registration (name, fname, postadd, personaladd, sex, clients, state, city, pincode, email, dob, mobile) VALUES ('$name', '$fname', '$postadd', '$personaladd', '$sex', '$clients', '$state', '$city', '$pincode', '$email', '$date', '$mobile')" ;
$r = mysqli_query($dbc, $q);

if($_POST['clients']=="farmer")
{
$b = "insert into message values((SELECT reg_id FROM registration ORDER BY reg_id DESC LIMIT 1),(select mobile from registration where reg_id ORDER BY reg_id DESC LIMIT 1),(select Company from seed_testing where State=(select State from registration ORDER BY reg_id DESC LIMIT 1)));";
$c = mysqli_query($dbc, $b);
}


if ($r) {

$value_to_page = array(
"farmer" => 'farm.php',
"investor" => 'interest.php',

);
if(isset($_POST['submitted'])){

// process other fields like $_POST['some-other']
if(isset($_POST['clients']) && isset($value_to_page[$_POST['clients']])){
**#line71 error** header('Location: '.$value_to_page[$_POST['clients']]);
return;
}
}

} else {
echo 'please enter some data or error :'.mysqli_error($dbc);
echo '<p>'.$q.'</p>';
}
}

?>




<div class="col-md-6 col-md-offset-3">


<form action="reg1.php" method="post" >

<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First name and Surname" required>
</div>


<div class="form-group">
<label for="fathername">Father Name</label>
<input type="text" class="form-control" id="fathername" name="fathername" placeholder="fathername">
</div>

<div class="form-group">
<label for="postadd">Postal Address</label>
<textarea type="text" class="form-control" id="postadd" name="postadd" placeholder="postal Address"></textarea>
</div>


<div class="form-group">
<label for="personaladd">Personal Address</label>
<textarea class="form-control" id="personaladd" rows="5" name="personaladd" placeholder="personal address"></textarea>
</div>

<div class="form-group">
<label class="radio-inline">
<input type="radio" name="sex" id="male" value="Male"> Male
</label>
<label class="radio-inline">
<input type="radio" name="sex" id="female" value="Female"> Female
</label>
</div>

<?php
?>


<div class="form-group">
<label class="radio-inline">
<input type="radio" name="clients" id="farmer" value="farmer"> farmer
</label>
<label class="radio-inline">
<input type="radio" name="clients" id="investor" value="investor"> investor
</label>
</div>

<div class="form group">
<label>state</label>
<select class="form-control" name="state">
<option selected="" ></option>
<option value="Andra Pradesh">Andra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Chhattisgarh">Chhattisgarh</option>
<option value="Delhi">Delhi</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Haryana">Haryana</option>
<option value="Himachal Pradesh">Himachal Pradesh</option>
<option value="Jammu and Kashmir">Jammu and Kashmir</option>
<option value="Jharkhand">Jharkhand</option>
<option value="Karnataka">Karnataka</option>
<option value="Kerala">Kerala</option>
<option value="Madya Pradesh">Madya Pradesh</option>
<option value="maharashtra">Maharashtra</option>
<option value="Manipur">Manipur</option>
<option value="Meghalaya">Meghalaya</option>
<option value="Mizoram">Mizoram</option>
<option value="Nagaland">Nagaland</option>
<option value="Orissa">Orissa</option>
<option value="Punjab">Punjab</option>
<option value="Rajasthan">Rajasthan</option>
<option value="Sikkim">Sikkim</option>
<option value="Tamil Nadu">Tamil Nadu</option>
<option value="Tripura">Tripura</option>
<option value="Uttaranchal">Uttaranchal</option>
<option value="Uttar Pradesh">Uttar Pradesh</option>
<option value="West Bengal">West Bengal</option>

</select>
</div>

<div class="form-group">
<label for="name">City</label>
<input type="text" class="form-control" id="city" name="city" placeholder="Enter your city">
</div>


<div class="form-group">
<label for="pincode">Pin Code</label>
<input type="number" class="form-control" id="pincode" name="pincode" placeholder="pincode">
</div>

<div class="form-group">
<label for="email">Email id</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Id">
</div>

<div class="form-group">
<label for="dob">Date of Birth</label>
<input type="date" class="form-control" id="date" name="date" >
</div>

<div class="form-group">
<label for="mobile">Mobile no.</label>
<input type="number" class="form-control" id="mobile" name="mobile" placeholder="mobile number" >
</div>



<button type="submit" class="btn btn-primary btn-lg btn-block">Save</button>
<input type="hidden" onclick = "jump()" name="submitted" value="1">

</form>
</div>
</div>
</div>

</body>
</html>


line 15 below of navigation.php

<nav class="navbar navbar-inverse" role="navigation">
<div class="container">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Contact us</a></li>
<li><a href="reg1.php?">registration</a></li>
<li><a href="stock.php?">farm stock</a></li>
</ul>

<div class="pull-right">
<ul class="nav navbar-nav">
<li class="dropdown">
**line 15 error** <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo$_SESSION['user_name'];?><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="logout.php">Logout</a></li>


</ul>
</li>
</ul>
</div>
</div>
</nav>

Answer

You cannot change the headers (i.e. redirect) if you have served anything to the page already. For example you cannot echo <p>hello</p> and then do header('Location.... because you have already served some content.

Change reg1.php to this, and it should work:

<?php 
#start session
session_start();

#database connection
include('../config/connection.php');
include('../config/setup.php');

if (!isset($_SESSION['user_name'])) {
    header('location:login.php');
}

if (isset($_POST['submitted'])==1) {
    $name   = mysqli_real_escape_string($dbc, $_POST['name']);
    $fname  = mysqli_real_escape_string($dbc, $_POST['fathername']);
    $postadd = mysqli_real_escape_string($dbc, $_POST['postadd']);
    $personaladd= mysqli_real_escape_string($dbc, $_POST['personaladd']);
    $sex   = mysqli_real_escape_string($dbc, $_POST['sex']);
    $clients   = mysqli_real_escape_string($dbc, $_POST['clients']);
    $state   = mysqli_real_escape_string($dbc, $_POST['state']);
    $city   = mysqli_real_escape_string($dbc, $_POST['city']);
    $pincode   = mysqli_real_escape_string($dbc, $_POST['pincode']);
    $email   = mysqli_real_escape_string($dbc, $_POST['email']);
    $date   = mysqli_real_escape_string($dbc, $_POST['date']);
    $mobile   = mysqli_real_escape_string($dbc, $_POST['mobile']);


    $q = "INSERT INTO registration (name, fname, postadd, personaladd, sex, clients, state, city, pincode, email, dob, mobile) VALUES ('$name', '$fname', '$postadd', '$personaladd', '$sex', '$clients', '$state', '$city', '$pincode', '$email', '$date', '$mobile')" ;
    $r = mysqli_query($dbc, $q);

    if($_POST['clients']=="farmer"){ 
        $b = "insert into message values((SELECT reg_id FROM registration ORDER BY reg_id DESC LIMIT 1),(select mobile from registration where reg_id ORDER BY reg_id DESC LIMIT 1),(select Company from seed_testing where State=(select State from registration ORDER BY reg_id DESC LIMIT 1)));";
        $c = mysqli_query($dbc, $b);
    }


    if ($r){
        $value_to_page = array(
        "farmer" => 'farm.php',
        "investor"  => 'interest.php',
        );

        if(isset($_POST['submitted'])){
            // process other fields like $_POST['some-other']
            if(isset($_POST['clients']) && isset($value_to_page[$_POST['clients']])){
                header('Location: '.$value_to_page[$_POST['clients']]);
                return;
            }
        }
    }
    else{
        echo 'please enter some data or error :'.mysqli_error($dbc);
        echo '<p>'.$q.'</p>';
    }
}
?>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo 'Registration | '.$site_title; ?></title>

    <?php include('../config/css.php'); ?>
    <?php include('../config/js.php'); ?>           
</head>

<body>
    <?php  include('../template/navigation.php'); ?>
    <h2><center><strong>General Information </strong></center></h2>

    <div id="wrap">
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <form action="reg1.php" method="post" >
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="First name and Surname" required>
                    </div>

                    <div class="form-group">
                        <label for="fathername">Father Name</label>
                        <input type="text" class="form-control" id="fathername" name="fathername" placeholder="fathername">
                    </div>

                    <div class="form-group">
                        <label for="postadd">Postal Address</label>
                        <textarea type="text" class="form-control" id="postadd" name="postadd" placeholder="postal Address"></textarea>
                    </div>

                    <div class="form-group">
                        <label for="personaladd">Personal Address</label>
                        <textarea class="form-control" id="personaladd" rows="5" name="personaladd" placeholder="personal address"></textarea>
                    </div>

                    <div class="form-group">
                        <label class="radio-inline">
                            <input type="radio" name="sex" id="male" value="Male"> Male
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" id="female" value="Female"> Female
                        </label>
                    </div>

                    <div class="form-group">
                        <label class="radio-inline">
                            <input type="radio" name="clients" id="farmer" value="farmer"> farmer
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="clients" id="investor" value="investor"> investor
                        </label>
                    </div>

                    <div class="form group">
                        <label>state</label>
                        <select class="form-control" name="state">
                            <option selected="" ></option>
                            <option value="Andra Pradesh">Andra Pradesh</option>
                            <option value="Arunachal Pradesh">Arunachal Pradesh</option>
                            <option value="Assam">Assam</option>
                            <option value="Bihar">Bihar</option>
                            <option value="Chhattisgarh">Chhattisgarh</option>
                            <option value="Delhi">Delhi</option>
                            <option value="Goa">Goa</option>
                            <option value="Gujarat">Gujarat</option>
                            <option value="Haryana">Haryana</option>
                            <option value="Himachal Pradesh">Himachal Pradesh</option>
                            <option value="Jammu and Kashmir">Jammu and Kashmir</option>
                            <option value="Jharkhand">Jharkhand</option>
                            <option value="Karnataka">Karnataka</option>
                            <option value="Kerala">Kerala</option>
                            <option value="Madya Pradesh">Madya Pradesh</option>
                            <option value="maharashtra">Maharashtra</option>
                            <option value="Manipur">Manipur</option>
                            <option value="Meghalaya">Meghalaya</option>
                            <option value="Mizoram">Mizoram</option>
                            <option value="Nagaland">Nagaland</option>
                            <option value="Orissa">Orissa</option>
                            <option value="Punjab">Punjab</option>
                            <option value="Rajasthan">Rajasthan</option>
                            <option value="Sikkim">Sikkim</option>
                            <option value="Tamil Nadu">Tamil Nadu</option>
                            <option value="Tripura">Tripura</option>
                            <option value="Uttaranchal">Uttaranchal</option>
                            <option value="Uttar Pradesh">Uttar Pradesh</option>
                            <option value="West Bengal">West Bengal</option>

                        </select>
                    </div>

                    <div class="form-group">
                        <label for="name">City</label>
                        <input type="text" class="form-control" id="city" name="city" placeholder="Enter your city">
                    </div>

                    <div class="form-group">
                        <label for="pincode">Pin Code</label>
                        <input type="number" class="form-control" id="pincode" name="pincode" placeholder="pincode">
                    </div>

                    <div class="form-group">
                        <label for="email">Email id</label>
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email Id">
                    </div>

                    <div class="form-group">
                        <label for="dob">Date of Birth</label>
                        <input type="date" class="form-control" id="date" name="date" >
                    </div>

                    <div class="form-group">
                        <label for="mobile">Mobile no.</label>
                        <input type="number" class="form-control" id="mobile" name="mobile" placeholder="mobile number" >
                    </div>

                    <button type="submit" class="btn btn-primary btn-lg btn-block">Save</button>
                    <input type="hidden" onclick = "jump()" name="submitted" value="1">
                </form>
            </div>
        </div>
    </div>
</body>
</html>
Comments