我正在动态地向表单添加文本字段,但现在我需要能够将这些值存储到mysql数据库中。如果没有动态创建的字段,我可以这样做。这是代码 -
<!DOCTYPE HTML>
<html>
<head>
<title> Company Value form </title>
<script language = "javascript">
function addTextField(){
var element = document.createElement("input");
element.setAttribute("name", "i");
element.setAttribute("value", "sample value");
element.setAttribute("size", 30);
var twitter = document.getElementById("twitterusernames");
twitter.appendChild(element);
}
</script>
</head>
<body>
<h1> Enter the values </h1>
<form method = "post" name = "config_form" action = "message.php">
<table border = "1px" >
<tr> <td>
<b> Twitter </b> <br/> <br/>
FeatureId: <input type = "text" name = "FeatureId"> <br/>
Image: <input type = "text" name = "Image"> <br/>
LabelEn: <input type = "text" name = "LabelEn"> <br/>
LabelFr: <input type = "text" name = "LabelFr"> <br/>
URL: <input type = "text" name = "URL"> <br/>
TwitterUserNames: <input type = "text" name = "TwitterUserName" size = "50">
<input type = "button" value = "Add" onclick = "addTextField()"/>
<span id="twitterusernames"> </span>
<br/>
Minimum Count: <input type = "text" name = "MinimumCount"> <br/>
Refresh Count: <input type = "text" name = "RefreshCount"> <br/>
EmptyMessage English: <input type = "text" name = "EmptyMessageEnglish"> <br/>
EmptyMessage French: <input type = "text" name = "EmptyMessageFrench"> <br/>
Widget: <input type = "text" name = "Widget"> <br/>
Email Share Text: <input type = "text" name = "EmailShareText"> <br/>
</td> </tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
和PHP代码:
<?php
include 'database.php';
$Twitter = array(
'FeatureId' => $_POST["FeatureId"],
'Image' => $_POST["Image"],
'LabelEn' => $_POST["LabelEn"],
'LabelFr' => $_POST["LabelFr"],
'URL' => $_POST["URL"],
'TwitterUserName' => $_POST["TwitterUserName"],
'MinimumCount' => $_POST["MinimumCount"],
'RefreshCount' => $_POST["RefreshCount"],
'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
'Widget' => $_POST["Widget"],
'EmailShareText' => $_POST["EmailShareText"]
);
$crud = new database();
$insert = $crud -> insert($Twitter);
if ($insert) {
echo "Success";
}
else {
echo "Values were not inserted into the database";
}
?>
答案 0 :(得分:3)
添加此element.setAttribute("name", "i[]");
然后在你的PHP处理程序中
$Twitter = array(
'FeatureId' => $_POST["FeatureId"],
'Image' => $_POST["Image"],
'LabelEn' => $_POST["LabelEn"],
'LabelFr' => $_POST["LabelFr"],
'URL' => $_POST["URL"],
'TwitterUserName' => $_POST["TwitterUserName"],
'MinimumCount' => $_POST["MinimumCount"],
'RefreshCount' => $_POST["RefreshCount"],
'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
'Widget' => $_POST["Widget"],
'EmailShareText' => $_POST["EmailShareText"],
'extraFields' => serialize( $_POST["i"] ) // Extra fields
);
$crud = new database();
$insert = $crud -> insert($Twitter);
if ($insert) {
echo "Success";
}
else {
echo "Values were not inserted into the database";
}
?>
显示数据。我们假设您已经拥有$extraFields
<?php
$extraFields = unserialize( $extraFields );
foreach($extraFields as $extra ):
?>
<input type="text" name="i[]" value="<?php echo $extra; ?> ">
<?php
endforeach;
?>
答案 1 :(得分:2)
以[]
:
element.setAttribute("name", "i[]");
然后,PHP将使用所有值的数组填充$_POST['i']
。
答案 2 :(得分:2)
根据你的脚本,我想到你点击按钮后添加文本框。因此,当您添加多个文本框时,您需要将文本框字段名称设为数组,即
<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />
修改addTextField()函数
function addTextField(){
var element = document.createElement("input");
element.setAttribute("name", "i[]");
element.setAttribute("value", "sample value");
element.setAttribute("size", 30);
var twitter = document.getElementById("twitterusernames");
twitter.appendChild(element);
}
在 message.php 文件中,您可以通过$ _POST [i]以数组格式获取值。
答案 3 :(得分:0)
如果添加的字段只包含辅助数据,我只需添加另一个文本类型的列,然后将这些字段的JSON编码值转储到一个块中。