0

Let me know where I'm wrong. I've been trying to send php variable within a onclick function argument but its not working. The argument is passed in InsertReply function. Here is the code:

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

Here is the InsertReply Function:

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

I know there are lots of issues with the code. Please guide me as I'm badly stuck. Let me know if anything else is required. I just want to send variable value to the InsertReply function. Any alternate method or suggestion will be highly appreciated.

2 Answers 2

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

You are already echoing it so pass the value like this

echo "<button class='btn btn-outline-info' onclick='InsertReply($comment_id)'>Reply</button>";
Sign up to request clarification or add additional context in comments.

9 Comments

Now it is saying that the input field is empty, even when I entered the text
i posted the same answer earlier... this is sad
change'InsertReply($comment_id)' to 'InsertReply(\"$comment_id\")'>
down vote is for invalid answers, this is not a race or competition, we all are here to help each other out. Thanks.
I appreciate your answer but please don't downvote any other answer without any valid reason
|
2

You cannot use <php> tag inside echo. change like this...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.