在 Sample.php 中我有
<html>
<head>
<title>Test</title>
</head>
<script language="javascript">
var count = 1;
function addRow(divId) {
var parentDiv = document.getElementById(divId);
var eleDiv = document.createElement("div");
eleDiv.setAttribute("name", "olddiv");
eleDiv.setAttribute("id", "olddiv");
var eleText = document.createElement("input");
eleText.setAttribute("type", "text");
eleText.setAttribute("name", 'textbox' + count);
eleText.setAttribute("id", "textbox" + count);
var eleBtn = document.createElement("input");
eleBtn.setAttribute("type", "button");
eleBtn.setAttribute("value", "delete");
eleBtn.setAttribute("name", "button");
eleBtn.setAttribute("id", "button");
eleBtn.setAttribute("onclick", "deleteRow('button')");
parentDiv.appendChild(eleDiv);
eleDiv.appendChild(eleText);
eleDiv.appendChild(eleBtn);
var golu=count.toString();
document.getElementById("h").value= golu;
count++;
}
function deleteRow(tableID) {
var div = document.getElementById('olddiv');
if (div) {
div.parentNode.removeChild(div);
}
var golu=count.toString();
document.getElementById("h").value= golu;
}
var golu=count.toString();
document.getElementById("h").value= golu;
</script>
<body>
<form action="Page.php" method="post">
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<div id="dataTable"><INPUT type="text" name="textbox0" id="textbox0"/></div>
<input type"hidden" id="h" name="h" value="0"/>
<button type="submit" name="submit" id="submit">Submit</button>
</form>
</body>
</html>
在 Page.php 中我有
<?php
include_once('db.php');
$x=$_POST["h"];
$y=intval($x);
$z=0;
while($z<=$y){
$var[z]=$_POST['textbox'][$z];
echo "$var[$z]";
$sql="INSERT into Data values('".$var[z]."');";
$query = mysql_query($sql);
}
?>
我的问题:
我的目标是接收textbox0、textbox1等的所有值。通过在 page.php 中使用循环。
但是我无法得到想要的结果。实际上提交后,Pape.php 显示为空。收到全部后,我还希望它们存储在MySQL数据库中。
每当我删除文本框时,文本框 ID 的序列都不会更改。
请告诉我该怎么做。
最佳答案
您可以按照以下方式执行此操作。
每当您使用 JavaScript 或 jQuery 创建文本框时,都会维护文本框的计数,假设 HTML 上默认有两个文本框,因此将该计数存储到隐藏字段中,就像您所做的那样:
<input type"hidden" id="h" name="h" value="0"/>
然后尝试这个,您正在以错误的方式读取值:
不要使用 $var[z]=$_POST['textbox'][$z];
使用 $var[z]=$_POST['textbox'.$z ];
.
我认为不必编辑每个文本框 id
值,只需将其从 HTML 中删除并 checkin PHP 代码即可:
<?php
include_once('db.php');
$x=$_POST["h"];
$y=intval($x);
$z=0;
while($z<=$y){
if(isset($_POST['textbox'.$z]) && !empty($_POST['textbox'.$z])){
$var[z]=$_POST['textbox'.$z];
echo "$var[$z]";
$sql="INSERT into the Data values('".$var[z]."');";
$query=mysql_query($sql);
}
}
?>
另一种方法可以解决您的两个问题:)
测试.html:
<html>
<title>TEST</title>
<body>
<form action="test.php" method="post">
<input type="text" name="demo[]" value=""/>
<input type="text" name="demo[]" value=""/>
<input type="text" name="demo[]" value=""/>
<input type="text" name="demo[]" value=""/>
<input type="text" name="demo[]" value=""/>
<input type="submit">
</form>
</body>
</html>
测试.php:
print_r($_POST);
exit;
输出:
Array ( [demo] => Array ( [0] => zxc [1] => zxc [2] => ewe [3] => ecc [4] => zzx ) )
关于javascript - 如何在 php 上获取动态创建的文本框的输入并使用循环将它们存储在 MySQL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971127/