php php表单的多个单选按钮数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20960036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple radio button array for php form
提问by taniakeira
I'm new to this. I'm studying web development and have to create a php response form for a questionnaire which then inputs into a database. I'm having trouble with the radio buttons. I can't create the right code that makes an array and displays the answers in the response form/page.
我是新手。我正在研究网络开发,必须为问卷创建一个 php 响应表单,然后将其输入到数据库中。我在使用单选按钮时遇到问题。我无法创建正确的代码来创建数组并在响应表单/页面中显示答案。
This is my code:
这是我的代码:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer[1]" value="6"> 5<input type="radio" name="answer[1]" value="5">
4<input type="radio" name="answer[1]" value="4"> 3<input type="radio" name="answer[1]" value="3">
2<input type="radio" name="answer[1]" value="2"> 1<input type="radio" name="answer[1]" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer[2]" value="6"> 5<input type="radio" name="answer[2]" value="5">
4<input type="radio" name="answer[2]" value="4"> 3<input type="radio" name="answer[2]" value="3">
2<input type="radio" name="answer[2]" value="2"> 1<input type="radio" name="answer[2]" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer[3]" value="6"> 5<input type="radio" name="answer[3]" value="5">
4<input type="radio" name="answer[3]" value="4"> 3<input type="radio" name="answer[3]" value="3">
2<input type="radio" name="answer[3]" value="2"> 1<input type="radio" name="answer[3]" value="1">
</p>
I know the answer can relate to the isset function but I don't know how to code it. Could someone possibly teach or help me out here?
我知道答案可能与 isset 函数有关,但我不知道如何对其进行编码。有人可以在这里教我或帮助我吗?
回答by sjagr
When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST)the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.
当您不确定如何处理您设置的 HTML 标记时,您应该var_dump($_POST)了解发送到 PHP 处理程序页面的值,以便您知道格式是什么样的,这样您就可以从那里继续。
When I created your HTML and tested it with a var_dumpand some random selections, the output was
当我创建您的 HTML 并使用 avar_dump和一些随机选择对其进行测试时,输出为
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
Notice that there is an array within the $_POST['answer']variable. So you should foreachover each element in that array to handle each respective value:
请注意,$_POST['answer']变量中有一个数组。因此,您应该foreach遍历该数组中的每个元素来处理每个相应的值:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
If you need to work with the answer number that you defined in the POSTarray, you can foreachwith a key:
如果您需要使用您在POST数组中定义的答案编号,您可以foreach使用一个键:
foreach ($_POST['answer'] as $answerNum => $answer) {
// do stuff with $answerNum and $answer
}
You can, of course, access your answer by its number directly:
当然,您可以直接通过号码访问您的答案:
if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
// do stuff with $_POST['answer'][1]
}
回答by bobkingof12vs
I don't imagine that this is quite what you are looking to do. If instead you give the three questions different names:
我不认为这正是你想要做的。如果你给这三个问题起不同的名字:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer1" value="6"> 5<input type="radio" name="answer1" value="5">
4<input type="radio" name="answer1" value="4"> 3<input type="radio" name="answer1" value="3">
2<input type="radio" name="answer1" value="2"> 1<input type="radio" name="answer1" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer2" value="6"> 5<input type="radio" name="answer2" value="5">
4<input type="radio" name="answer2" value="4"> 3<input type="radio" name="answer2" value="3">
2<input type="radio" name="answer2" value="2"> 1<input type="radio" name="answer2" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer3" value="6"> 5<input type="radio" name="answer3" value="5">
4<input type="radio" name="answer3" value="4"> 3<input type="radio" name="answer3" value="3">
2<input type="radio" name="answer3" value="2"> 1<input type="radio" name="answer3" value="1">
</p>
then in php all you will need to do is find the values using the $_POSTvariable as such
然后在 php 中,您需要做的就是使用$_POST变量查找值
<?php
echo $_POST['answer1'];
echo $_POST['answer2'];
echo $_POST['answer3'];
?>

