开发者

Retrieve POST data without knowing exact number of fields

I'm creating an online poll from scratch which will be held in a database. I'm working 开发者_运维技巧on getting a system set up so someone can create a new poll.

I will be having the user fill out a simple HTML form with the Questions and Answers (there may be several answers). The user will be able to add multiple questions and multiple answers for each question.

As the total number of questions and answers will be decided by the user, I need to create some clever PHP to cater for this - however many there are.

When dealing with a static number of questions, it's simple. But I'm having trouble thinking of a way to get all the POST data into individual PHP variables so I can process them.

I was thinking of using a foreach loop, anyone got any ideas?

Sorry for the long winded description! If anyone needs anything clarified, I'd be happy to do so. My problem is that I can't get my head around how to deal with the POST values when I don't know exactly which element of the array will contain what. If things were static with a set number of questions and answers, I'd know $_POST[0] was Question1, etc

Thank you! =)


$_POST should always been an associative array, with the name of the form element mapping to the index of the array. You should never have to access $_POST[0] unless you're specifically naming your form fields 0. Instead, if your element is <input name="field1" /> you'd find the submitted value at $_POST['field1'].

Keep in mind that you can force PHP to interpret incoming form elements as array values by appending array indices to their names:

<input name="question[]" type="text" />
<input name="question[]" type="text" />

When posted back to you, $_POST['questions'] will contain an array of all of the question fields.

If you're dynamically adding form elements to your page to allow users to add multiple questions, just be sure to name them appropriately and PHP will handle building the array for you.

You'll be able to loop over the incoming post data with something resembling:

foreach ($_POST['questions'] as $question) {
   // sanitize/save your user's question
}


When you name your input fields like questions[], they'll be passed in the parameters like this: questions[]=Q1&questions[]=Q2 (whether in the query string or the post body). PHP will convert those into an array in $_POST['questions'] where you can iterate through them easily. You can also specify the key explicitly if you need to have multiple fields combine into a single array.

The beauty in this comes when you need to nest data, so "question 1" has a question string, n answers, and maybe a setting option:

<input type="text" name="questions[0]">
<input type="text" name="questions[0][answers][]">
<input type="text" name="questions[0][answers][]">
<input type="text" name="questions[0][answers][]">
<input type="text" name="questions[0][answers][]">
<input type="text" name="questions[0][option][foo]">

…converts into…

[questions] => Array
    (
        [0] => Array
            (
                [answers] => Array
                    (
                        [0] => answer1
                        [1] => answer2
                        [2] => answer3
                        [3] => answer4
                    )

                [option] => Array
                    (
                        [foo] => bar
                    )

            )

    )


foreach will work.

foreach($_POST as $name => $item)

$name will contain the name of the value, by which you can identify which value it is. It will contain 'question1' for the field that was named 'question1'. :)


Create multiple input fields with name ending with [], i.e:

ans1: <input type=text name='ans[]'>
ans2: <input type=text name='ans[]'>
name: <input name='name'>

Then all values will appear as an array inside $_POST array.:

array(2) {
  ["ans"]=>
  array(2) {
    [0]=>
    string(3) "LOL"
    [1]=>
    string(3) "WTF"
  }
  ["name"]=>
    string(4) "John"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜