0
Opa,
I have a form in which post the form data and capture via a forech.
In the form I have a button where I create new inputs by the jquery append, inputs with same name.
The problem I have is that when registering only one item, the Insert is normally performed, but when informing more items, only the first set of data is inserted, is the second returns with values 0.
The code to retrieve the values are:
foreach( $_POST['numero'] as $key => $n ) {
$data_cadastro = explode('/', $_POST['dt_cadastro'][$key]);
$data_cadastro = $data_cadastro[2].'-'.$data_cadastro[1].'-'.$data_cadastro[0];
$telefone_numero = str_replace("(", "", $_POST['numero'][$key]);
$telefone_numero = str_replace(")", "", $telefone_numero);
$telefone_numero = str_replace("-", "", $telefone_numero);
$telefone_numero = str_replace(" ", "", $telefone_numero);
$_POST['id_usuario'] = $_POST['id_usuario'][$key];
$_POST['numero'] = $telefone_numero;
$_POST['id_operadora'] = $_POST['id_operadora'][$key];
$_POST['dt_cadastro'] = $data_cadastro;
$_POST['status'] = $_POST['status'][$key];
$_POST['id_numero'] = $_POST['id_numero'][$key];
$result = DBCreate($tbl, $_POST, TRUE, TRUE);
}
When you print the post, I get this:
Array
(
[id_usuario] => 3
[numero] => 82999999999
[id_operadora] => 1
[dt_cadastro] => 2016-04-14
[status] => 1
[id_numero] => 23232323
)
Array
(
[id_usuario] =>
[numero] => 2
[id_operadora] =>
[dt_cadastro] => --0
[status] =>
[id_numero] => 3
)
There’s something wrong with my foreach?
The fields of the same name have the same name, following a pattern: name="id_numero[]"
Check that when you print the post before foreach I get:
Array
(
[id_usuario] => 31
[numero] => Array
(
[0] => (82) 99999-9999
[1] => (82) 8888-8888
)
[id_operadora] => Array
(
[0] => 1
[1] => 5
)
[dt_cadastro] => Array
(
[0] => 14/04/2016
[1] => 14/04/2016
)
[status] => Array
(
[0] => 1
[1] => 1
)
[id_numero] => Array
(
[0] => 23232323
[1] => 9830048884
)
)
That is, the values are correct before the foreach
I don’t understand why you’re rewriting the $_POST, besides, you put a value,
$n
, would already bring the value.– Ivan Ferrer
I am doing maintenance on a system that I do not know, the function of Insert in BD captures the posts of the form, I had to rewrite it to work. Vlw
– sNniffer
but ideally you pass a new array, in this case...
– Ivan Ferrer
Just do not replace the POST as I was doing this code, I inserted the columns and fields directly in the Dbcreate field, and everything started to work normally. Too bad I’ll have to edit the whole system, poorly designed code :(
– sNniffer
I can see that.
– Ivan Ferrer
Fix the title there, Forech is wrong, it’s Foreach
– Wallace Maxters