繁体   English   中英

循环中未通过 id 获取输入 type='text' 值

[英]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
    }
}

<input type="“text”" assign id value to php string< div><div id="text_translate"><p> 我有一个模式表单将员工工作时间戳写入 SQL 数据库。 I select 从下拉菜单中选择员工,并使用 id 标签填写 javascript function 姓名和姓氏输入框。 当我提交表单时,数据使用 php 写入数据库。 问题是我无法将 javascript 字符串转换为 php 字符串或将输入标签的 id 标签分配给 php 字符串。 我知道在输入中写入我可以用 php value="...." 分配值。</p><pre> $nome = '&lt;script&gt;document.writeln(a);&lt;/script&gt;';</pre><p> 不工作</p><p>这里的代码</p><pre>&lt;div class="modal-body"&gt; &lt;input type="hidden" name="edit_id" value="&lt;?php echo $id; ?&gt;"&gt; &lt;div class="form-group"&gt; &lt;label class="control-label col-sm-2" for="dipendente"&gt;Dipendente:&lt;/label&gt; &lt;div class="col-sm-4"&gt; &lt;select id="dipendente" name="dipendente" onchange="nomeCognome()"&gt; &lt;option value="dipendente"&gt;Seleziona dipendente&lt;/option&gt; &lt;?php $sqlOperatore="SELECT nome, cognome FROM login ORDER BY nome ASC"; $resultOperatore = $conn-&gt;query($sqlOperatore); if ($resultOperatore-&gt;num_rows &gt; 0) { while($row = $resultOperatore-&gt;fetch_assoc()) { $nome = $row['nome']; $cognome = $row['cognome']; $dipendente = $nome. ' '. $cognome; ?&gt; &lt;option&gt;&lt;?php echo $dipendente; ?&gt;&lt;/option&gt; &lt;?php }?&gt; &lt;/select&gt; &lt;/div&gt; &lt;?php }?&gt; &lt;label class="control-label col-sm-2" for=" "&gt; &lt;/label&gt; &lt;div class="col-sm-4"&gt;&lt;/div&gt; &lt;/div&gt; &lt;br&gt; &lt;div class="form-group"&gt; &lt;label class="control-label col-sm-2" for="nome"&gt;Nome:&lt;/label&gt; &lt;div class="col-sm-4" id="nome"&gt; &lt;input type="text" class="form-control" id="nome" name="nome" readonly&gt; &lt;/div&gt; &lt;label class="control-label col-sm-2" for="cognome"&gt;Cognome:&lt;/label&gt; div class="col-sm-4" id="cognome"&gt; &lt;input type="text" class="form-control" id="cognome" name="cognome" readonly&gt; &lt;/div&gt; &lt;/div&gt; &lt;br&gt; &lt;script type="text/javascript"&gt; function nomeCognome() { var x = document.getElementById("dipendente").value; var [a, b, c] = x.split(' '); document.getElementById("nome").innerHTML = [a]; document.getElementById("cognome").innerHTML = [b, c].join(' '); } &lt;/script&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button type="submit" class="btn btn-primary" name="add_item"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt; Aggiungi&lt;/button&gt; &lt;button type="button" class="btn btn-warning" data-dismiss="modal"&gt;&lt;span class="glyphicon glyphicon-remove-circle"&gt;&lt;/span&gt; Annulla&lt;/button&gt; &lt;/div&gt; &lt;?php //Add Matricola if(isset($_POST['add_item'])){ $edit_id = $_POST['edit_id']; $nome = $_POST['nome']; $cognome = $_POST['cognome']; $sql = "INSERT INTO gest_personale.timbratura (nome, cognome) VALUES ('$nome', '$cognome')"; if ($conn-&gt;query($sql) === TRUE) { echo '&lt;script&gt;window.location.href="visualizzaTimbrature.php"&lt;/script&gt;'; } else { echo "Errore: ". $sql. "&lt;br&gt;". $conn-&gt;error; } }?&gt;</pre><p> 我收到以下错误/通知:注意:未定义的索引:nome 和注意:未定义的索引:cognome</p></div>

[英]<input type=“text” assign id value to php string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历列表元素并将文本添加到jQuery中的输入值 如何在javascript中打印输入文本类型id的值 通过使用名称而不是javascript中的ID获取输入类型文本的值 <input type="“text”" assign id value to php string< div><div id="text_translate"><p> 我有一个模式表单将员工工作时间戳写入 SQL 数据库。 I select 从下拉菜单中选择员工,并使用 id 标签填写 javascript function 姓名和姓氏输入框。 当我提交表单时,数据使用 php 写入数据库。 问题是我无法将 javascript 字符串转换为 php 字符串或将输入标签的 id 标签分配给 php 字符串。 我知道在输入中写入我可以用 php value="...." 分配值。</p><pre> $nome = '&lt;script&gt;document.writeln(a);&lt;/script&gt;';</pre><p> 不工作</p><p>这里的代码</p><pre>&lt;div class="modal-body"&gt; &lt;input type="hidden" name="edit_id" value="&lt;?php echo $id; ?&gt;"&gt; &lt;div class="form-group"&gt; &lt;label class="control-label col-sm-2" for="dipendente"&gt;Dipendente:&lt;/label&gt; &lt;div class="col-sm-4"&gt; &lt;select id="dipendente" name="dipendente" onchange="nomeCognome()"&gt; &lt;option value="dipendente"&gt;Seleziona dipendente&lt;/option&gt; &lt;?php $sqlOperatore="SELECT nome, cognome FROM login ORDER BY nome ASC"; $resultOperatore = $conn-&gt;query($sqlOperatore); if ($resultOperatore-&gt;num_rows &gt; 0) { while($row = $resultOperatore-&gt;fetch_assoc()) { $nome = $row['nome']; $cognome = $row['cognome']; $dipendente = $nome. ' '. $cognome; ?&gt; &lt;option&gt;&lt;?php echo $dipendente; ?&gt;&lt;/option&gt; &lt;?php }?&gt; &lt;/select&gt; &lt;/div&gt; &lt;?php }?&gt; &lt;label class="control-label col-sm-2" for=" "&gt; &lt;/label&gt; &lt;div class="col-sm-4"&gt;&lt;/div&gt; &lt;/div&gt; &lt;br&gt; &lt;div class="form-group"&gt; &lt;label class="control-label col-sm-2" for="nome"&gt;Nome:&lt;/label&gt; &lt;div class="col-sm-4" id="nome"&gt; &lt;input type="text" class="form-control" id="nome" name="nome" readonly&gt; &lt;/div&gt; &lt;label class="control-label col-sm-2" for="cognome"&gt;Cognome:&lt;/label&gt; div class="col-sm-4" id="cognome"&gt; &lt;input type="text" class="form-control" id="cognome" name="cognome" readonly&gt; &lt;/div&gt; &lt;/div&gt; &lt;br&gt; &lt;script type="text/javascript"&gt; function nomeCognome() { var x = document.getElementById("dipendente").value; var [a, b, c] = x.split(' '); document.getElementById("nome").innerHTML = [a]; document.getElementById("cognome").innerHTML = [b, c].join(' '); } &lt;/script&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button type="submit" class="btn btn-primary" name="add_item"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt; Aggiungi&lt;/button&gt; &lt;button type="button" class="btn btn-warning" data-dismiss="modal"&gt;&lt;span class="glyphicon glyphicon-remove-circle"&gt;&lt;/span&gt; Annulla&lt;/button&gt; &lt;/div&gt; &lt;?php //Add Matricola if(isset($_POST['add_item'])){ $edit_id = $_POST['edit_id']; $nome = $_POST['nome']; $cognome = $_POST['cognome']; $sql = "INSERT INTO gest_personale.timbratura (nome, cognome) VALUES ('$nome', '$cognome')"; if ($conn-&gt;query($sql) === TRUE) { echo '&lt;script&gt;window.location.href="visualizzaTimbrature.php"&lt;/script&gt;'; } else { echo "Errore: ". $sql. "&lt;br&gt;". $conn-&gt;error; } }?&gt;</pre><p> 我收到以下错误/通知:注意:未定义的索引:nome 和注意:未定义的索引:cognome</p></div> 如何在输入类型文本中以id的形式显示结果数据? jQuery模式对话框通过id获取输入类型文本的值 如何循环输入类型文本? 获取输入文本值的ID 通过循环ID为ID的jQuery值整数 通过函数传递来自输入 ID 的值
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM