0

I have a little problem with header redirection. I try make it in this way, but it doesn't redirect. I checked if statement and all instructions are made properly. Only redirect does not work and I don't know why.

<!DOCTYPE html>
<html class="full" lang="en">

<head>
    <?php include 'html/head.html'; ?>
    <link href="css/the-big-picture.css" rel="stylesheet">
</head>

<body>

<?php include 'html/nav.html';
session_start();
if(isset($_POST['returnItem'])){
    $connection = mysqli_connect("localhost", "root", "stdinv2016", "invbook");
    $query = "SELECT id_wypozyczenia, nazwa_przedmiotu, nr_fabryczny_przychodu,imie_nazwisko, data_wypozyczenia FROM `wypozyczenia` WHERE id_wypozyczenia=".$_POST['returnItem'];
    $connection->set_charset("utf8");
    $result = mysqli_query($connection, $query, $resultmode = MYSQLI_STORE_RESULT) or die("Query fail: " . mysqli_error());
    if ($result->num_rows < 1)
    {
        echo '<div class="alert alert-dismissible alert-danger col-lg-4 col-lg-offset-4" align="center">
        <strong>Aktualnie żaden sprzęt nie został wypożyczony</strong><br> Spróbuj jeszcze raz. <br></div>';
    }
    else{
        echo "<div class='col-lg-offset-3 col-lg-6'>
    <div class='well bs-component'>
    <div class='alert alert-dismissible alert-warning'>
    <strong><p align='center'>Czy na pewno chcesz zatwierdzić zwrot poniższego przedmiotu?</p></strong>
    </div>
        <div class=\"table table-striped table-hover\">";
                echo "<table class=\"table\"><tr><th>ID</th><th>Nazwa przedmiotu</th><th>Nr inwentarzowy<th>Wypożyczający<th>Data wypożyczenia</th></tr>";
                // output data of each row
                    $row = $result->fetch_assoc();
                    echo "<tr><td>" . $row['id_wypozyczenia'] . "</td><td>" . $row['nazwa_przedmiotu'] . "</td><td>" . $row['nr_fabryczny_przychodu'] . "</td><td>" .$row['imie_nazwisko'] . "</td><td>" . $row['data_wypozyczenia']. "</td></tr>";

                echo "</table>

                </div>
                <form method='post' action='confirmReturn.php'>
                <div align='center'>
                <input type='hidden' name='invNumber' value=".$row['id_wypozyczenia'].">
                <button class='btn btn-success' type='submit' name='confirmReturn' value='yes'>Tak</button>
                <button class='btn btn-danger' type='submit' name='confirmReturn' value='no'>Nie</button>
                </form>
                </div>
                </div></div>";
    }
}
else if(isset($_POST['confirmReturn']) && $_POST['confirmReturn']=='yes'){
    //print_r($_POST);
    /*$connection = mysqli_connect("localhost", "root", "stdinv2016", "invbook");
    $query = "SELECT id_wypozyczenia, nazwa_przedmiotu, nr_fabryczny_przychodu,imie_nazwisko, data_wypozyczenia FROM `wypozyczenia` WHERE id_wypozyczenia=".$_POST['returnItem'];
    $connection->set_charset("utf8");
    $result = mysqli_query($connection, $query, $resultmode = MYSQLI_STORE_RESULT) or die("Query fail: " . mysqli_error());
    if ($result->num_rows < 1)
    {
        echo '<div class="alert alert-dismissible alert-danger col-lg-4 col-lg-offset-4" align="center">
        <strong>Aktualnie żaden sprzęt nie został wypożyczony</strong><br> Spróbuj jeszcze raz. <br></div>';
    }*/
}
else if(isset($_POST['confirmReturn']) && $_POST['confirmReturn']=='no'){
    header("location: returnItem.php");
}
else{
header("location: returnItem.php");
}



include 'html/scripts.html'; ?>
</body>
</html>

P.S. Sorry for my English ;)

Dominik Adamski
  • 164
  • 2
  • 14
  • 1
    Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Jonnix Jun 01 '16 at 08:41
  • Write exit; after header("Location:returnItem.php"); and there is no meaning to include some file at the bottom. – Ravi Hirani Jun 01 '16 at 08:50

1 Answers1

2

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Original from php.net

Dumkaaa
  • 488
  • 2
  • 9