0

I have a normal form on my page. After I sent the form, I can reload and it will send the same a second time. I don't want that something like this happen.

How can I fix the problem? I don't want to use Header("location"), because the user should see a message like = "Successfull". And I had often problems with Header("location").

Here is my Code:

<?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');  /* Here is the header -------------------------------------------*/
        }
        


        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>

And now i get an error message: Cannot modify header information - headers already sent by (output started at /users/skeptar/www/Website/index.php:18)

I dont know how to fix that.

Skeptar
  • 149
  • 1
  • 10
  • header("Location: ..") would be the correct solution for a situation like this. To show a success message, you can just create a seperate page that shows the message. – Dragony Feb 18 '15 at 17:28
  • *"And i had often problems with Header("location")"* - Let me guess... headers already sent. – Funk Forty Niner Feb 18 '15 at 17:33
  • With problems i mean, it often dont work. I tested it local but on a webspace it didnt worked. – Skeptar Feb 18 '15 at 17:37
  • Probably because you may be outputting before header. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Feb 18 '15 at 17:48
  • However, the code you posted isn't what needs to be worked with. We need to see your PHP for it, not the HTML form. – Funk Forty Niner Feb 18 '15 at 17:50
  • Okay thanks for the answer! I will answer this evening. – Skeptar Feb 18 '15 at 17:55
  • I posted the Code in my question – Skeptar Feb 18 '15 at 17:59
  • If you had problems with header, add `exit;` after the header. I.e.: `header('Location: index.php'); exit;` that will stop further execution of any code below that. Many times, that fixes the problem. – Funk Forty Niner Feb 18 '15 at 18:17
  • Nope in this case not. I get this error code: ` Cannot modify header information - headers already sent by (output started at /users/skeptar/www/Website/index.php:18) in ` is that enough info? – Skeptar Feb 18 '15 at 18:27
  • That's because you have output before the header. You can't have anything above it. If you have a form, or included file, or possibly a BOM (byte order mark), that will also cause the headers already sent. You can try doing this though ` – Funk Forty Niner Feb 18 '15 at 18:46
  • Too many things for me to explain to you. You have a lot to learn and I won't be able to continue commenting like this. You have an answer below. You can also read the following and read it attentively. http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php Good luck. – Funk Forty Niner Feb 18 '15 at 19:06

1 Answers1

1

You can, after success or unsuccess form submission, use:

header('Location: success.php');

or you can use PGR pattern: PGR Pattern

enter image description here

lmarcelocc
  • 1,301
  • 11
  • 21
  • Cool graphic. Now where's the icon for *"Don't put anything above this, otherwise you'll get a `headers already sent...` warning"*? - OP stated: *"And i had often problems with Header("location")"* – Funk Forty Niner Feb 18 '15 at 17:34
  • @Fred-ii-, wikipedia is an open-source plataform where anyone can help...you should improve that article, justo follow the link above the image. – lmarcelocc Feb 18 '15 at 17:38
  • *Hm...*, interesting and a good idea; I might just do that. *Cheers* – Funk Forty Niner Feb 18 '15 at 17:39
  • btw thanks for the answer! i try to understand the image + wikipedia ^^ – Skeptar Feb 18 '15 at 18:03