是否可以在isset $ _post ['submit']中添加变量

时间:2017-03-03 01:19:42

标签: php if-statement isset

是否可以做这样的事情?

 if (isset($_POST['Submit_'.$_POST['ID']])) {
}

if (isset($_POST['Submit_' + $_POST['ID']])) {
    }

我想这样做,所以我可以为所有帖子获得一个不同的提交按钮,因为我在帖子系统中有一个评论系统..所以在所有帖子上它是一个新的提交按钮。

submitbutton是这样的:

echo "<button type='submit' class='commentbtn' name='commentSubmit_".$row['ID']."'></button>

我已经检查过所有的按钮都有不同的名字,所以它只是我不能工作的另一件事。

评论表:

 echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
                    <textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
                    <input type='hidden' name='uname' value='".$_SESSION['name']."'>
                    <input type='hidden' name='uid' value='".$_SESSION['id']."'>
                    <input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
                    <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
                    <input type='hidden' name='sid' value='".$row1['sid']."'>
                    <button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
    <img src='images/comment-icon.png'  height='24'' alt='comment' title='Comment''  />
</button></form></div>";

setComment功能:

  function setComment($conn) {
    if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
        $uname = $_POST['uname'];
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $comment = $_POST['commentText'];
        $uimg = $_POST['uimg'];
        $sid = $_POST['sid'];

        $sql = "INSERT INTO status_comments (uid, sid, uname, comment, uimg, date) VALUES ('$uid', '$sid', '$uname', '$comment', '$uimg', '$date')";
        $result = mysqli_query($conn, $sql);
    }
  }

完整的setStatus函数,其中包含注释表单:

 function getStatus($conn) {
       $sql = "SELECT * FROM status ORDER BY sid DESC";
       $query = mysqli_query($conn, $sql);
       while ($row1 = $query->fetch_assoc()) {
           echo "<div class='commentbox'>";
           echo "<div class='commentimg'><img src='images/".$row1['commentimg']."' width='50px'></div>";
           echo "<div class='namedate'>";
           echo $row1['uidname']."<br>";
           echo "<div class='commdate'>".$row1['date']."<br><br></div></div><hr>";
           echo "<div class='statusmessage'><p>".nl2br($row1['message'])."</p></div>";
           echo "<div class='statusimage'><img src='userimages/".$row1['status_image']."'></div>";
           echo "<div class='likerow'>";

           $result = mysqli_query($conn, "SELECT * FROM status_like WHERE uid=".$_SESSION['id']." and sid=".$row1['sid']."");
                if (mysqli_num_rows($result) == 1) {
                    echo "<span><a href='' class='unlike' id='".$row1['sid']."'><div class='unlike-btn'><img src='images/dislike.png'></div></a><p>| ".$row1['likes']."  likes!</p></span>";
                } else {
                    echo "<span><a href='' class='like' id='".$row1['sid']."'><div class='like-btn'><img src='images/like.png'></div></a><p>| ".$row1['likes']."  likes!</p></span>";
                }
           echo "</div>";    

           echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
                    <textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
                    <input type='hidden' name='uname' value='".$_SESSION['name']."'>
                    <input type='hidden' name='uid' value='".$_SESSION['id']."'>
                    <input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
                    <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
                    <input type='hidden' name='sid' value='".$row1['sid']."'>
                    <button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
    <img src='images/comment-icon.png'  height='24'' alt='comment' title='Comment''  />
</button></form></div>";

           if ($_SESSION['id'] === $row1['uid'] || $_SESSION['usertype'] === 'topadmin'){
           echo "<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>

                   <input type='hidden' name='sid' value='".$row1['sid']."'>
                   <input type='hidden' name='sid' value='".$row1['sid']."'>
                   <button type='submit' title='Delete Status' name='statusDelete'>X</button>
                </form>
                <form class='edit-form' method='POST' action='editcomment.php'>
                   <input type='hidden' name='sid' value='".$row1['sid']."'>
                   <input type='hidden' name='uid' value='".$row1['uid']."'>
                   <input type='hidden' name='date' value='".$row1['date']."'>
                   <input type='hidden' name='message' value='".$row1['message']."'>
                   <button type='submit' style='border: 0; background: transparent'>
    <img src='images/edit-icon.png'  height='10'' alt='edit' title='Edit Status''  />
</button>


                </form>";
           }
           echo "</div>";
       }


   }

3 个答案:

答案 0 :(得分:1)

是的,你的第一个表达是正确的。有关详细信息,请查看PHP concatenate。虽然我会使用following来检查表单提交。

// check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    // .... your code
}

此外,我已经查看了您的代码。我认为错误是HTML格式。

echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>

它使功能运行4次并记录4次。请替换action =&#39;#&#39;或者行动=&#39; processComent.php&#39; //进程代码的位置。

中做同样的事情
<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>

同样的事情也会在删除时发生。

答案 1 :(得分:0)

是的,这是可能的,您的第一个变体是正确的。

以为更好的想法可能是为每个评论字段创建一个单独的表单,并将ID添加为隐藏的输入。

这样的事情:

<form method="post" action="/something">
<textarea name="comment">
<input type="hidden" name="id" value="$row['ID']">
<button type="submit" name="submit"></button>
</form>

答案 2 :(得分:0)

我无法真正遵循你的代码中的逻辑,但似乎你的过程使你的过程变得复杂......而且我认为你的行动实际上并没有像你所展示的那样指向那个功能,而是指向具有该功能的页面......无论如何......如果你有这个表格

 echo "
<div class='commentform'>
    <form id='comment_form_".$row1['sid']."' action='setcomment.php' method='POST'>
        <textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
        <input type='hidden' name='uname' value='".$_SESSION['name']."'>
        <input type='hidden' name='uid' value='".$_SESSION['id']."'>
        <input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
        <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
        <input type='hidden' name='sid' value='".$row1['sid']."'>
        <button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
            <img src='images/comment-icon.png'  height='24'' alt='comment' title='Comment''  />
        </button>
    </form>
</div>";

然后当该表单发布到setcomment.php时,像这样简单的东西会起作用......

if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
     setComment($conn);  
}
 function setComment($conn) {

        $uname = $_POST['uname'];
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $comment = $_POST['commentText'];
        $uimg = $_POST['uimg'];
        $sid = $_POST['sid'];

        $sql = "
            INSERT INTO 
                status_comments 
            (
                uid, sid, uname, comment, uimg, date
            ) 
            VALUES 
            (
                '".mysqli_real_escape_string($conn,$uid)."', 
                '".mysqli_real_escape_string($conn,$sid)."', 
                '".mysqli_real_escape_string($conn,$uname)."', 
                '".mysqli_real_escape_string($conn,$comment)."', 
                '".mysqli_real_escape_string($conn,$uimg)."', 
                '".mysqli_real_escape_string($conn,$date)."'
            )";
        $result = mysqli_query($conn, $sql);

  }