为了账号安全,请及时绑定邮箱和手机立即绑定

无法获取表单数组值

无法获取表单数组值

PHP
摇曳的蔷薇 2022-09-17 22:38:41
我有一个动态表单,用户可以在其中向表单添加其他行。最终,我想获取此数据并将其提交到我的数据库,并带有插入,更新或删除的条件,但现在我只想检索数组值。每当我点击提交时,它只会进入空白的白页。以下是表格:<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[],我可以让表单行回显,以便我知道表单正在发布数据。但是一旦我将名称转换为数组,名称[]我似乎无法获取数据。我做错了什么?
查看完整描述

2 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

您需要通过其索引获取所有字段:


<?php

    require_once '../../../wp-load.php';


    global $wpdb;

    $newdb = new wpdb( 'user' , 'pass' , 'table' , 'localhost' );


    if(isset($_POST['submit'])) {


        $username = $_POST['username'];

        $first_name = $_POST['first_name'];

        $last_name = $_POST['last_name'];

        $county = $_POST['county'];

        $street = $_POST['street'];

        $city = $_POST['city'];

        $ward = $_POST['ward'];

        $precinct = $_POST['precinct'];

        $id = $_POST['id'];

        $is_active = $_POST['is_active'];



        if(is_array($_POST['username'])) {

            foreach($username as $index => $value) {

                // You can get all other items by array[$index];

                // Example: $street[$index]


                // If you include array items inside a string

                // enclose it between curly braces

                echo "Username: $value, First name: {$first_name[$index]}<br>";

            }   

        }


    }


?>


查看完整回答
反对 回复 2022-09-17
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

它必须是:

$first_name = $_POST['first_name'][];


查看完整回答
反对 回复 2022-09-17
  • 2 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号