2
I have to make a form in which I will have this table, only instead of being static it will be dynamic, one can enter 1 familiar, 2 ,3 ,4, 10, whatever she wants. That’s what I’m using it for JQUERY
, And every time someone clicks on an add button they add a new row to the table, this part I know how to do.
My doubt is as follows, how then I send all the information to the database, told me that I would have to store in a array, only that I array s in PHP I’m not very good.
MY EXAMPLE
html part
<form method="post" id="formulario" action="dados.php">
<div id="telefone">
<p><label>Nome</label><input type="text" class="fone" name="agregado[]" size="15" /><span class="adicionar">Adicionar registo</span></p>
<p><label>telefone</label><input type="text" class="fone" name="agregado[]" size="15" /></p>
</div>
<p><input type="submit" value="Enviar" id="btEnviar" name="btEnviar" />
</form>
PART DATA.PHP
$ligacao = mysqli_connect("localhost", "root", "", "teste");
if (mysqli_connect_errno()) {
echo "Erro na liga??o MySQL: " . mysqli_connect_error();
}
$result = count($_POST["agregado"]);
$agregado=$_POST["agregado"];
var_dump($agregado);
for($i=0; $i<$result; $i++){
$sql="insert into teste (nome) values ('$agregado[i]', '$agregado[i]')";//DUVIDA
echo $sql;
}
All right, I’ll get to this part var_dump($agregado)
where the exit is something like this
array(2) { [0]=> string(4) "pessoa" [1]=> string(3) "123" }
This is the way out in case you enter 1 person, if you put 2 the way out will be:
array(4) { [0]=> string(4) "pessoa" [1]=> string(3) "123" [2]=> string(5) "pessoa_2" [3]=> string(3) "234" }
and so on and so forth
How do I insert each position of the array into its proper place?
I do the go-around to go through all array positions but then can’t get it to enter the correct position..
What you can do is: in the name of each input you add
[]
. Example:input name="name[]"
. That is, you can create infinite inputs with the same name (name[]
). And all that information will be on$_POST['name']
as an array. Here in the comments it is difficult to exemplify, but I’ll leave it to someone to make a better example for you...– Rafael Withoeft
because my doubt was also in the input name. Just one thing, if I have 5 input name="name[]" at the end the variable $_POST['name'] will contain the value of these 5 inputs, this is it or I’m wrong?
– Filipe
Exactly. The output will be more or less like this (var_dump):
'name' => 
 array (size=4)
 0 => string '41234' (length=5)
 1 => string '' (length=0)
 2 => string '41234' (length=5)
 3 => string '41234' (length=5)
. Then just go through this array (for) and do the information Insert for each position you pass, example:"INSERT .... VALUES ($_POST['name']['2'], $_POST['outro'][2]....)"
.– Rafael Withoeft
Here is an example for you to take as a basis: http://forum.imasters.com.br/topic/477945-insert-input-dinamico/? p=1899235
– Rafael Withoeft
I didn’t understand the Insert part, I edited my question and added an example code to see if you can help me
– Filipe
As soon as possible, I’ll see you.
– Rafael Withoeft
I believe the right html would be
<input type="text" class="fone" name="nome[]" size="15" />
and<input type="text" class="fone" name="telefone[]" size="15" />
, (note the name of the input) the way your name is now may end up getting confused later.... So the name and phone comes in separate arrays. And in every interaction you can use$nomes[$i]
and$telefones[$i]
. I don’t know the structure of your table, so I can’t indicate an SQL.– Rafael Withoeft