I'm having a problem submitting only one of the forms at my site.
The forms are being created dynamic through a foreach loop.
Every time i'm submitting one form, it submits all of them, so if there is 10 posts then they have a reply form each, and all it inserted to the database.
<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
?>
As far as I am aware the submit button will only submit the form that it is in. In fact it is very hard to submit more than one form at a time.
I suspect the problem is that the form submits the reply and that it is not bound to a current comment and ends up getting added to all of them.
I suggest that you start with something like this:
<?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>
Then your PHP
can make SQL
that pins the comment down to one parent comment. Of course you are forcing me to guess because there is no way what you have posted is the entire code.