如何在单击AddNew按钮并使用php pdo将数据插入数据库时生成2个新的输入字段

如何在单击AddNew按钮并使用php pdo将数据插入数据库时生成2个新的输入字段,php,jquery,mysql,pdo,Php,Jquery,Mysql,Pdo,我在下面有一个html表单。当用户单击“添加新”按钮时,将生成两个新的输入字段(warishan名称和关系)。用户可以添加许多字段。我如何做到这一点,并将数据存储到两个表中,其中name和contact进入userinfo表 和userid(最后插入)、warishan名称和关系进入关系表。有人帮忙吗 <form id='test' action=""method="post"> Name : <input type="text" id="applicantName" name

我在下面有一个html表单。当用户单击“添加新”按钮时,将生成两个新的输入字段(warishan名称和关系)。用户可以添加许多字段。我如何做到这一点,并将数据存储到两个表中,其中name和contact进入userinfo表 和userid(最后插入)、warishan名称和关系进入关系表。有人帮忙吗

<form id='test' action=""method="post">
Name : <input type="text" id="applicantName" name="applicantName" value="" placeholder="Your Name">
Contact : <input type="text" id="ContactNumber" name="ContactNumber" value="" placeholder="Contact Number">

<label class="label"> <strong> Warishan Names <strong> </label>

<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th> # </th>
<th>Warishan Names</th>
<th>Relations</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><input type="text" name="wname" id="wname" value="" placeholder=" "></td>
<td><input type="text" name="wr" id="wr" value="" placeholder=""></td>
</tr>
<tr>
</tbody>
</table> 

<button> add new Warishan </button>
<input type="submit" name="submit" value="Submit" class="btn btn-info">
</form>

姓名:
联系人:
华里山名称
# 
瓦里山的名字
关系
1.
新增华山

如果我理解正确,您希望在单击按钮时添加一些
输入
字段,对吗? 要做到这一点,请使用javascript(您可以为此尝试jQuery)。 PHP是一种服务器端脚本语言,您不应该使用它
对于您想要的前端内容。

按原样使用。我已经检查了这个代码。工作很好

<form id='test' action="SomePage.php" method="post">
    Name : <input type="text" id="applicantName" name="applicantName" value="" placeholder="Your Name">
    Contact : <input type="text" id="ContactNumber" name="ContactNumber" value="" placeholder="Contact Number">

    <label class="label"> <strong> Warishan Names <strong> </label>
    <div class="table-responsive">
        <table id="form_table" class="table table-bordered">
            <tr>
                <th>S. No</th>
                <th>Warishan Names</th>
                <th>Relations</th>
            </tr>
            <tr class='case'>
                <td><span id='snum'>1.</span></td>
                <td><input class="form-control" type='text' name='wname[]'/></td>
                <td><input class="form-control" type='text' name='wr[]'/></td>
            </tr>
        </table>
        <button type="button" class='btn btn-success addmore'>+ add new Warishan</button> <br>
    </div>
    <input type="submit" name="submit" value="Submit" class="btn btn-info">
</form>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
    $(document).ready(function(){
        $(".addmore").on('click', function () {
            var count = $('table tr').length;
            var data = "<tr class='case'><td><span id='snum" + count + "'>" + count + ".</span></td>";
            data += "<td><input class='form-control' type='text' name='wname[]'/></td> <td><input class='form-control' type='text' name='wr[]'/></td></tr>";
            $('#form_table').append(data);
            count++;
        });
    });
</script>

对我知道需要使用jquery来生成输入字段。但是如何生成两个字段呢?提交时,如何使用php获取两个字段值?要获取两个字段值,有两个选项:1。尝试将它们从$\u get变量2中取出。尝试从数据库中获取它们。在一个案例中,如果你在完成任务时遇到困难,请与我联系:psanatov(at)gmail(dot)comi我在这里找到了一些信息,但这里生成了一个字段,但我需要两个。另外,我如何使用foreach循环获取发布的数据。谢谢。类似这样的内容:foreach($\u POST as$k=>$v){返回“key-”$k.”,value-“$v;}。关于这个好答案的小提示,如果你在写数据库,请确保还包括对$\u POST的安全检查/清理哇…谢谢@Nana Partykar。先生,你让我高兴极了。这就是我想要的代码。这解决了我的问题。@Nana Partykar…你能帮我个小忙吗???如何使用移除图标移除额外生成的字段(如果不需要逐个移除)???是的。这是可能的。但是,你得等15-20分钟。我正忙着在办公室工作。别介意。我20分钟后回来。@RiyadhAhmed
<?
$wname=$_POST['wname'];
$wr=$_POST['wr'];

$totalName=sizeof($wname);

for($i=0;$i<$totalName;$i++)
{
    $name = $wname[$i];
    $relation = $wr[$i];
    echo $name." ".$relation."<br>";

    //Use $name and $relation in your query. 
}
?>