我如何在我的数据库中插入多个文本框值,使用动态文本字段
How can i insert multiple textboxes value in my database, using dynamic text fields
我正在构建表单与动态文本框,如果你点击按钮"添加",它会出现一个新的文本框,我使用php和javascript。
但我只是可以添加到我的数据库从第一个文本框的值。
我如何插入多个值到我的数据库?
<html>
<head>
<title></title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript"><!--
$(document).ready(function(){
var counter = 2;
$("#add").click(function () {
if(counter==11){
alert("Too many boxes");
return false;
}
$("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input type='textbox' id='t"+counter+"' > </div>'n");
++counter;
});
$("#remove").click(function () {
if(counter==1){
alert("No boxes");
return false;
}
--counter;
$("#d"+counter).remove();
});
});
// --></script>
</head>
<body>
<form method="POST">
<div id='textBoxes'>
<label for="t1"> Textbox 1</label>
<input name="nome" type='textbox' id='t1' ></div>
</div>
<input type='button' value='add' id='add'>
<input type='button' value='remove' id='remove'>
<input type='submit' name="adicionar" value='adicionar'>
</form>
<?php
$dbHost = 'localhost';
$dbUsername = 'hr';
$dbPassword = '';
$dbDatabase = 'r_inserir';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
if(isSet($_POST['adicionar']))
{
$nome=mysql_real_escape_string($_POST['nome']);
$sql= mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
}
?>
</body>
</html>
在html:
中这样命名<input name="nome[]" type='textbox' id='t1' ></div>
then in your javascript:
$("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input name='nome[]' type='textbox' id='t"+counter+"' > </div>'n");
这些代码将数组数据返回到PHP文件中。然后将它插入到数据库中。
if(isset($_POST['adicionar']))
{
//$nome=mysql_real_escape_string($_POST['nome']);
foreach($_POST['nome'] as $nome){
mysql_query("INSERT INTO registos(nome) values ('".mysql_real_escape_string($nome)."')");
}
//$sql= mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
}
重要:
此扩展在PHP 5.5.0中已弃用,并将在将来被删除。应该使用MySQLi或PDO_MySQL扩展名
首先,您必须为输入字段提供动态名称和id,其次,循环遍历从post获取的所有输入值,然后保存它。
例句:
<input type='textbox' id='t"+counter+"' name='name"+counter+"' >
,然后使用$_POST['name'], $_POST['name1']等将其存储在数据库中
不是最好的解决方案,但是您可以让表单以数组的形式发送文本字段:
<input type="text" name="nome[]" ... />
然后你可以在PHP中使用:
<?php
$_POST['nome'] //array containing all the values
?>
最后,您可以将这个数组序列化存储在数据库中。看看http://www.php.net/manual/en/function.serialize.php
<?php
//when you store the data
$nome=mysql_real_escape_string( serialize ($_POST['nome']) );
//....
//when you retrieve the data
$nomes = unserialize($row['nome']); //(for example, returns array with all names)
?>
重要提示:当使用序列化内容时,您可能会将MySQL字段类型更改为TEXT而不是VARCHAR,因为它可能是一个很长的字符串。
附录:你也应该使用mysqli而不是mysql!
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 使用jquery将输入字段转换为文本
- 如何从查询字符串中的输入字段发回文本
- 表单输入字段随着溢出的文本而增长
- 使用单个文本框向多个字段添加严格搜索
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 使用jQuery的输入字段文本换行
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 模拟文本输入字段上的退格键
- 输入字段中的可持续文本
- 未获取文本输入字段的值
- 没有文本安全性的密码字段
- 将禁用的输出字段更改为带前缀的文本
- 为什么 react 将输入[类型=“文本”] 字段设为只读,除非我提供 onChange 回调
- 使用SAPUI5中的“文本”字段显示正确的“日期”
- Kendo,如何将MVVM文本:字段绑定到远程DataSource