简体   繁体   中英

Best practice for submitting nested form data to PHP?

The Problem

I'm trying to figure out the best way to submit data from your typical HTML form to a PHP file. The data looks something like this:

  1. question 1
    • answer choice 1
    • answer choice 2
    • answer choice 3
  2. question 2
    • answer choice 1
    • answer choice 2

My Solution

This is where my question comes in. Would it be appropriate to use a multi-dimensional array? As you can see from my example above, the number of choices could vary for each question. So why not store the number of choices per each question into the second slot of each row of choices (the first slot would be the string for the question text):

  • [0][0] : String for question 1
  • [0][1] : Number of choices for question 1
  • [0][2] : Answer choice for question 1
  • [0][3] : Answer choice for question 2
  • [0][4] : Answer choice for question 3
  • [0][5] : Answer choice for question 4
  • [1][0] : String for question 2
  • [1][1] : Number of choices for question 2
  • [1][2] : Answer choice for question 2
  • [1][3] : Answer choice for question 2

I feel this is not a good approach however, because it would be difficult to change later. Is there some way of passing objects into PHP without the need for JavaScript?

If you know questions ids you will post only answers array

<input type="radio" name="answer[<quest_id>]" value="0">
<input type="radio" name="answer[<quest_id>]" value="1">
<input type="radio" name="answer[<quest_id>]" value="2">
<input type="radio" name="answer[<quest_id>]" value="3">
<input type="radio" name="answer[<quest_id>]" value="4">

After post you will have answers array

foreach($_POST['answer'] as $question_id => $answer)

Well, you don't have to be so reliant on numeric indexes since you can use the form input names to build your own structures. For example:

  • question0_string : String for question 1
  • question0_choices[0] : Answer choice for question 1
  • question0_choices[1] : Answer choice for question 2
  • question0_choices[2] : Answer choice for question 3
  • question0_choices[3] : Answer choice for question 4
  • question1_string : String for question 2
  • question1_choices[0] : Answer choice for question 2
  • question1_choices[1] : Answer choice for question 2

...and then grab the form names by looking at $_POST in your php, perhaps with array_walk() and a callback function that split() s the array_keys() by _ and puts them in a new array. You can use preg_match() instead of split to make parsing out the question numbers easier. On my way to bed, but you can probably figure it out with some trial and error.

Your solution IMO, is the best possible choice - I would just remove the [*][1] indexes since their value can be easily calculated in PHP by just doing:

count($nested[$i]) - 1

However you can also follow another approach:

  • ['String for question 1'][0] : Answer choice #1 for question 1
  • ['String for question 1'][1] : Answer choice #2 for question 1
  • ['String for question 1'][2] : Answer choice #3 for question 1
  • ['String for question 1'][3] : Answer choice #4 for question 1
  • ['String for question 2'][1] : Answer choice #1 for question 2
  • ['String for question 2'][2] : Answer choice #2 for question 2

And in PHP:

foreach ($nested as $question => $answers)
{
    foreach ($answers as $answer)
    {
    }
}

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