-1

我目前有一个页面,它从 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>

需要注意的一些重要事项:

  1. 有3种类型的问题,“选择” - 单选(单选按钮);"Multiple" - 多项选择(复选框);“打开” - 用户输入(文本框)。
  2. 每个元素的名称对应于适当的问题编号(问题旁边显示的数字是 Question+1(因为它从 0 开始)。[例如,问题 14 将以 Radio[14] 作为名称。

我的主要问题:如何提交这些字段以存储到数据库中?我试图弄清楚如何编写代码来找出每个问题选择了哪个选项。

附带问题:是否也可以验证这些问题以确保为每个问题选择至少一个选项?(检查那个文本框!=“”很容易,但我如何为单选按钮/复选框做到这一点?)

如果需要,可以提供用于生成此表单的 PHP 代码!它本质上是使用一个变量来存储问题编号($qno),它在循环语句以从 MySQL 中提取数据时用作计数器,找出答案的类型,并在表单上放置适当的控件。

4

4 回答 4

1

选择的选项在您的$_POST数组中,radio2而不是radio[2]即使您的也有效,或者radio[]在所有单选按钮中使用名称,您将获得包含所有选中单选按钮的数组。

此外,检查的选项应位于同一 $_POST数组中的数组中。

您为复选框使用一个简单的名称,这只会将检查的最后一个值发送到您的 php 脚本,并且即使检查了多个值,也将作为收音机工作:

而不是name="Check4"它必须是name="Check4[]".

为了显示答案,您可以简单地迭代$_POST以下值:

<?php

 if($_POST['submit']) {

    foreach($_POST as $key=>$value){  
      echo "Input name : $key Value:$value";//add condition to exclude your button or hidden fields
    }
  }
 ?>
于 2013-09-10T11:04:12.513 回答
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>
于 2013-09-10T10:59:45.673 回答
0

验证单选按钮使用这个:

if($("#radio:checked").length==0)
{
  alert("Please Select atleast one");
  return false;
}
于 2013-09-10T11:03:21.140 回答
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
)
于 2013-09-10T11:03:45.690 回答