Dominik Adamski
Dominik Adamski

Reputation: 164

PHP header location doesn't work

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 ;)

Upvotes: 0

Views: 686

Answers (1)

Dumkaaa
Dumkaaa

Reputation: 488

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

Upvotes: 2

Related Questions