How to submit data from a JS array by PHP?

2

I have a form that has a table that will be generated as the user adds elements to it through the "Add" button. (One client can have multiple addresses)

How many data will be generated, I will need an array. The only way I can do this is by using javascript.

How do I get these javascript data and "play" them on my submit form button? Is there any way I can do this using only PHP?

    
asked by anonymous 29.08.2018 / 19:32

1 answer

1

There are several ways to do this, it would be more interesting if you make the current code available.

Anyway look at the code below. Maybe I can give you a light on what to do. If that does not help, let me know that I can think of something to help you.

<?php 

print_r($_POST);

?>
<div>

    <form method="post">

        <div>
            <label> Campo 1 </label>
            <input type="text" name='campo[]'>
        </div>

        <div>
            <label> Campo 2 </label>
            <input type="text" name='campo[]'>
        </div>

        <div>
            <label> Campo 3 </label>
            <input type="text" name='campo[]'>
        </div>

        <button> Enviar </button>

    </form>

</div>

The result for the furmulario above is:

Array
(
    [campo] => Array
        (
            [0] => 11
            [1] => 22
            [2] => 33
        )

)

Note that by adding the '[]' at the end attribute 'name' of the input PHP will understand that it is an array. When duplicating the fields of the address you will have for example a field CEP being an array in PHP:

Array
    (
        [cep] => Array
            (
                [0] => 74063-470
                [1] => 75266-392
            )

    )
    
29.08.2018 / 20:10