输入类型=“文本”值未在循环中通过ID提取

我做错了什么,我不知道那是什么。我正在尝试通过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函数,其中将检查输入字段是否为空。

InsertReply()函数:

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
    }
}

问题在于,它仅从第一个输入字段获取文本,即使我输入了文本,对于其余的输入字段,它也会显示警告框“您未输入任何内容”。 让我知道如果您有任何疑问,任何其他解决方案/建议将不胜感激!

maomao2222221 回答:输入类型=“文本”值未在循环中通过ID提取

如果您需要使用同一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
    }
}
本文链接:https://www.f2er.com/3104011.html

大家都在问