Error with loop for

Asked

Viewed 52 times

1

I wanted this to display one by one the content of my form however it displays me all at once, the right would be it display the next only after clicking send and this data I will save in an array.

Is it my logic that this wrong? could someone give me a helping 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++;
}
?>
  • Hi Fernando. Can you explain better how the code that runs in the browser should "talk" to PHP and generate content? are you using ajax or want it to happen when the page loads?

  • Hi Pablo, I would like it to only be displayed one by one, and only the next form to be displayed after the click of the button, however it is displaying everything at once and it was only to show 2 forms

1 answer

0

The way you developed the code will really show all the information 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 ready page (html, css, js) which was built by php.

So what’s happening there is this, you made the request and it 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 an ajax, with ajax you can take php information in the background and play on the screen, so every time you press the button this action will mount the "new" screen for you.

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

It teaches how to add fields dynamically in a form.

Update

inserir a descrição da imagem aqui

This photo will help you in your trouble.

You will assemble an initial form, on this page you will have a jquery for example, this jquery through ajax will call in background a php page, this php page will manipulate the data, fetch the information in the database 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 will have to study about:

  • jquery (It will manipulate the screen)
  • ajax (It will perform the actions with php in background)
  • json (This is the language you will use to exchange messages with js)
  • php (I believe you already know how it works)
  • I understood the script that I was trying to do something similar only that with changes in php was in pure JS, so I searched with this link and other pages I won’t be able to send what I receive from jquery to php I would have to have an intermediary that I serve the correct json? or has another form?

  • Json is a great alternative!

  • Only problem is that I never used json, it is a file that can be accessed directly by brownser or is not that even php is hidden? using it I can access either with js or php correct?

  • It is difficult to explain here, but I’ll give you a north, is a string, where you can exchange information between javascript and php, for you better understand see this link: https://www.w3schools.com/js/js_json_intro.asp

  • I edited the question to help you with your problem, but in short you will have to study some things to do what you want. @Fernando

Browser other questions tagged

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