我有一个表单,用户可以根据需要增加或添加字段数量。
字段添加成功.....
为此我使用javascript,代码在这里..
<script type="application/javascript">
var counter3 = 0;
function addAuthor() {
// Get the main Div in which all the other divs will be added
var mainContainer = document.getElementById('Author');
// Create a new div for holding text and button input elements
var newDiv = document.createElement('div');
// Create a new text input
var newText = document.createElement('input');
newText.type = "text";
//var i = 1;
newText.name = "Author[]";
//newText.value = counter3 + 2 + ". ";
//Counter starts from 2 since we already have one item
//newText.class = "input.text";
// Create a new button input
var newDelButton = document.createElement('input');
newDelButton.type = "button";
newDelButton.value = "-";
// Append new text input to the newDiv
newDiv.appendChild(newText);
// Append new button input to the newDiv
newDiv.appendChild(newDelButton);
// Append newDiv input to the mainContainer div
mainContainer.appendChild(newDiv);
counter3++;
//i++;
// Add a handler to button for deleting the newDiv from the mainContainer
newDelButton.onclick = function() {
mainContainer.removeChild(newDiv);
counter3--;
}
}
</script>
我在这里有一个表格..
<form method="post" action="">
<input type = "text" name="Author[]" />
<input type="button" value="+" id="Authorbutton" onclick="addAuthor()" />
<input type="submit" value="submit" name="submit">
</form>
我使用 php 和 mysql 查询将数据保存到数据库中.. 代码代码是..
<?php
if(isset($_POST['submit']))
{
$authors = $_POST['Author'];
foreach($authors as $author)
{
$sql=mysql_query("INSERT INTO test (number, type) VALUES ('{$_POST['type']}','{$author}')");
if (!$sql){
die('Error: ' . mysql_error());
}
//endforeach;
}
}
?>
但问题是数据没有正确存储在数据库中......
它只保存数据库中的第一个字段,但不保存第二个、第三个、第四个等......
帮我 ........