我制作了一个按钮,每次点击都会创建一个名称=1,2,3 ...的文本。我想将这些文本字段的所有输入存储在数据库中。
<?php
$con = mysqli_connect("localhost", "root","", "abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$maxoptions = 10;
// I don't want only 10 inputs from text fields
// but as many as the user creates and fills
for ($i = 1; $i < $maxoptions; $i++) {
$sql="INSERT INTO qa (q, a$i)
VALUES
('$_POST[q1]', '$_POST[i]')";
// '$_POST[i]' is not working
}
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
现在,如何使用这些文本字段在数据库中动态创建列?
这是我用来创建文本字段的 JavaScript 代码:
var intTextBox1 = 0;
//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement1()
{
intTextBox1 = intTextBox1 + 1;
var contentID = document.getElementById('content1');
var newTBDiv = document.createElement('div');
newTBDiv.setAttribute('id','strText'+intTextBox1);
newTBDiv.innerHTML = "Option" + intTextBox1 +
": <input type='text' id='" + intTextBox1 +
"' name='" + intTextBox1 + "'/>";
contentID.appendChild(newTBDiv);
}
//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement1()
{
if (intTextBox1 != 0)
{
var contentID = document.getElementById('content1');
contentID.removeChild(document.getElementById('strText'+intTextBox1));
intTextBox1 = intTextBox1 - 1;
}
}
这是按钮的代码:
<form id="s1form" name="s1form" method="post" action="qno1.php">
<input type="text" name="q1">
<input type="button" value="Add a choice" onClick="javascript:addElement1();" />
<input type="button" value="Remove a choice" onClick="javascript:removeElement1();" />
<div id="content1"></div>