将数据数组从动态表单插入数据库

时间:2018-01-04 12:43:24

标签: javascript php jquery html forms

我有一个动态表格。在表单中,我可以根据需要添加多个字段。但我很困惑如何将其保存到数据库中。

这是我的表单代码,就像这样但不同的https://jsfiddle.net/Twisty/q8zj00s0/1/

   <form id="tambahepisode" method="post" style="margin-bottom: 20px;"></form>
<form>
  <ul id="fieldList">
    <li>
      <input name="name[]" type="text" placeholder="Name" form="tambahepisode"/>
    </li>
    <li>
      <input name="phone[]" type="text" placeholder="Phone" form="tambahepisode"/>
    </li>
    <li>
      <input name="email[]" type="text" placeholder="E-Mail" form="tambahepisode"/>
    </li>
  </ul>
  <button id="addMore">Add more fields</button>
  <br>
  <br>
  <input type="submit" name="submit" class="btn btn-default" value="Publikasi" form="tambahepisode" style="width: 100%;" />
</form>

在这里我的jquery创建动态表单

<script type="text/javascript">
    $(function() {
  $("#addMore").click(function(e) {
    e.preventDefault();
    $("#fieldList").append("<li>&nbsp;</li>");
    $("#fieldList").append("<li><input type='text' name='name[]' placeholder='Name' /></li>");
    $("#fieldList").append("<li><input type='text' name='phone[]' placeholder='Phone' /></li>");
    $("#fieldList").append("<li><input type='text' name='email[]' placeholder='E-Mail' /></li>");
  });
});

</script>

我对数据库的基本插入

if(isset($_POST['submit'])){
        $name= htmlentities($_POST['name']);
        $phone= htmlentities($_POST['phone']);
        $email= htmlentities($_POST['email']);
        $query = $db->prepare("INSERT INTO `data`(`name`,`phone`,`email`)
        VALUES (:name,:phone,:email)");
        $query->bindParam(":name", $name);
        $query->bindParam(":phone", $phone);
        $query->bindParam(":email", $email);
        $query->execute();
        header("location: index.php");
    }

1 个答案:

答案 0 :(得分:0)

首先,您需要计算要保存的记录数量,因为您需要通过添加更多按钮来保存数组。

例如,计数名称或任何其他字段。

    ldap.enabled = true

####### LDAP TEST##############
ldap.urls= ldap://ldap.forumsys.com:389/
ldap.base.dn= dc=example,dc=com
ldap.username= cn=read-only-admin,dc=example,dc=com
ldap.password= password
ldap.user.dn.pattern = uid={0}

然后使用foreach或for循环。

$count = count($_POST['name']);
相关问题