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


How to generate 2 new input fields when click add new button and insert data into database using php-pdo

我下面有一个html表单。当用户单击添加新按钮时,将生成2个新的输入字段(warishan名称和关系)。用户可以添加许多字段。我如何做到这一点,并将数据存储到两个表中,其中name和contact转到userinfo表和userid(last_inserted),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>

照原样使用。我已经检查了这段代码。工作良好。

<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>

创建SomePage.php,并将其赋予<form></form>action属性。

类似:<form id='test' action="SomePage.php" method="post">

SomePage.php

<?
$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. 
}
?>

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