How to fix form data in array that is not submitting properly

43rdworld

I'm building an admin form that allows other teams to update aspects of their own data so they don't have to call me to do it for them.

They update the data via checkboxes or text fields. There is also a requirement that they be able to edit multiple rows at once. So the form required them to select which rows they will be updating or have updated. so far so good.

My problem is thus: If the first record has one checkbox selected and the second record has 3 boxes checked, when the form is submitted and the data echoed back out, the first record shows 3 values and the second record shows 1 indicating that record 1 has somehow taken the values from record 2 for itself. I know that's not the case but can't figure out why that data is not staying with record 2.

I've beat around this and searched and found numerous examples that should work but they are just using a single text field and not checkboxes. So, I'm hoping someone here can help me sort it out.

<?php
    if(isset($_POST['updateRecord'])) {
        // echo "Update Record Outer Check<br>";
        if ((strtolower($_POST['updateRecord']) == "update")) {
            foreach ($_POST['update'] as $key => $update) {
                $id = $_POST['id'][$key];
                $regOK = (
                    isset($_POST['regOK'][$key]) ?
                    $_POST['regOK'][$key] : false
                );
                $paidOK = (
                    isset($_POST['paidOK'][$key]) ?
                    $_POST['paidOK'][$key] : false
                );
                $ebdOK = (
                isset($_POST['ebdOK'][$key]) ?
                    $_POST['ebdOK'][$key] : false
                );
                $appOK = (
                isset($_POST['appOK'][$key]) ?
                    $_POST['appOK'][$key] : false
                );
                echo "RECORD ID: ".$id."<br>";
                echo " REG: ".$regOK."<br>";
                echo "PAID: ".$paidOK."<br>";
                echo " EBD: ".$ebdOK."<br>";
                echo " APP: ".$appOK."<br><br>";
            }
        }
    }
    echo "<br><br>";
?>
<style type="text/css">
    th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
    <table cellpadding="2" cellspacing="0" class="resultsTable">
        <tr>
            <th>REG</th>
            <th>PAID</th>
            <th>EBD</th>
            <th>APP</th>
            <th>Update Record</th>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
            <td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" class="test" name="update[]" id="update" value="1">
                <input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="1"  />
            </td>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
            <td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" class="test" name="update[]" id="update" value="2">
                <input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="2"  />
            </td>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[]" id="regOK" value="1"></td>
            <td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" name="update[]" id="update" value="3">
                <input type="hidden" name="updateRecord" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="3"  />
            </td>
        </tr>
        <tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
    </table>
</form>

The first two checkboxes are checked. Leave row 1 alone and check the second checkbox for record 2, select the update checkbox for the first two rows and then submit the form. What I expect to see is:

RECORD ID: 1 REG: 1 PAID: EBD: APP:

RECORD ID: 2 REG: 1 PAID: 1 EBD: APP:

What I'm getting in this example is: RECORD ID: 1 REG: 1 PAID: 1 EBD: APP:

RECORD ID: 2 REG: 1 PAID: EBD: APP:

You can see where the first record shows the data from row two.

Suraj Tiwari

Bug in your code is indexing, handling form with index will solve it

<?php
    if(isset($_POST['updateRecord'])) {
        // echo "Update Record Outer Check<br>";
        if ((strtolower($_POST['updateRecord']) == "update")) {
            foreach ($_POST['update'] as $key => $update) {
                $id = $_POST['id'][$key];
                $regOK = (
                    isset($_POST['regOK'][$key]) ?
                    $_POST['regOK'][$key] : false
                );
                $paidOK = (
                    isset($_POST['paidOK'][$key]) ?
                    $_POST['paidOK'][$key] : false
                );
                $ebdOK = (
                isset($_POST['ebdOK'][$key]) ?
                    $_POST['ebdOK'][$key] : false
                );
                $appOK = (
                isset($_POST['appOK'][$key]) ?
                    $_POST['appOK'][$key] : false
                );
                echo "RECORD ID: ".$id."<br>";
                echo " REG: ".$regOK."<br>";
                echo "PAID: ".$paidOK."<br>";
                echo " EBD: ".$ebdOK."<br>";
                echo " APP: ".$appOK."<br><br>";
            }
        }
    }
    echo "<br><br>";
?>
<style type="text/css">
    th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
    <table cellpadding="2" cellspacing="0" class="resultsTable">
        <tr>
            <th>REG</th>
            <th>PAID</th>
            <th>EBD</th>
            <th>APP</th>
            <th>Update Record</th>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
            <td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" class="test" name="update[]" id="update" value="1">
                <input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="1"  />
            </td>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[1]" id="regOK" value="1" checked></td>
            <td><input type="checkbox" name="paidOK[1]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[1]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[1]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" class="test" name="update[1]" id="update" value="2">
                <input type="hidden" name="updateRecord[1]" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="2"  />
            </td>
        </tr>
        <tr>
            <td><input type="checkbox" name="regOK[2]" id="regOK" value="1"></td>
            <td><input type="checkbox" name="paidOK[2]" id="paidOK" value="1"></td>
            <td><input type="checkbox" name="ebdOK[2]" id="ebdOK" value="1"></td>
            <td><input type="checkbox" name="appOK[2]" id="appOK" value="1"></td>
            <td>
                <input type="checkbox" name="update[2]" id="update" value="3">
                <input type="hidden" name="updateRecord" id="updateRecord" value="update">
                <input type="hidden" name="id[]" id="id" value="3"  />
            </td>
        </tr>
        <tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
    </table>
</form>

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change Request before submitting data to a Form?

Form is not submitting data on mobile

Submitting form data to database

How do I post form data without submitting the form with jQuery?

How can I edit form json data before submitting a form?

amp form not submitting form data

How to redirect on submitting a form?

AJAX submitting form without refresh, not working properly

Form submitting but mysql query not properly functioning

How can I empty this AngularJS form after submitting the data?

How to add data to a form when submitting via a POST AJAX request?

How to send data to servlet using ajax without a submitting form

How to get and replace data from form serialize() before submitting?

How to load table data after submitting the form in react js php

How do I redirect users after successfully submitting form data?

How to prevent redirect after submitting data through a form

how clear form filed after submitting data in flutter?

how to display form data for preview before submitting in Laravel 5.6

How to stop button inside form tags from submitting data

Codeigniter 3.1.9 Form not submitting data

submitting form data to MySQL Issue

Html Form not submitting data to server

Form not submitting with large amounts of data

Display the data entered in a form before submitting the form

Submitting form using selection value with form data

How to use button for submitting form?

How to redirect after submitting form

Fix array not properly being assigned

how to form an array of arrays with data