猿问

无法获取表单数组值

我有一个动态表单,用户可以在其中向表单添加其他行。最终,我想获取此数据并将其提交到我的数据库,并带有插入,更新或删除的条件,但现在我只想检索数组值。每当我点击提交时,它只会进入空白的白页。


以下是表格:


<form id="" class="form" name="parts" method="post" action="http://website.com/home/wp-content/themes/Avada-child/update-list.php">

    <table class="table table-bordered">

        <thead>

            <tr>

                <th>First Name</th>

                <th>Last Name</th>

                <th>County</th>

                <th>Street</th>

                <th>City</th>

                <th>Ward</th>

                <th>Precinct</th>

                <th>Actions</th>

            </tr>

        </thead>

        <tbody>

            <tr>

                <td style="display:none;">

                    <input type="hidden" class="form-control" name="username[]" id="username" value="user">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="first_name[]" id="first_name">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="last_name[]" id="last_name">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="county[]" id="county">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="street[]" id="street">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="city[]" id="city">

                </td>

                <td>

                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="ward[]" id="ward">

                </td>


如果我没有名称作为 name[],我可以让表单行回显,以便我知道表单正在发布数据。但是一旦我将名称转换为数组,名称[]我似乎无法获取数据。我做错了什么?


摇曳的蔷薇
浏览 133回答 2
2回答

绝地无双

您需要通过其索引获取所有字段:<?php&nbsp; &nbsp; require_once '../../../wp-load.php';&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; $newdb = new wpdb( 'user' , 'pass' , 'table' , 'localhost' );&nbsp; &nbsp; if(isset($_POST['submit'])) {&nbsp; &nbsp; &nbsp; &nbsp; $username = $_POST['username'];&nbsp; &nbsp; &nbsp; &nbsp; $first_name = $_POST['first_name'];&nbsp; &nbsp; &nbsp; &nbsp; $last_name = $_POST['last_name'];&nbsp; &nbsp; &nbsp; &nbsp; $county = $_POST['county'];&nbsp; &nbsp; &nbsp; &nbsp; $street = $_POST['street'];&nbsp; &nbsp; &nbsp; &nbsp; $city = $_POST['city'];&nbsp; &nbsp; &nbsp; &nbsp; $ward = $_POST['ward'];&nbsp; &nbsp; &nbsp; &nbsp; $precinct = $_POST['precinct'];&nbsp; &nbsp; &nbsp; &nbsp; $id = $_POST['id'];&nbsp; &nbsp; &nbsp; &nbsp; $is_active = $_POST['is_active'];&nbsp; &nbsp; &nbsp; &nbsp; if(is_array($_POST['username'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($username as $index => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You can get all other items by array[$index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Example: $street[$index]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If you include array items inside a string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // enclose it between curly braces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Username: $value, First name: {$first_name[$index]}<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>

慕姐8265434

它必须是:$first_name&nbsp;=&nbsp;$_POST['first_name'][];
随时随地看视频慕课网APP
我要回答