I am currently working on a forum where I use multiple forms on one page with the same ID. So when I try to get the input through document.getElementById("#id").val it will always select the first form on the website. I generate these forms so I can't change the IDs separately.
So what I tried to do is pass parameters to the jQuery function like this:
echo '<form id="delete-form" method="POST" action="javascript:deleteComment(comment_id_java.val, category_id_java.val, category_java.val)">';
echo '<input id="comment_id_java" type="hidden" name="id" value="'.$result_row['comment_id'].'">';
echo '<input id="category_id_java" type="hidden" name="urlid" value="'.$_GET['id'].'">';
echo '<input id="category_java" type="hidden" name="category" value="'.$_GET['category'].'">';
echo '<input id="delete-button" type="submit" name="submit" value="X">';
echo '</form>';
function deleteComment(comment_id_java, category_id_java, category_java){
//Don't worry about this part, this works for sure I tested it.
$.post("delete-comment.php", {
id: comment_id,
urlid: category_id,
category: s_category
});
$("#comment-container").load("load-comments.php", {category:s_category, id:category_id});
}
This doesn't seem to work though because I keep getting variable undefined error.
What am I doing wrong?
EDIT:
This is the actual HTML code:
<div id="comment-container">
<div class="post-section">
<div class="post-user-section">
<h4>fiddel</h4>
<h4 style="color:rgb(70,70,70);">User</h4>
<img src="Images/profile-picture.png">
<h4 style="color:rgb(180,180,180);">18-05-22</h4>
<h4 style="color:rgb(180,180,180);">Reputation: 0</h4>
</div>
<div class="post-body-section-wide">
<h5>Welcome User,
<br>
<br>This is the Announcement Section where we post any types of news!
</h5>
</div>
<form id="vote-form" method="POST"><input type="hidden" name="id" value="17"><input id="upvote-button" type="submit" name="submit" value="🞁"><input id="downvote-button" type="submit" name="submit" value="🞃"></form>
</div>
<div class="post-section">
<div class="post-user-section">
<h4>fiddel</h4>
<h4 style="color:rgb(70,70,70);">User</h4>
<img src="Images/profile-picture.png">
<h4 style="color:rgb(180,180,180);">18-05-24</h4>
<h4 style="color:rgb(180,180,180);">Reputation: 0</h4>
</div>
<div class="post-body-section-wide">
<h5>ddadas</h5>
</div>
<form id="vote-form" method="POST"><input type="hidden" name="id" value="21"><input id="upvote-button" type="submit" name="submit" value="🞁"><input id="downvote-button" type="submit" name="submit" value="🞃"></form>
</div>
<div class="post-section">
<div class="post-user-section">
<h4>fiddel</h4>
<h4 style="color:rgb(70,70,70);">User</h4>
<img src="Images/profile-picture.png">
<h4 style="color:rgb(180,180,180);">18-05-25</h4>
<h4 style="color:rgb(180,180,180);">Reputation: 0</h4>
</div>
<div class="post-body-section-wide">
<h5>Why is this not locked LOL</h5>
</div>
<form id="vote-form" method="POST"><input type="hidden" name="id" value="33"><input id="upvote-button" type="submit" name="submit" value="🞁"><input id="downvote-button" type="submit" name="submit" value="🞃"></form>
</div>
<div class="post-section">
<div class="post-user-section">
<h4>JasonB</h4>
<h4 style="color:rgb(255,23,41);">Owner</h4>
<img src="Images/profile-picture.png">
<h4 style="color:rgb(180,180,180);">18-05-29</h4>
<h4 style="color:rgb(180,180,180);">Reputation: 0</h4>
</div>
<div class="post-body-section">
<h5>fdfsd</h5>
</div>
<form id="delete-form" method="POST" action="javascript:deleteComment(comment_id_java.value, category_id_java.value, category_java.value)"><input id="comment_id_java" type="hidden" name="id" value="83"><input id="category_id_java" type="hidden" name="urlid" value="17"><input id="category_java" type="hidden" name="category" value="announcements"><input id="delete-button" type="submit"
name="submit" value="X"></form>
</div>
<div class="post-section">
<div class="post-user-section">
<h4>JasonB</h4>
<h4 style="color:rgb(255,23,41);">Owner</h4>
<img src="Images/profile-picture.png">
<h4 style="color:rgb(180,180,180);">18-05-29</h4>
<h4 style="color:rgb(180,180,180);">Reputation: 0</h4>
</div>
<div class="post-body-section">
<h5>asasf</h5>
</div>
<form id="delete-form" method="POST" action="javascript:deleteComment(comment_id_java.value, category_id_java.value, category_java.value)"><input id="comment_id_java" type="hidden" name="id" value="84"><input id="category_id_java" type="hidden" name="urlid" value="17"><input id="category_java" type="hidden" name="category" value="announcements"><input id="delete-button" type="submit"
name="submit" value="X"></form>
</div>
</div>
var forms = document.querySelector("form");Then you can iterate over them.