[英]Input type='text' value not fetched through id in a loop
我做错了什么,我不知道那是什么。 我正在尝试通过 id 从输入字段中获取文本,并且输入字段在 while 循环内,因为有多个输入字段。 这是我的代码:
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>";
}
}
单击按钮时,将调用 InsertReply function 来检查输入字段是否为空。
插入回复() 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
}
}
问题是它仅从第一个输入字段中获取文本,即使我输入文本,它也会为输入字段的 rest 显示警告框“您没有输入任何内容”。 如果您有任何疑问,请告诉我,任何替代解决方案/建议将不胜感激!
如果您需要处理多个name
输入,则应在其名称后添加[]
以便在提交表单时保持所有输入。
尝试这个:
echo " <input type='text' name='reply[$comment_id]' id='reply_$comment_id' placeholder='Enter Reply here' min='5' max='100' class='big-input' style='width:40%;margin-left:40px;margin-right:40px;'>";
echo "<input type='hidden' name='mail[$comment_id]' id='mail_$comment_id' value= '".$current_user."'/>";
echo "<input type='hidden' name='postid[$comment_id]' id='postid_$comment_id' value= '".$id."'/>";
在接收表单数据的 PHP 页面中,您会将它们作为数组获取,例如:
$_POST['reply'][here the comment id]
$_POST['mail'][here the comment id]
$_POST['postid'][here the comment id]
要知道所有输入数据,请执行print_r($_POST)
(如果帖子有method='get'
然后使用$_GET
变量。
另请注意,我为每个id
属性添加了 id ,因为在整个 HTML 文档中,所有id
都必须是唯一的。
function InsertReply(x) {
alert("Insert Reply Function Called! with comment id : " + x);
//Storing values in variables
var reply = document.getElementById("reply_" + x).value; //Error Here, not accepting reply
var mail = document.getElementById("mail_" + x).value;
var p_id = document.getElementById("postid_" + x).value;
if (reply.length == 0) { //If user has entered nothing
alert("You Entered Nothing!"); //Show message
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.