简体   繁体   English

从表中获取数据到php

[英]fetch data from table into php

I have a table where i add rows dynamically using j-query i want to fetch data from each fields in to my php and do somthing with it when i press submit or maybe we can use j-query to loop around the table to get all the values and put it in an array i do not know how to go about it can some one please help me. 我有一个表格,我使用j-query动态添加行我想从我的php每个字段中获取数据,并在我按下提交时使用它做一些事情或者我们可以使用j-query围绕表格来获取所有价值观并把它放在一个数组我不知道如何去做它可以请一些人帮助我。 this is what i have so far 这就是我到目前为止所拥有的

Jquery.js 的jquery.js

 $(document).ready(function(){
        $(".add-row").click(function(){
            var name = $("#name").val();
            var email = $("#email").val();
            var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + name + "</td><td>" + email + "</td></tr>";
            $("table tbody").append(markup);
        });

        // Find and remove selected table rows
        $(".delete-row").click(function(){
            $("table tbody").find('input[name="record"]').each(function(){
                if($(this).is(":checked")){
                    $(this).parents("tr").remove();
                }
            });
        });
    });

Index.php 的index.php

<form>
            <input type="text" id="name" placeholder="Name">
            <input type="text" id="email" placeholder="Email Address">
            <input type="button" class="add-row" value="Add Row">
        </form>
        <table>
            <thead>
                <tr>
                    <th>Select</th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="checkbox" name="record"></td>
                    <td>Peter Parker</td>
                    <td>peterparker@mail.com</td>
                </tr>
            </tbody>
        </table>
        <button type="button" class="delete-row">Delete Row</button>
        <button type="button" class="Submit">Submit</button>

here is a Fiddle 这是一个小提琴

You could add a hidden input to your form when you create your new row: 您可以在创建新行时向表单添加隐藏输入:

$(".add-row").click(function () {
    var name = $("#name").val();
    var email = $("#email").val();

    var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + name + "</td><td>" + email + "</td></tr>";
    markup += "<input type='hidden' name='name' value='" + name + "'>";
    markup += "<input type='hidden' name='email' value='" + email + "'>";

    $("table tbody").append(markup);
});

Then on the PHP side, you'd receive them like this in the $_POST array like this: 那么在PHP方面,你会在$_POST数组中像这样接收它们,如下所示:

$_POST['name'], $_POST['email]

Since you want to be able to post more than one at a time, you can have them loded into an array, say called customer by naming them like this: 由于您希望能够一次发布多个,因此您可以将它们编入一个数组,例如通过命名为customer来命名为:

markup += "<input type='hidden' name='customer[0][name]' value='" + name + "'>";
markup += "<input type='hidden' name='customer[0][email]' value='" + email + "'>";

Then, $_POST['customer'] would be an array of arrays, each with two keys name and email 然后, $_POST['customer']将是一个数组数组,每个数组都有两个键nameemail

All together it could look like: 它们一起可能看起来像:

var x = 0;

$(".add-row").click(function () {
    var name = $("#name").val();
    var email = $("#email").val();


    var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + name + "</td><td>" + email + "</td></tr>";
    markup += "<input type='hidden' name='customer[" + x + "][name]' value='" + name + "'>";
    markup += "<input type='hidden' name='customer[" + x + "][email]' value='" + email + "'>";
    x++;

    $("table tbody").append(markup);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM