How to send information from a dynamic form?

Asked

Viewed 1,161 times

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.

inserir a descrição da imagem aqui

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...

  • 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?

  • 1

    Exactly. The output will be more or less like this (var_dump): 'name' => &#xA; array (size=4)&#xA; 0 => string '41234' (length=5)&#xA; 1 => string '' (length=0)&#xA; 2 => string '41234' (length=5)&#xA; 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]....)".

  • Here is an example for you to take as a basis: http://forum.imasters.com.br/topic/477945-insert-input-dinamico/? p=1899235

  • I didn’t understand the Insert part, I edited my question and added an example code to see if you can help me

  • As soon as possible, I’ll see you.

  • 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.

Show 2 more comments

1 answer

1

Well, if you can do the jQuery part, that’s a start. From jQuery you can send the values to PHP through POST in various ways, JSON, array, csv, or other.

Imagining a csv, to facilitate the example, imagine that we have a field that brings text, in which the delimiter of the various lines is a ";" and that the delimiter of the fields is a ","

// variavel POSTed por jQuery
$csv = "nome1,parentesco1,idade1,contacto1;nome2,parentesco2,idade2,contacto2"

If the form field is called 'items', you must access it using $_POST['items']. Then:

$items = explode(";", $csv);

foreach ($items as $item) {
$colunas = explode(",", $item);
// Inserir aqui na base de dados, usando $coluna[0], $coluna[1], ..., $coluna[N]
}

Note that this code was made out of my head, is not tested.

Browser other questions tagged

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