从 foreach 循环中只提交一个表单

Submit only one form from foreach loop

我在我的网站上仅提交其中一个表单时遇到问题。
表单是通过 foreach 循环动态创建的。

每次我提交一个表单时,它都会提交所有表单,所以如果有 10 个帖子,那么他们每个都有一个回复表单,并将所有表单都插入到数据库中。

<div id="newsFeed">

<div class='newsBox'>
    <div class="newsProfileInfo">
        <div class="newsProfileImg">
            <img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $name; ?>">
        </div><!--newsProfileInfo ends here-->


        <div class="newsName">
            <p>
                <?php echo $res->name." ".$res->surname;?>
            </p>
        </div><!--newsName-->

    </div><!--newsProfileInfo ends here-->


    <div class="newPost">
        <form action='' method='post'>

            <textarea name="newPost" placeholder="Opret nyt indlæg"></textarea>

            <input type="submit" name="postSubmit" value="Opret">
        </form>
    </div>

<?php 
    $data = newsFeed($db);

    foreach($data as $news){

        $newsId     = $news['newsId'];
        $headline   = $news['headline'];
        $post       = $news['post'];
        $date       = $news['postDate'];
        $dates      = date_create($date);

        $name       = $news['name'];
        $surname    = $news['surname'];
        $profileImg = $news['profileImage'];

        $path = "images/profileImages/";

?>

<div class="newsProfileInfo">
    <div class="newsProfileImg">
        <img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name; ?>">
    </div><!--newsProfileInfo ends here-->

    <div class="newsName">
        <p><a href=""><?php echo substr($name ." ". $surname, 0,11);?></a></p>
    </div><!--newsName-->

    <p class="postDate">
        <?php echo date_format($dates, 'd/m/Y') ." - ".date_format($dates,'H:i');?>
    </p>

</div><!--newsProfileInfo ends here-->


<div class="newsPost">
    <p>
        <?php echo $post;?>
    </p>
</div>



<?php
    $reply = replyToPost($db,$newsId);

    foreach($reply as $msg){


        $replyId    = $msg['replyId'];
        $message    = $msg['message'];
        $news_fk    = $msg['newsId_fk'];
        $userId_fk  = $msg['userId_fk'];
        $date       = $msg['date'];
        $name       = $msg['name'];
        $surname    = $msg['surname'];
        $profileImg = $msg['profileImage'];

        $path = "images/profileImages/";
?>

<div class="replysLoop">
    <div class="replyImg">
        <img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name;?>">
    </div>

    <div class="replysBoxes">
        <p><?php echo $message;?></p>
    </div><!--replysBoxes ends here-->


</div><!--replysLoop ends here-->

<?php
    }
?>

<div class="replyToMsg">

    <div class="replyImg">
        <img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $res->name; ?>">
    </div>

    <?php
    if(isset($_POST['replySubmit'])){
        insertReply($db,$newsId);
        header('Location: index.php');
    }
    ?>

    <div class="inputHolder">
        <form action="" method="post">
            <input type="text" name="replys" placeholder="Besvar indlæg">
            <input type="submit" name="replySubmit" value="Besvar">
        </form>
    </div>

</div>

</div><!--newsbox ends here-->
    <?php                  
        }//foreach loop newsfeed ends here
    ?>

据我所知,提交按钮只会提交它所在的表单。事实上,一次提交多个表单是非常困难的。

我怀疑问题在于提交回复的表单未绑定到当前评论并最终被添加到所有评论中。

我建议你从这样的事情开始:

<?php
//... something that results in this post setting `$postid`
?>
<div class="reply">
    <form action="" method="post">
        <input type="hidden" name="replyToPostID" value="<?php echo $postid; ?>">
        <input type="text" name="replyToPost" placeholder="Besvar indlæg">
        <input type="submit">
    </form>
</div>

然后您的 PHP 可以使 SQL 将评论固定到一个父评论。当然你是在逼我猜,因为你发布的不可能是完整的代码。

  • 另请参阅:Submit Multiple Forms With One Button