0

Is it possible to do something like this?

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

or

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

I want to do this so i can get a different submit button for all the posts, because i got a comment system, inside a posts system.. so on all the posts its a new submit button.

submitbutton is made like this:

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

i have checked that all the buttons gets a different name, so its just the other thing i dont get to work.

comment form:

 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:

  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);
    }
  }

complete setStatus Function with comment form inside:

 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>";
       }


   }
Mathias Hermansen
  • 261
  • 1
  • 5
  • 13

3 Answers3

1

Yes your first expression is correct. Check PHP concatenate for more information. Though I would use following to check form submit.

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

Additionally, I have had look on your code. I think the error is in form HTML.

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

It cause function to run 4 times and recording coment 4 times. Please replace action='#' or action='processComent.php' // location of process code.

Do the same in

<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>

also as same thing will happen while deleting.

Community
  • 1
  • 1
Narayan Bhandari
  • 426
  • 3
  • 11
  • I cannot use the setComments($conn) function? – Mathias Hermansen Mar 04 '17 at 02:36
  • 1
    Yes you can use setComments($conn) function. But, not as action="setComments($conn)". action attribute on HTML tag
    means it is the location of script that handle once form is submitted. Not the function name. For example: if you put the function setComments($conn) in processComment.php then, in tag
    . Hope this makes it clear. if not please check form "The Action Attribute" in https://www.w3schools.com/html/html_forms.asp
    – Narayan Bhandari Mar 04 '17 at 02:58
  • Yes it did! Sorry that i forgot to hit the "usefull answer" button ^^, Thank you again! – Mathias Hermansen Mar 05 '17 at 12:34
0

Yes, it's possible and your first variant is correct.

Thought a better idea might be make a separate form for each comment field and add the ID as a hidden input.

Something like this:

<form method="post" action="/something">
<textarea name="comment">
<input type="hidden" name="id" value="$row['ID']">
<button type="submit" name="submit"></button>
</form>
Alex
  • 4,674
  • 5
  • 38
  • 59
0

I can't really follow the logic in your code, but it seems as if you are over complicating the process... and i assume that your action was not actually pointing to that function as you show, but instead, is pointing to a page with that function ... anyway ... if you have this form

 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>";

and then when that form posts to setcomment.php, something simple like this would work ...

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);

  }