我目前有一个页面可以从MySQL数据库中动态填写调查问题(问题,答案为单选按钮/复选框)。生成的HTML看起来像这样:
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[0]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[0]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[0]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio[1]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio[1]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio[1]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio[1]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio[2]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio[2]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio[2]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio[3]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
</form>
需要注意的一些重要事项:
我的主要问题:如何提交这些字段以存储到数据库中?我想弄清楚如何编写代码,找出每个问题选择哪个选项。
方面问题:是否也可以验证这些问题以确保为每个问题选择至少一个选项? (检查textbox!=“”很简单,但我如何为单选按钮/复选框执行此操作?)
如果需要,可以提供用于生成此表单的PHP代码!它本质上是使用一个变量来存储问题编号($ qno),它用作计数器,同时循环语句以从MySQL中提取数据,找出答案的类型,并在表单上放置适当的控件。
答案 0 :(得分:1)
选项已选,将在您的$_POST
数组和radio2
中,而不是radio[2]
,即使您的工作也是如此,或使用名称{{1在所有单选按钮中,您将获得包含所有选定单选按钮的数组。
此外,选中的选项应位于同一radio[]
数组中的数组
您使用一个简单的名称作为复选框,这只会将最后检查的值发送到您的php脚本,并且即使检查了多个值也将作为无线电工作:
而不是$_POST
,它必须是name="Check4"
。
为了显示答案,您可以像这样迭代name="Check4[]"
的值:
$_POST
答案 1 :(得分:0)
做这样的事情:
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" id="radio[0]" value="Alien" name="question1" />Alien
<br />
<input type="radio" id="radio[1]" value="Hobbit" name="question1" />Hobbit
<br />
<input type="radio" id="radio[2]" value="Tree" name="question1" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" id="radio[3]" value="Camel Collector" name="question2" />Camel Collector
<br />
<input type="radio" id="radio[4]" value="sadasd" name="question2" />sadasd
<br />
<input type="radio" id="radio[5]" value="Voolome" name="question2" />Voolome
<br />
<input type="radio" id="radio[6]" value="31231235" name="question2" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" id="radio[7]" value="Nobody Knows" name="question3" />Nobody Knows
<br />
<input type="radio" id="radio[8]" value="Somebody Knows" name="question3" />Somebody Knows
<br />
<input type="radio" id="radio[9]" value="Who Knows" name="question3" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" id="radio[10]" value="Answer1" name="question4" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" value="Bike" name="question5[]">Answer One<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Two<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="question6" />
<br /><br/>
</form>
答案 2 :(得分:0)
验证单选按钮使用此:
if($("#radio:checked").length==0)
{
alert("Please Select atleast one");
return false;
}
答案 3 :(得分:0)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio1[]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio1[]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio1[]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio1[]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio2[]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio2[]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio2[]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio3[]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
<input type="submit" name="submit">
</form>
</body>
</html>
PHP代码
<?php if($_POST['submit']) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;
}
?>
<强>输出强>
Array
(
[radio] => Array
(
[0] => Hobbit
)
[radio1] => Array
(
[0] => sadasd
)
[radio2] => Array
(
[0] => Somebody Knows
)
[Ans5] =>
[submit] => Submit Query
)