0

Get information from database

$video_id = $_GET['id'];
$query = "SELECT * FROM video_comments WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindValue(':video_id',$video_id);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$id = $row['id']; // The id for each comment

// Show/Hide Script
echo '<script>function replyToCommentFunction'.$id.'() {
var x = document.getElementById("replyToComment'.$id.'");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}</script>';

<button name="replyToComment" class="videoCommentsReplyButton" onClick="replyToCommentFunction'.$id.'()">Reply</button>

// The Div to show...
echo '<div id="replyToComment'.$id291.'">';
echo 'Hello, World!';
echo '</div>';

}

I thought I had this working. But now, only one of the "reply" buttons work on the page. The most recent reply button refreshes the page, which confuses me, because no script or action exists, which would tell it to submit a form or refresh the page. I would appreciate any assistance. Thank You.

2 Answers2

0

Why are you creating a lot of functions when you can just create one and make all the buttons work with it?

Add this code on the end of the page:

<script type="text/javascript">
function replyToCommentFunction(x) {
    if(x.style.display === "block") {
        x.style.display = "none";
    } else {
        x.style.display = "block";
    }
}
</script>

Edit the button to this:

<button name="replyToComment" class="videoCommentsReplyButton" onClick="replyToCommentFunction(this);">Reply</button>
  • How does the function understand which DIV to expand if all the DIV's on the page has the same id value, which is actually not even allowed. – Donald Faulknor Jan 04 '20 at 02:02
  • Yes, the **(this)** will send the element being clicked to the function. The function doesn't catch the element by id, that why I'd deleted this line. The **x** on your function already is the element, so "getElementById" isn't necessary anymore. ;) – William Carneiro Jan 04 '20 at 02:04
  • ok, but still confused a little I have this from you. `one function` `div a`, `div b`, `div c`. `button a`, `button b`, `button c`. Information passes from button a to the only function. Then from the function to, which div? What exactly tells the button which div should expand since id is not necessary anymore. Something has to identify the div being expanded, right? – Donald Faulknor Jan 04 '20 at 02:08
  • You can do that on the button onclick attribute. ``, then you can get the div element with the variable x on your function by ID. `document.getElementById("replyToComment" + x)`. – William Carneiro Jan 04 '20 at 02:17
  • ahh, I see. Thank you very much. I always thought it was improper to while loop the scripts. I just didn't know another way to do it until now. Thank you. – Donald Faulknor Jan 04 '20 at 02:44
-1

Can you try to add type="button" to button that refreshes the page?

maciejze
  • 177
  • 7