Button在HTML中重定向错误的表单

时间:2017-11-19 19:27:16

标签: php html css

我创建了一个程序,其中有一部分用户评论和一个部分,您可以在登录时键入评论。

如果您已登录,则您编写的任何评论都有一个删除按钮,可将您重定向到名为“deletecomment.php”的单独php页面,该页面处理删除评论。

还有一个用于提交评论的按钮,以不同的形式使用不同的操作,可以将您带到处理提交评论的“comment.php”。

我的问题是带有操作“comment.php”的表单会将您重定向到“deletecomment.php”。但由于它们是两个单独的形式,有两个单独的按钮,所以不应该发生这种情况。

为什么第二种形式会将我重定向到“deletecomment.php”而不是“comment.php”

<div class="textblock">
                <h1>User Comments</h1>
    <ul id="usercomment">
        <li>
            <?php foreach($readin as $string):?>
        <div>
    <form action="deletecomment.php" method="post">
        <?php if($count%2==1): ?>
                <?php $readin_copy = $readin;
                echo current($readin_copy);?>
             - <?php  echo $string?> 
                    <?php if($string===$_SESSION['username']):?> 
            <button type="submit">Delete comment</button> 
        <input name='delcomment' type='hidden' value= "<?php echo current($readin_copy);?>">
            </form>
        </div>
        <?php endif;?>
            <?php endif; ?>
            <?php $count++;?>
             <?php endforeach; ?>
        </li>
    </ul>
            </div>
        <br />
            <div class="textblock">
        <ul>
         <?php if($_SESSION['logged_in']==true):?>
        <li><form action="comment.php" method="post"> <input type="text" placeholder="Comment" name="comment" required> <button type="submit">Enter Comment</button></form>
        </li>
        <?php endif;?>
        </ul>
            </div>

2 个答案:

答案 0 :(得分:0)

你的文件上还有其他代码吗?你有一个isset函数来处理点击了哪个按钮吗?

您在哪里存储评论?

对于每个提交按钮,请按以下方式命名:

<input type="submit" name="delete" value="Delete Comment"/>
<input type="submit" name="submit" value="Submit Comment"/>

然后,用PHP编写:

if(isset($_POST['delete'])) {
    header("Location: deletecomment.php");
}
if(isset($_POST['submit'])) {
    header("Location: comment.php");
}

但是,我仍然对您存储新旧评论的地方感到困惑。你是用MySQL做的吗?

答案 1 :(得分:0)

似乎我在if语句中出错了,else ifs和endifs应该在form标签内。