I'm doing something wrong and I don't know what's that. I'm trying to fetch text from input field through id, and the input field is within a while loop as there are multiple input fields. Here is my code:
if($count22->num_rows>0){
while($row = $count22->fetch_array()){
$mail = $row['UserMail'];
$comment = $row['Comment'];
$comment_id = $row['ID'];
// Reply input field starts
echo "<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;'>";
echo "<button class='btn btn-outline-info' onclick='InsertReply($comment_id)'>Reply</button>";
}
}
On clicking button, InsertReply function is called where input field is checked whether it is empty or not.
InsertReply() function:
function InsertReply(x) {
alert("Insert Reply Function Called! with comment id : " + x);
//Storing values in variables
var reply = document.getElementById("reply").value; //Error Here, not accepting reply
var mail = document.getElementById("mail").value;
var p_id = document.getElementById("postid").value;
if (reply.length == 0) { //If user has entered nothing
alert("You Entered Nothing!"); //Show message
}
}
The issue is that it is fetching text from only first input field, it is showing alert box 'You entered nothing' for the rest of the input fields even if I enter text. Let me know if you have any queries, any alternative solution/suggestion will be highly appreciated!