Receive array method POST form

Asked

Viewed 6,454 times

0

Good afternoon,

I’m trying to receive a Form as an array, but I’m not able to properly manipulate this array. I have 6 fields 3 fields ID and 3 fields text , I tried to do so but without success. But it runs 6 times the array , and not 3 as it should be.

returns like this. --ID-1--- -name--------ID-2--- -name--------ID-3--- -name-------ID------ --name-wwe--------ID------- --name-qwwqe-------ID------ -name-wqeeqw---

                            <label>ID</label>
                            <input type="text" class="form-control form-control-sm" name="array[][id]" >
                            <label>ID</label>
                            <input type="text" class="form-control form-control-sm" name="array[][id]" >
                            <label>ID</label>
                            <input type="text" class="form-control form-control-sm" name="array[][id]" >
                            <label>array</label>
                            <input type="text" class="form-control form-control-sm" name="array[][nome]" >
                            <input type="text" class="form-control form-control-sm" name="array[][nome]" >
                            <input type="text" class="form-control form-control-sm" name="array[][nome]" >
                            <button type="submit" class="btn btn-sm btn-default" name="salvar">
                                salvar
                            </button>
                            </form>

$test = $_POST['array']; mysqli_select_db($Conn,"test"); foreach ($test as $key1 => $value1) { echo "--ID-". $value1['id']." ---"; echo "
"; echo "-name-". $value1['name']."---";

$query = mysqli_query($conn, "UPDATE excluidos SET rev2_email = '$aux' WHERE rev1_nome = '".$value1['id']."';");

}

  • Bruno, what if you invert the fields in the form? Instead: "array[][id]", put this: "array[id][]". You should probably review your foreach as well, because the created array is multidimensional.

1 answer

1


Another way to do it:

Change name="array[][id]" for name="arrayid[]" and name="array[][nome]" for name="arraynome[]", in PHP:

$id = $_POST['arrayid'];
$nome = $_POST['arraynome'];

for($i = 0; $i < count($id); $i++) {
    echo "| $id[$i] | $nome[$i] |<br>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.