Split form to insert into rows other than Database (codeigniter)

Asked

Viewed 95 times

0

I have a dynamic form that is filled as there are questions in the database, this form also writes text boxes, checkbox, textareas, etc according to the type of question defined in the database, the problem is that when doing Ubmit I need him to take every question and every answer and insert it into different lines of the database, but I’m not able to do that.

inserir a descrição da imagem aqui

Now I’m doing it differently, every set of inputs I want on different lines are distinguished by (name-idPergunta)

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 make Inserts one of each x?

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

Thanks in advance

  • 1

    What exactly is your difficulty? Make the dynamic form or query query?

  • the dynamic form is already done, but when submitting I want to divide the entire form into groups, that is, the complete form has several questions, each question will have answer either text or multiple choice, will also have a field that says whether that question is private or not and whether it has already been verified or not. but when doing Ubmit it sends me everything together and I am not able to separate by groups (id of the question, idda default answer if it is checkbox, reply if it is text, privacy and verified )

1 answer

0

As I see it, 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, correct ?

I 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, see how I did:

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

The structure of the fields was more or less 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 will pick up in the 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 I helped friend.

Browser other questions tagged

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