Divide form to insert in different lines of the database (codeigniter)

0

I have a dynamic form that is filled as there are questions in the database, this form tb writes text boxes, checkboxes, textareas, etc according to the type of question defined in the database, the problem is that when submitting I need that it picks up every question and answer and inserts it into different rows of the database, but I can not do that.

NowI'mdoingitdifferently,eachsetofinputsIwantondifferentlinesaredistinguishedby(name-idQuestion)

array(22){["privacidade-16"]=> string(1) "1" ["idPergunta-16"]=> string(2) "16" ["resposta-16"]=> string(14) "Vitor Bonzinho" ["privacidade-17"]=> string(1) "1" ["idPergunta-17"]=> string(2) "17" ["resposta-17"]=> string(10) "2015-10-10" ["privacidade-18"]=> string(1) "1" ["idPergunta-18"]=> string(2) "18" ["idRespostaPre-18"]=> string(2) "19" ["privacidade-19"]=> string(1) "1" ["idPergunta-19"]=> string(2) "19" ["resposta-19"]=> string(16) "Rua do mormugão" ["privacidade-20"]=> string(1) "1" ["idPergunta-20"]=> string(2) "20" ["idRespostaPre-20"]=> string(2) "38" ["privacidade-21"]=> string(1) "1" ["idPergunta-21"]=> string(2) "21" ["resposta-21"]=> string(8) "4465-213" ["privacidade-30"]=> string(1) "1" ["verificada-30"]=> string(1) "1" ["idPergunta-30"]=> string(2) "30" ["idRespostaPre-30"]=> array(2) { [0]=> string(3) "186" [1]=> string(3) "188" } }

How do I get the controller to separate these sets and insert one of each x?

insert all inputs that have -16 insert all inputs that have -17 ... thereafter

Thank you in advance

    
asked by anonymous 27.10.2015 / 11:28

1 answer

0

In my view, your problem is one of logic. If your form is dynamic, there is certainly a button to add a new question, even if a previous one has not yet been saved, right?

I have had a similar problem, I had a register of people responsible for certain areas in the company, where each one has 1 name, email, phone and etc.

The problem was, how to identify the data for each person?

For this I created a JS script to organize things for me, here's how I did it:

I knew that person has name, phone and email, so we have 3 fields. I need to put the name attribute of each field so that I can identify each respective client and its data.

The structure of the fields was something like this:

<div class='contato'>
   <input type='text' name='nome' />
   <input type='text' name='email' />
   <input type='text' name='telefone' />
</div>
<!-- Saber a quantidade de grupos de campos que vamos pegar no POST -->
<input type='hidden' name='qtdCampos' id='qtdCampos' value='1' />

So my script worked like this:

function organizarCampos() {

   var i     =  0;

   var cont  =  1; 

   $( '.contato' ).find( 'input[type=text]' ).each( function () {

      //Pegando somente letras
      var name  =  $( this ).attr( 'name' ).replace(/[^a-zA-Z]+/g, '');

      //Novo valor para o name
      var newName  += i;

      $( this ).attr( 'name', newName );

      if( cont == 3 ) {
         cont = 1;
         i++;
      }else {
         cont++;
      }

   });

}

Now we need to know how many fields we'll get in POST

var qtd   =  $( '.contato' ).find( 'input[type=text]' ).length / 3;

$( '#qtdCampos' ).val( qtd );

Now in PHP

$qtdGrupos   =   $this->input->post( 'qtdCampos' );

for( $i = 0; $i < $qtdCampos; $i++ ) {
   $nome  =  $this->input->post( 'nome' . $i );
   ...
}

I hope to have helped my friend.

    
23.12.2015 / 02:37