表格重新加载后再次发送

时间:2015-02-18 17:27:34

标签: php html

我的页面上有一个普通的表格。我发送表格后,我可以重新加载,它会再发送一次。我不希望发生这样的事情。

我该如何解决这个问题?我不想使用Header("location"),因为用户应该看到类似=" Successfull"的消息。我经常遇到Header("location")的问题。

这是我的代码:



<?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>
&#13;
&#13;
&#13;

现在我收到一条错误消息:无法修改标题信息 - 已经发送的标题(输出从/users/skeptar/www/Website/index.php:18开始)

我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您可以在成功或不成功提交表单后使用:

header('Location: success.php');

或者你可以使用PGR模式: PGR Pattern

enter image description here