我创建了一个动态添加和删除文本框,连续4个文本框。现在我希望每个文本框中的数据一行保存到数据库中;但在不同的属性下。我已经尝试了只有两个文本框,它的工作原理。但是在4个文本框的情况下,保存在数据库中的数据是错误的。每次按下添加文本框按钮时,将显示另外4个文本框,这4个文本框中的数据将保存在另一行下。 继承我的代码,请告诉我什么是错的。
使用Javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#append").click(function (e) {
e.preventDefault();
var textboxes = $(".textbox").length;
$(".inc").append("<div class='controls'><input class='textbox form-control' type='text' name='textbox[]'><input class='textbox form-control' type='text' name='box[]' ><input class='textbox form-control' type='text' name='box1[]' ><input class='textbox form-control' type='text' name='box2[]' ><a href='#' class='remove_this btn btn-danger'>remove</a> </div>");
});
$(document).on('click', '.remove_this', function (e) {
e.preventDefault();
$(this).parent().remove();
});
});
</script>
Html:
<form class="form-horizontal" method= "POST">
<div class="control-group">
<div class="inc">
<div class="controls">
<button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
Add Textbox</button>
<br>
<br>
</div>
</div>
<input type="submit" class="btn btn-info" name="submit" value="submit"/>
</div>
PHP:
$host = "localhost";
$dbname = "lala";
$user = "root";
$pass = "";
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
if (isset($_POST['submit'])) {
$textboxes = $_POST['textbox'];
$textboxes1 = $_POST['box1'];
$textboxes2 = $_POST['box2'];
$boxes = $_POST['box'];
$x=0;
$a=0;
$z=0;
foreach ($textboxes as $textbox ) {
$y=0;
if($y==$x){
foreach ($boxes as $box ) {
if($z==$y){
foreach ($textboxes1 as $box1 ) {
if($a==$z){
foreach ($textboxes as $box2 ) {
if($y==$x){
$sql = "INSERT into sasa (sasa,sasa1,sasa2,sasa3) values('$textbox','$box','$box1','$box2')";
echo $sql . "<br>";
$q = $conn->query($sql);
}
$x++;
$y++;
}
}
$a++;
}
}
$z++;
}
}
}
}
答案 0 :(得分:1)
不需要多个嵌套
forEach
。
试试这个:
$(document).ready(function() {
$("#append").click(function(e) {
e.preventDefault();
var textboxes = $(".textbox").length;
$(".inc").append("<div class='controls'><input class='textbox form-control' type='text' name='textbox[]'><input class='textbox form-control' type='text' name='box[]' ><input class='textbox form-control' type='text' name='box1[]' ><input class='textbox form-control' type='text' name='box2[]' ><a href='#' class='remove_this btn btn-danger'>remove</a> </div>");
});
$(document).on('click', '.remove_this', function(e) {
e.preventDefault();
$(this).parent().remove();
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form class="form-horizontal" method="POST">
<div class="control-group">
<div class="inc">
<div class="controls">
<button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
Add Textbox</button>
<br>
<br>
</div>
</div>
<input type="submit" class="btn btn-info" name="submit" value="submit" />
</div>
&#13;
<强> PHP:强>
<?php
$host = "localhost";
$dbname = "lala";
$user = "root";
$pass = "";
$conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass);
if (isset($_POST['submit'])) {
$textboxes = $_POST['textbox'];
$textboxes1 = $_POST['box1'];
$textboxes2 = $_POST['box2'];
$boxes = $_POST['box'];
$x=0;
$a=0;
$z=0;
foreach ($textboxes as $key => $textbox ) {
$box=$textboxes[$key];
$box1=$textboxes1[$key];
$box2=$textboxes2[$key];
$sql = "INSERT into sasa (sasa,sasa1,sasa2,sasa3) values('$textbox','$box','$box1','$box2')";
echo $sql . "<br>";
$q = $conn->query($sql);
}
}
?>