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

时间:2015-12-09 09:56:14

标签: php jquery mysql 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>

2 个答案:

答案 0 :(得分:2)

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

<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 ,并将其action属性<form></form>

赞:<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. 
}
?>

答案 1 :(得分:0)

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