-1

I know this topic exist already: Link, but this answer didnt helped me. I have a formular, if I reload the page, the formular will send the same again. Now I wrote this Question. Here is my Code now of the file:

<?php
    $article_id = $_GET['post'];
    if(isset($_REQUEST['submit'])){

        $article_id = $_GET['post'];
        $name = $_POST['name'];
        $nachricht = $_POST['nachricht'];
        $aufgabe = $_POST['aufgabe'];
        $random = $_POST['random'];

        $successfull = FALSE;
        $error_message_empty = "";
        $counter = 0;

        if(empty($name)){
            $successfull = FALSE;
            $error_message_empty = $error_message_empty . "<li>Name</li>";
            $counter++;
        }

        if(empty($nachricht)){
            $successfull = FALSE;
            $error_message_empty = $error_message_empty . "<li>Nachricht</li>";
            $counter++;
        }

        if(empty($aufgabe)){
            $successfull = FALSE;
            $error_message_empty  = $error_message_empty . "<li>Spamschutz</li>";
            $counter++;
        }
        else{
            $sql = "SELECT * FROM captcha WHERE captcha_id='".$random."'";
            $result = mysqli_query($db, $sql);
            while($row=mysqli_fetch_assoc($result)){
                if(!($row['captcha_solution'] == $aufgabe)){
                    $successfull = FALSE;
                    $error_message_empty  = $error_message_empty . "<li>Spamschutz</li>";
                    $counter++;
                }
            }
        }

        if($counter != 0){
        }
        else{
            $successfull = TRUE;
        }

        if($successfull == TRUE){
            $eintrag = "INSERT INTO comments (comment_author, comment_date, comment_message, comment_post_id) VALUES ('$name', NOW(), '$nachricht', '$article_id')";
            $eintragen = mysqli_query($db, $eintrag);
            header('Location: index.php'); // ERROR MESSAGE
            exit;
        }



        if(!(empty($error_message_empty))){
            $error_message_empty = "<ol class='contact-form_error-ol'>".$error_message_empty;
            $error_message_empty = $error_message_empty . "</ol>";
        }
    }

?>


<div class="section-head">
        <?php
            $select = $_GET['post'];
            $sql = "SELECT * FROM posts WHERE post_id='".$select."'";
            $result=mysqli_query($db,$sql);

            while($row=mysqli_fetch_assoc($result)){
                $sql2 = "SELECT * FROM categories";     
                $result2=mysqli_query($db,$sql2);

                while($row2=mysqli_fetch_assoc($result2)){
                    if($row['post_categorie'] == $row2['categorie_id']){
                        echo "<p>Der ausgewählte Beitrag befindet sich in der Kategorie: <a href='index.php?categorie=$row2[categorie_id]' class='link'>$row2[categorie_name]</a></p>";
                    }
                } 
            }
        ?>
</div>
<div class="section-body">
        <?php
            $sql = "SELECT * FROM posts WHERE post_id='".$select."'";
            $result = mysqli_query($db, $sql);
            $counter = 1;
            while($row=mysqli_fetch_assoc($result)){
                echo "<article>";
                    echo "<a class='article-title'>$row[post_title]</a>";
                    echo "<br>";
                    $d = date_create($row['post_date']); 
                    $datum = $d->format('j.m.Y \u\m H:i \U\h\r'); 
                    echo "<a class='article-author-date'>geschrieben von $row[post_author] am $datum</a>";
                    echo "<br>";
                    echo "<br>";
                    echo "<p class='article-message'>$row[post_message]</p>";

                    echo "<div class='article-div-image'>";
                        $counter2 = 1;
                        for($i = 1; $i <= 4; $i++){
                            if($row['post_image_'.$i] != "0"){
                                if($counter2 % 2 == 0){
                                    echo '<a data-lightbox="image-'.$counter.'" href="upload/'.$row['post_image_'.$i].'" class="wow"><img src="upload/'.$row['post_image_'.$i].'" class="article-image"></a>';
                                    $counter2 = 0;
                                }
                                else{
                                    echo '<a data-lightbox="image-'.$counter.'" href="upload/'.$row['post_image_'.$i].'" class="wow"><img src="upload/'.$row['post_image_'.$i].'" class="article-image margin-right"></a>';    
                                }
                            }
                            $counter2++;
                        }
                    echo "</div>";

                    if($counter != mysqli_num_rows($result)){
                        echo "<hr>";
                    }
                    $counter++;
                echo "</article>";
            } 
        ?>
        <hr>
        <article>
            <a class="article-title">Hinterlasse doch ein Kommentar</a><br>
            <a>Alle unten aufgelisteten Felder müssen ausgefüllt werden.</a>        
            <?php 
                if(!(empty($error_message_empty))){
                    echo "<div class='contact-form-error'>";
                        echo "Folgende Felder wurden nicht korrekt ausgefüllt: <br>";
                        echo $error_message_empty."<br>";
                        echo "Nach der Behebung der oben aufgelisteten Fehler, versuchen Sie es erneut.";
                    echo "</div>";
                }
                else if(!(empty($error_spam))){
                    echo "<div class='contact-form-error'>";
                        echo $error_spam;
                    echo "</div>";
                }
                else if(isset($successfull)){
                    if($successfull == TRUE){
                        echo "<div class='contact-form-success'>";
                            echo "Der Kommentar wurde erfolgreich erstellt!";
                        echo "</div>";
                    }
                }
            ?>
            <form class="contact-formular" action="index.php?post=<?php echo $article_id ?>" method="POST">
                <a class="article-input-a">Name:</a><br><input name="name" type="text"  maxlength="30" value="<?php if(isset($_POST['name'])){ echo $_POST['name'];}?>"><br>
                <a class="article-input-a">Nachricht:</a><br><textarea name="nachricht"><?php if(isset($_POST['nachricht'])){ echo $_POST['nachricht'];}?></textarea><br>
                <a class="article-input-a">Captcha:</a><br>
                <?php
                    $sql = "SELECT * FROM captcha";
                    $result = mysqli_query($db, $sql);
                    $random = rand(1, 3);
                    while($row=mysqli_fetch_assoc($result)){
                        if($random == $row['captcha_id']){
                            echo "<img class=contact-formular-captcha src=images/captcha/$row[captcha_task]>";
                            echo "<input type='hidden' name='random' value='$random' />";
                        }
                    }   
                ?>
                <br><input name="aufgabe" type="text"  maxlength="30"><br>
                <button class="contact-formular-button" name="submit" type="submit">Kommentar abschicken</button>   
            </form>
        </article>
        <div class="comment-box">
            <?php
                $select = $_GET['post'];
                $sql = "SELECT * FROM comments WHERE comment_post_id='".$select."' ORDER BY comment_date DESC";
                $result=mysqli_query($db,$sql);
                $number = mysqli_num_rows($result);

                if(empty($number)){
                }
                else{
                    echo "<hr>";
                    echo "<a class='article-title'>Bereits erstelle Kommentare</a><br>";
                    while($row=mysqli_fetch_assoc($result)){
                        echo "<div class='comment-only-one'>";
                        $d = date_create($row['comment_date']); 
                        $datum = $d->format('j.m.Y \u\m H:i \U\h\r'); 
                        echo "Geschrieben von $row[comment_author] am $datum<br><br>";
                        echo "$row[comment_message]<br>";
                        echo "</div>";
                    }
                }
            ?>
        </div>
</div>

Here is the error message: Warning: Cannot modify header information - headers already sent by (output started at /users/skeptar/www/Website/index.php:18) in /users/skeptar/www/Website/include/contant-output-one-article.html on line 54 I marked the line in the code with // ERROR MESSAGE Here is my index:

<!DOCTYPE HTML>
<html>
    <head>
        <link href="css.css" type="text/css" rel="stylesheet">

        <link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">

        <link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>

        <!-- Lightbox -->
        <script src="js/jquery-1.11.0.min.js"></script>
        <script src="js/lightbox.min.js"></script>
        <link href="css/lightbox.css" rel="stylesheet" />

        <meta charset="utf-8">
    </head>
    <body>
        <?php
            include "connection.php"; 
        ?>
        <header>
        </header>
        <br>
        <br><br>
        <div class="wrapper">
            <nav>
                <ul>
                    <li><a href="index.php?content=news"><i class="fa fa-home"></i>Startseite</a></li>
                    <li><a href="index.php?content=picture-gallery"><i class="fa fa-picture-o"></i>Bildergalerie</a></li>
                    <li><a href="index.php?content=contact"><i class="fa fa-child"></i>Kontakt</a></li>
                </ul>
            </nav>

            <section>
                <?php
                    if(!(isset($_GET['content']))){
                        if(!(isset($_GET['categorie']))){
                            if(!(isset($_GET['post']))){
                                include "include/content-output-all-news.html";                
                            }
                            else{
                                include "include/contant-output-one-article.html";    
                            }
                        }
                        else{
                            include "include/content-output-categorie-sort.html";
                        }
                    }
                    else{
                        switch($_GET['content']){
                            case "news":
                                include "include/content-output-all-news.html";
                                break;
                            case "contact": 
                                include "include/contact/contact-form.html";
                                break;
                            case "picture-gallery":
                                include "include/picture-gallery/picture-gallery.html";
                                break;
                            default:
                                include "include/content-output-all-news.html";
                                break;
                        }
                    }

                ?>
            </section>

            <aside>
                <div class="aside-title no-margin-top">
                    Wer bin ich?
                </div>
                <img class="aside-thats-me" src="images/thats-me.png">
                <p>
                    usto duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temporinvidunt                     ut labore et dolore magna aliquyam erat, sed diam <a href="#" class="link">askasd</a> voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est                     Lorem ipsum dolor sit amet.
                </p>
                <div class="aside-title">
                    Kategorien
                </div>
                <p>
                    usto duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temporinvidunt                     ut labore et dolore magna aliquyam erat, sed diam <a href="#" class="link">askasd</a> voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est                     Lorem ipsum dolor sit amet.
                </p>
                <br>
                <br>
                <?php
                    include 'include/aside-output-categories.html';
                ?>
            </aside>
            <div class="both"></div>
            <footer>
                <ul>
                    <li><a href="#"><i class="fa fa-info"></i>Impressum</a></li>
                    <li><a href="index.php?content=contact"><i class="fa fa-child"></i>Kontakt</a></li>
                    <div class="both"></div>
                </ul>
            </footer>
        </div> 

    </body>
</html>

Here is the code of the database connection:

<?php


    $db = mysqli_connect("localhost", "root", "", "blog");
    mysqli_set_charset($db, 'utf8');
    if(!$db){
        die("Verbindung zur Datenbank fehlgeschlagen!");
    }

?>

Maybe someone of you have an idea.

Community
  • 1
  • 1
Skeptar
  • 149
  • 1
  • 10

1 Answers1

0

Where is the part with template <html><head></head><body>... ?

I guess you already sent it to the browser somewhere before?

So that means you can't change headers.

If you create your site from scratch (not using any cms or framework)

You can start your index.php main controller with:

ob_start();

and handle the outputs any specific way you want.

for example:

ob_clean();
header('Location: index.php'); // ERROR MESSAGE

According to your post changes Now we can see that you have to change your index.php starts with:

<?php ob_start(); ?><!DOCTYPE HTML>

Alex
  • 16,739
  • 1
  • 28
  • 51
  • Now my Code look like this: ' ob_clean(); header('Location: index.php'); // ERROR MESSAGE exit; }', but it wont work – Skeptar Feb 19 '15 at 17:20
  • the first thing to do - change the first line in your index.php! check bottom of my updated answer. first characters MUST be ` – Alex Feb 19 '15 at 17:22
  • 1
    you are welcome :-) and by the way this was 100% duplicate :-) try to read and understand explanations deeper :-) good luck! – Alex Feb 19 '15 at 17:32