在HTML中的onclick函数参数中发送php变量在循环中不起作用

让我知道我错了。我一直在尝试在onclick函数参数中发送php变量,但是它不起作用。该参数在InsertReply函数中传递。 这是代码:

$sql22 = "SELECT * FROM comments WHERE PostID=$id";
$count22=$conect->query($sql22);
if($count22->num_rows>0){               //If comments exist
while($row = $count22->fetch_array()){      //Fetching Comments
    $mail = $row['UserMail'];
    $comment = $row['Comment'];
    $comment_id = $row['ID'];
     echo "<br>";
     echo "<p>".$comment_id."</p>";
    // Reply input field starts 
    ?>
     <input type='text' name='reply' id='reply' placeholder='Enter Reply here' min='5' max='100' class='big-input' style='width:40%;margin-left:40px;margin-right:40px;'>
 <?php
                    echo "<input type='hidden' name='mail' id='mail' value= '".$current_user."'/>";
                    echo "<input type='hidden' name='postid' id='postid' value= '".$id."'/>";
echo "<button class='btn btn-outline-info' onclick='InsertReply(<?php echo $comment_id ?>)'>Reply</button>";
}       
}

这是InsertReply函数:

function InsertReply(x) {
alert("Insert Reply Function Called! with comment id : " + x);
//Storing values in variables
var reply = document.getElementById("reply").value; 
var mail = document.getElementById("mail").value;
var p_id = document.getElementById("postid").value;
alert("Reply is "+reply);

alert("Reply will be added with Reply : " + reply + ",by : "+ mail + ",Post ID : " + p_id + " & Comment ID : "+x);

我知道代码有很多问题。由于我严重卡住,请指导我。让我知道是否还有其他要求。我只想将变量值发送到InsertReply函数。任何其他方法或建议将不胜感激。

lwb896148823 回答:在HTML中的onclick函数参数中发送php变量在循环中不起作用

echo "<button class='btn btn-outline-info' onclick='InsertReply(<?php echo $comment_id ?>)'>Reply</button>";

您已经在回显它,因此请传递这样的值

echo "<button class='btn btn-outline-info' onclick='InsertReply($comment_id)'>Reply</button>";
,

您不能在echo中使用<php>标签。像这样改变...

echo "<button class='btn btn-outline-info' onclick='InsertReply(".$comment_id.")'>Reply</button>";
本文链接:https://www.f2er.com/3104552.html

大家都在问