【问题标题】:How to generate 2 new input fields when click add new button and insert data into database using php-pdo [closed]单击添加新按钮并使用php-pdo将数据插入数据库时​​如何生成2个新输入字段[关闭]
【发布时间】:2016-03-14 13:04:27
【问题描述】:

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

【问题讨论】:

    标签: php jquery mysql pdo


    【解决方案1】:

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

    <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,并把它也交给&lt;form&gt;&lt;/form&gt;action属性。

    点赞:&lt;form id='test' action="SomePage.php" method="post"&gt;

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

    【讨论】:

    • 关于这个好答案的小提示,如果您正在写入数据库,请确保还包括对 $_POSTs 的安全检查/清理
    • 哇...谢谢@Nana Partykar。先生,你让我很开心。这是我想要的代码。它解决了我的问题。
    • @Nana Partykar ...你能帮帮我吗?如何使用删除图标删除额外生成的字段(如果不需要一一)???演示link
    • 是的。有可能的。但是,您必须等待 15-20 分钟。我在办公室忙着工作。别介意。我会在 20 分钟后回来。 @RiyadhAhmed
    【解决方案2】:

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

    【讨论】:

    • 是的。我知道需要使用 jquery 来生成输入字段。但我怎样才能生成 2 个字段?还有提交时如何使用 php 获取两个字段值?
    • 要获取两个字段值,您有两种选择:1. 尝试从 $_GET 变量中获取它们,2. 尝试从您的数据库中获取它们。如果您在完成任务时遇到问题,请与我联系:psanatov(at)gmail(dot)com
    • 我在这里找到了一些信息link,但这里生成了一个文件,但我需要两个。还有我如何使用 foreach loop.thanks 获取发布的数据。
    • 类似这样的东西:foreach($_POST as $k => $v) { return "key - ".$k.", value - ".$v; }
    猜你喜欢
    • 2020-05-17
    • 2015-03-30
    • 2018-06-19
    • 2012-02-24
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多