开发者

use a variable to access $_POST

I have a form with a lot of textareas.

<开发者_如何转开发;textarea cols="30" rows="6" id="q1"></textarea>
<textarea cols="30" rows="6" id="q2"></textarea>
<textarea cols="30" rows="6" id="q3"></textarea>
...
<textarea cols="30" rows="6" id="q54"></textarea>

Don't ask why we need 54 questions.

I want to print them out but don't want to do it manually.

$i = 1;
while ($i <= $countTextareas) {
  $questID = "q" . $i;

  $question = $_POST[$questID];

  echo "Question " . $i . ": " . $question . "<br />";

  $i++;
}

The result of that code is:

Question 1: <br />

Any assistance or even a point in the general direction would be great.

Thanks in advance.

It may not be the most elegant solution but it works...

$i = 1;
while ($i <= $countTextareas) {
    $question = $_POST['question'][$i];
    echo "Question " . $i . ": " . $question . "<br />";
    $i++;
}


How about good old foreach?

foreach ($_POST as $key => $value) {
    echo 'Question '.$key.': '.$value.'<br />';
}


Since you are using PHP, you should exploit PHP's nifty feature of turning a name attribute like question[] into an array.

So if you had...

<textarea name="question[]" rows="5" cols="5">
</textarea>

<textarea name="question[]" rows="5" cols="5">
</textarea>

Your $_POST would be...

question[0] = 'whatever';
question[1] = 'something else';
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜