我有一個表單,其中單擊添加更多按鈕時出現新的字段。我如何將它們存儲到php變量中,然後放入mysql數據庫中。 我有PHP和MySQL的工作知識。 我的HTML和JavaScript代碼如下:在陣列如何爲動態生成的輸入字段添加記錄
<script type="text/javascript">
function($) {
$.fn.relCopy = function(options) {
var settings = jQuery.extend({
excludeSelector: ".exclude",
emptySelector: ".empty",
copyClass: "copy",
append: '',
clearInputs: true,
limit: 0 // 0 = unlimited
}, options);
settings.limit = parseInt(settings.limit);
// loop each element
this.each(function() {
// set click action
$(this).click(function(){
var rel = $(this).attr('rel'); // rel in jquery selector format
var counter = $(rel).length;
// stop limit
if (settings.limit != 0 && counter >= settings.limit){
return false;
};
var master = $(rel+":first");
var parent = $(master).parent();
var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append);
//Remove Elements with excludeSelector
if (settings.excludeSelector){
$(clone).find(settings.excludeSelector).remove();
};
//Empty Elements with emptySelector
if (settings.emptySelector){
$(clone).find(settings.emptySelector).empty();
};
// Increment Clone IDs
if ($(clone).attr('id')){
var newid = $(clone).attr('id') + (counter +1);
$(clone).attr('id', newid);
};
// Increment Clone Children IDs
$(clone).find('[id]').each(function(){
var newid = $(this).attr('id') + (counter +1);
$(this).attr('id', newid);
});
//Clear Inputs/Textarea
if (settings.clearInputs){
$(clone).find(':input').each(function(){
var type = $(this).attr('type');
switch(type)
{
case "button":
break;
case "reset":
break;
case "submit":
break;
case "checkbox":
$(this).attr('checked', '');
break;
default:
$(this).val("");
}
});
};
$(parent).find(rel+':last').after(clone);
return false;
}); // end click action
}); //end each loop
return this; // return to jQuery
};
})(jQuery);
</script>
<script type="text/javascript">
$(function(){
var removeLink = ' <a class="remove" href="#" onclick="$(this).parent().slideUp(function(){ $(this).remove() });
return false">remove</a>';
$('a.add').relCopy({ append: removeLink});
});
</script>
<style type="text/css">
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px; }
.remove {color:#cc0000}
.input{ border: solid 1px #006699; padding:3px}
</style>
</head>
<body>
<form method="post" action="">
<table width="580" border="1" class="clone" rules="groups" align="center">
<tr>
<td width="129" height="29" align="right"><strong>Child Name :</strong></td>
<td width="435"><input name="petname[]" type="text" class="txt_box" id="petname" />
</td>
</tr>
<tr>
<td align="right" height="29"><strong>Pet Image :</strong></td>
<td><input name="petimage[]" type="file" class="txt_box" id="petimage" /></td>
</tr>
</table>
<p style=" margin-left:60px;"><a href="#" class="add" rel=".clone">Add More</a></p>
</form>
</body>
</html>
你嘗試過這麼遠嗎?順便說一句。這可能會變得相當複雜(我曾經沿着這些線寫過),所以確保你有一個堅實的數據庫(確保你的ERD是正確的等) – Bono
@Bono DB沒有問題,我只是想知道如何讓他們在PHP中,即以數組的形式,或者如果有別的東西。 – user995426