Home » Php » php – Cannot get form array values

php – Cannot get form array values

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a form that is dynamic where a user can add additional rows to the form. Ultimately, I would like to get this data and submit it to my database with conditionals for either insert, update or delete, but for now I just want to retrieve the array values. Whenever I hit submit it just goes to a blank white page.

Here is the form:

<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>
                <td>
                    <input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="precinct[]" id="precinct">
                </td>
                <td style="display:none;">
                    <input type="hidden" class="form-control" name="id[]" id="id">
                </td>
                <td style="display:none;">
                    <input type="hidden" class="form-control active" name="is_active[]" id="is_active" value="yes">
                </td>
                <td> 
                    <a class="delete" title="" data-toggle="tooltip" data-original-title="Delete"><i class="material-icons"></i></a>
                </td>
            </tr>
        </tbody>
    </table>
    <div class="col-lg-12" style="text-align:right; padding-right:0;">
        <button type="button" class="btn btn-info add-new"><i class="fa fa-plus"></i> Add New</button>
    </div>
    <div style="float:left; width:100%; text-align:right; padding-bottom:24px; padding-top:24px;">
        <input type="submit" id="savey" name="submit" value="submit" border="0">
    </div>
</form>

And here is my form action file:

<?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['submit'])) {
            foreach($username as $key ) {
                echo $key . "<br>";
            }   
        }

    }

?>

If I do not have the names as name[] I can get the form row to echo so I know that the form is posting the data. But once I turn the names to arrays, name[] I can’t seem to grab the data. What am I doing wrong?

How to&Answers:

You need to get all fields by it’s index:

<?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>";
            }   
        }

    }

?>

Answer:

It has to be:

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