多个单选按钮数组PHP表单 [英] Multiple radio button array for php form
问题描述
我是新来这个。我正在学习网络开发和必须创建一个PHP的响应形式的问卷,然后输入到数据库中。我在与单选按钮的麻烦。我无法创建正确的code,使一个数组,并显示在响应形式/页的答案。
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.
这是我的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>
我知道答案可能涉及到使用isset功能,但我不知道如何code吧。
可能有人可能教或帮助我在这里?
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?
推荐答案
当你不确定如何处理,你已经设置了,你应该的var_dump($ _ POST)的HTML标记
这让你知道的格式将是什么样子发送到PHP页面处理的值,这样你可以从那里继续。
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
和一些随机选择,产量为
When I created your HTML and tested it with a var_dump
and some random selections, the output was
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
注意,还有就是 $ _ POST ['答案']
变量中的数组。所以,你应该的foreach
在数组来处理每个相应值中的每个元素:
Notice that there is an array within the $_POST['answer']
variable. So you should foreach
over each element in that array to handle each respective value:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
如果你需要与你的 POST
阵列,可以的foreach
在定义的答复号工作一个关键的:
If you need to work with the answer number that you defined in the POST
array, you can foreach
with a key:
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]
}
这篇关于多个单选按钮数组PHP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!