简体   繁体   中英

Form table based on previous inputs

I'm new to working with PHP and have a problem I can't seem to find the answer to.

I have to create an event creation form spread across multiple pages. On one page there is to be a table that vertically will have ticket types listing only ones selected on a previous page and horozontally there is a 'sections' column based also on a previous page. The table will then allow the user to enter a price to then be assigned to the ticket type depending on the different sections.

My question being, is it possible to do this with html/PHP alone? I am using the POST method to move between the pages of the form and then adding it all to the database at the end but should I do it a different way to achieve this?

I hope the question makes sense but I'm coming from a non lingo position.

There are a few approaches to take:

  1. Use the POST method as you described.
  2. Use session variables to store data across pages.
  3. Use jQuery/javascript to hide/show different forms on one page (takes out the need to multi POST or store data in session).

What you can do (not necessarily should , that's your own judgment call) is save all the POST ed data as session variables. This can help you clean up code clutter, as otherwise you'll have to POST all the data from all the forms to all the forms in order to maintain the information through all page navigations.

As long as the first page has a form method with POST this could be done. Here are the steps:

  1. On the first page, the user will submit the data by the POST method.
  2. On the second page, save the data in a temporary variable as such : $temp_var = $_POST['data'];
  3. Create the second page with data based on first.
  4. (On submit) Save the new data with old data into database

This sounds like a good case for sessions !

On your first page, your form can submit with POST and then you can store the data in the user's session:

session_start();
$_SESSION['form_submission'] = $_POST;

Then, on page #2 (or any other page), you can access it with:

session_start();
$form_variable = $_SESSION['form_submission']['field_name'];

You can more-generally store/retrieve them to make it more readable as well:

$_SESSION['field1'] = $_POST['field1'];
$field1 = $_SESSION['field1'];

PHP and HTML will be fine to manage the kind of system you have in mind.

To get you started:

PHP uses sessions to manage data from one page to the next creating a process like this:

  1. User fills in first form
  2. POST request to web server with data
  3. PHP script interprets data and creates a new view on the page or redirects to another page.

in your PHP scripts at the top of EVERY page you use:

session_start();

You can now save data to the session:

$_SESSION['mySessionVar'] = 'This is my session variable';

Whenever you need to retrieve from a session, make sure that a) session_start() has been called and b) retrieve it like this:

$myVariable = $_SESSION['mySessionVar'];

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM