Hello.

How is it done so that within one PHP file, for example, anketa.php , you can go to the next item in the same file (by pressing the "Next" button to bring up the next item, for example).

Let there be such a situation.

  1. The user goes to the profile page ( anketa.php ).
  2. There is one input field and below button NEXT.
  3. He enters the name "Dude" and presses the NEXT button.
  4. A new input field appears, it says: "What do you smoke, brother?"
  5. He writes the word Banzai and presses MORE.
  6. 2 words appear:

    Name: Dude
    Smokes: Banzai
    From now on you are recorded in the database

I want to learn how to do this within the same file.

I can horrible method. In the anketa.php file anketa.php first field, press NEXT (the variable goes into the session) transfers it to the anketa2.php file, there it enters what it smokes, presses NEXT (this also into the session, total in 2 variables) and in the anketa_result.php file what he wrote, and immediately written to the database.

But how is all this in one file, I do not know (like anketa.php?question=2 ).

Please, tell me a simple code so that I understand it further, or a link to an article may be.

  • There was already a similar question about step-by-step registration - Deonis
  • You have a little bit of fundamental knowledge in the subject area. I do not know where it is better to advise you to start, but, as regards this issue, I would advise you to read about the state machines ( ru.wikipedia.org/wiki/… ), about the states, about the stateless protocol, which is HTTP ( en.wikipedia .org / wiki / Stateless_protocol ). - Gino Pane
  • PHP is the server code, and what you explain is the client part. You can use a plugin like github.com/wbotelhos/stepy to create a form and at the very end send data to the server using the standard SUBMIT, displaying the entered data using JavaScript. - Daniel Protopopov

4 answers 4

A couple of options:

1) do it dynamically within one page, without reloading, on JS (jQuery);

2) depending on the parameters that have arrived, show different forms from anketa.php : no parameters: the first empty field; have a name - show the name and an empty what you smoke; there is this and that - the third option. If Name is - show the form where this field contains the entered value and this field has an attribute readonly="readonly" . The values ​​of the form should be transferred by POST

 $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); $brand = filter_input(INPUT_POST, 'brand', FILTER_SANITIZE_STRING); if( empty($name) && empty($brand)) { // показываем форму с пустым полем для имени } elseif( !empty($name) && empty($brand)) { // показываем форму с заполненным именем readonly или вообще hidden и пустым полем для brand } elseif( !empty($name) && !empty($brand)) { // Вас зовут Вася и вы курите Беломор } else { // фигня какая-то, этого не может быть : ) } 

    Not serious all this - just 4 days ago .

     $question = isset($_GET['question']) ? $_GET['question'] : 1; if ($question == 1) { // магия первого вопроса } // еще магии? 

    And how it will be requested (with reboot or ajax) the script is almost parallel.

    Sessions remain sessions - in them and store.

    I repeat. In order for the result to look cultural and be easily maintained, you need to think it over carefully. Read how I work with routing in any frameworks.

    • Link to "4 days ago" 404 - Sergiks
    • @sergiks, thanks. Link updated - xEdelweiss
     <?php session_start(); $steps = array('name','smoke'); $_SESSION['user'] = array('name'=>'','smoke'=>''); $_SESSION['step'] = 0; $step = $steps[$_SESSION['step']]; if(!empty($_POST[$step])){ $_SESSION['user'][$step] = $_POST[$step]; $_SESSION['step']++; if($_SESSION['step']>=count($steps)){ print('Отныне ты записан в БД'); } } $questions = array('Как тебф зовут?','Что ты куришь?'); ?> <form action='anketa.php' method='post'> <label><?php print $questions[$step];?><input type='text' name='<?php print $step?>'></label> <input type='submit' value='ok' /> </form> 

      On the button, you then send the forms to the same file, but with a different parameter, for example step. Inside pkhp, you take this parameter from $ _POST or $ _GET and display the corresponding page.