Error with the for loop

1

I want this to be displayed one by one the contents of my form, however it shows me everything at once, the right would be to display the next only after clicking send and this data I will save in an array. >

Would it be my logic that is wrong? could someone give me a hand.

Follow my test:

<?php
$totalPerguntas = 2;
$quantidadeVezes =0;

while($quantidadeVezes <= $totalPerguntas){
    for ($i = 1; $i <= $totalPerguntas; $i++) {
        echo '<input type="text" name="produto[1][nome]" value="nome do produto" />';
        echo '<input type="text" name="produto[1][valor]" value="valor do produto" />';
        echo '<input type="text" name="produto[1][codigo]" value="codigo do produto" />';
        echo '<button>Enviar</button>'; 
    }
    $quantidadeVezes++;
}
?>
    
asked by anonymous 19.10.2017 / 12:41

1 answer

0

The way you developed the code will actually show all the info at once, only with php you will not be able to do this, because php works like this, it receives a request from the server and the server responds with the page ready (html, css, js) that was mounted by php.

So what's going on here is the following, you made the request and he returned all the information at once, the best way to solve your problem is, assemble the static form and put an action on the button to call one ajax, with ajax you can get php information in the background and play on the screen, so every time you hit the button this action will mount the "new" screen for you.

If you have no idea how to do this, see this answer: Adding more fields in form dynamically in Jquery

It teaches you how to dynamically add fields to a form.

Update

This photo will help you with your problem.

You'll put up an initial form on this page you will have a jquery for example, this jquery through ajax will call in the background a php page, this php page will manipulate the data, fetch the information in the bank and after all ready to build a json, this json will be sent back to jquery, and finally jquery will build the new fields on the screen, so you'll have to study about:

  • jquery (It will manipulate the screen)
  • ajax (It will perform actions with php in the background)
  • json (This is the language you will use to exchange messages with js)
  • php (I believe you already know how it works)
19.10.2017 / 13:00