forms - how to structure server side script with php

232

I am trying to start an online service. Users fill out a form submit it then go to another page. On the 2nd page they choose a service. From that page they go to a 3rd page. I would like to do this in 3 pages as to encourage users to finish the sequence (they are already invested time-wise by 2nd page).

How could I better send data from page 1 to page 3? So far I was just going to send data from page 1 to a server side php script, send that data to page 2 then repeat the process until page 3. Is there a more proper way to accomplish this?

my code for the form submit is

<?php
$varRedirect = "order.php";

if(isset($_POST["submit"]) )
{

  $var1 = $_POST["element_1"];
    $var2 = $_POST["element_1"];
      $var3 = $_POST["element_1"];
        $var4 = $_POST["element_1"];
          $var5 = $_POST["element_1"];
            $var6 = $_POST["element_1"];
              $var7 = $_POST["element_1"];
                $var8 = $_POST["element_1"];
                  $var9 = $_POST["element_1"];
}

/* begin switch logic */


redirect($varRedirect."?var1=".$var1."&var2=".$var2."&var3=".$var3."&var4=".$var4."&var5=".$var5."&var6=".$var6."&var7=".$var7."&var8=".$var8."&var9=".$var9);
/* functions */
function redirect($url, $statusCode = 303)
{
header('location: ' .$url, true, $statusCode);
die();
}
?>
228

Answer

Solution:

if you prefer redirect, you can simplify the process by usinghttp_build_query

$params = $_POST;
unset($params['submit']); // exclude 'submit' if you don't need it
$query = http_build_query($parms); 
redirect($varRedirect."?$query);

not checked well, but you can see the idea.

154

Answer

Solution:

Recommend using session. There you can store data for a user through pages. http://php.net/manual/en/features.sessions.php

881

Answer

Solution:

Wow! Don't use get so send forms unless this is an search form! Send data with POST between pages!

Now you have few solutions:

  1. Simple and easy solution is to use JavaScript (for example some jQuery plugin) and create one page with 3 forms. Then validate and show forms on page by javascript and store data in javascript variables. On the final form send it to database.
  2. Serialize data of your form, and store form's data in the user's session. In such way you will get the data on every page. On last form just use collected data and clear the session.
  3. Serialize the form's data and store in cookies.
  4. When you want full control and statistics about your forms, you can insert the data to database after every form submitting. The first form submitting will return an unique ID from database. Then you can use GET REDIRECT with?some_id=332 appended to the url and every next form submitting save data to database using?some_id=332 . In such way you don't need to pass data between forms. You can get it always form the database by?some_id=332. Moreover, in such way user can to do refreshes, next/prev editing. You just need to check if?some_id=332 is provided in the url, and if yes - load the data to form from database.

People are also looking for solutions to the problem: php - Getting file name after uploading the file in CI

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.