php表单的多个单选按钮数组 [英] Multiple radio button array for php form

查看:17
本文介绍了php表单的多个单选按钮数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手.我正在研究网络开发,必须为问卷创建一个 php 响应表单,然后将其输入到数据库中.我在使用单选按钮时遇到问题.我无法创建正确的代码来创建数组并在响应表单/页面中显示答案.

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.

这是我的代码:

<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 函数有关,但我不知道如何对其进行编码.有人可以在这里教我或帮助我吗?

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?

推荐答案

当您不确定如何处理已设置的 HTML 标记时,您应该 var_dump($_POST)发送到 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['answer'] 变量中有一个数组.所以你应该 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆