简体   繁体   English

从动态字段事件向数据库添加多行

[英]Adding multiple rows to database from dynamic field event

I have a dynamic event in JS in my form which adds another block of fields so my users can add another address: 我在表单中的JS中有一个动态事件,该事件添加了另一个字段块,以便我的用户可以添加另一个地址:

<script type="text/javascript">
$(document).ready(function() {
  $('#btnAdd').click(function() {
    var $address = $('#address');
    var num = $('.clonedAddress').length; 
    var newNum = new Number(num + 1);
    var newElem = $address.clone().attr('id', 
    'address' + newNum).addClass('clonedAddress');

//set all div id's and the input id's
newElem.children('div').each (function (i) {
this.id = 'input' + (newNum*11 + i);
    });

newElem.find('input').each (function () {
this.id = this.id + newNum;
this.name = this.name + newNum;
    });

    if (num > 0) {
        $('.clonedAddress:last').after(newElem);
    } else {
        $address.after(newElem);
    }


    $('#btnDel').removeAttr('disabled');

    if (newNum == 3) $('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click(function() {
    $('.clonedAddress:last').remove();
    $('#btnAdd').removeAttr('disabled');
    if ($('.clonedAddress').length == 0) {
    $('#btnDel').attr('disabled', 'disabled');
    }
});
$('#btnDel').attr('disabled', 'disabled');
});
</script>

However, when I put my form action the page just refreshes when I click my 'add another address' button: 但是,当我执行表单操作时,单击“添加其他地址”按钮时页面会刷新:

<form action="address.php" method="post" name="regForm" id="regForm" >

These are my fields: 这些是我的领域:

if(empty($err)) {
for($i = 0; $i < 10; $i++)
    {
       $Street = $_POST['Street'][$i];
       $Line2 = $_POST['Line2'][$i];
       $Line3 = $_POST['Line3'][$i];
       $Town = $_POST['Town'][$i];
       $Postcode = $_POST['Postcode'][$i];
       $Country = $_POST['Country'][$i];
       $Tele = $_POST['Tele'][$i];
       $Fax = $_POST['Fax'][$i];
       $Type = $_POST['Type'][$i];
       $Mobile = $_POST['Mobile'][$i];
$sql_insert = "INSERT into `address`
       (`Street`,`Line2`,`Line3`,`Town`, `Postcode` ,`Country`,`Tele`,`Fax`,`Type`
       ,`Mobile`            )
VALUES
       ('$Street','$Line2','$Line3','$Town','$Postcode','$Country',
       '$Tele','$Fax','$Type', '$Mobile'
    )";
mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());     
    }

I want all addresses to go to mysql database. 我希望所有地址都转到mysql数据库。 I hope this is clear 我希望这很清楚

Define buttons as followed: <input type="button" value="ButtonLabel" /> . 如下定义按钮: <input type="button" value="ButtonLabel" />

My short test resulted in my <button> getting treated as submit type input by firefox. 我的简短测试导致我的<button>被firefox视为提交类型输入。 This means <button>FooBar</button> and <input type="submit" value="FooBar" /> are equivalent. 这意味着<button>FooBar</button><input type="submit" value="FooBar" />是等效的。

You might also want to simplify your javascript code. 您可能还想简化javascript代码。 You can use the array notation for input names: 您可以将数组符号用于输入名称:

<input type="text" name="street[]" />
<input type="text" name="zip[]" />

<input type="text" name="street[]" />
<input type="text" name="zip[]" />

will result in $_POST["street"][0] and $_POST["street"][1] beeing filled with the user's input. 会导致$_POST["street"][0]$_POST["street"][1]充满用户的输入。 This is what you want judging from your php code, anyway. 无论如何,这就是您想从您的php代码中判断出来的内容。

You don't need ids for all your input tags. 您不需要所有输入标签都包含ID。 Just keep one full set of inputs for one address and append this to your form. 只需保留一个地址的全套输入​​并将其附加到您的表单即可。 Maybe something like: 也许像:

$('#address').append(
    '<div>' + 
    '<input type="text" name="street[]" />' +
    '<input type="text" name="zip[]" />' +
    '</div>'
);

Or just have a full set hidden on your page and clone it, then append. 或者只是将全套隐藏在页面上并进行克隆,然后追加。 I'm sure our jQuery pros will know a better solution. 我确信我们的jQuery专业人员会知道更好的解决方案。

And finally: Please sanatize your input with mysql_real_escape_string 最后:请使用mysql_real_escape_string整理输入内容

$Street = mysql_real_escape_string($_POST['Street'][$i]);
// and so on for the other values.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM