在表单中提交多个字段(PHP) [英] Submitting multiple fields in a form (PHP)

查看:81
本文介绍了在表单中提交多个字段(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个页面可以动态填充MySQL数据库中的一项调查(问题,答案为单选按钮/复选框)。生成的HTML看起来像这样:

 < form id =form1name =form1method =post行动=  > 
1。你如何分类自己?
< br />
< input type =radioname =radio [0]id =radio [0]value =Alien/> Alien
< br />>
< input type =radioname =radio [0]id =radio [1]value =Hobbit/> Hobbit
< br />>
< input type =radioname =radio [0]id =radio [2]value =Tree/> Tree
< br />< br />

2。你是谁?
< br />
< input type =radioname =radio [1]id =radio [3]value =骆驼收藏家/>骆驼收藏家
< br />
< input type =radioname =radio [1]id =radio [4]value =sadasd/> sadasd
< br />>
< input type =radioname =radio [1]id =radio [5]value =Voolome/> Voolome
< br />
< input type =radioname =radio [1]id =radio [6]value =31231235/> 31231235
< br />>
< br />
3。测试问题
< br />
< input type =radioname =radio [2]id =radio [7]value =Nobody Knows/>没有人知道
< br />>
< input type =radioname =radio [2]id =radio [8]value =Somebody Knows/> Somebody Knows
< br />>
< input type =radioname =radio [2]id =radio [9]value =Who Knows/> Who Knows
< br />>
< br />
4。测试问题2
< br />
< input type =radioname =radio [3]id =radio [10]value =Answer1/> Answer1
< br />< br />
5。 First Multiple
< br />
< input type =checkboxname =Check4value =Bike>答案一< br>

< br />
< input type =checkboxname =Check4value =Bike> Answer Two< br>

< br />
< input type =checkboxname =Check4value =Bike> Answer Three< br>

< br />< br />
6。首次开放!
< br />
< input type =textname =Ans5/>

< br />< br />
< / form>

一些重要的事情要注意:


  1. 有3种类型的问题,选择 - 单选(单选按钮); 多个 - 多选(复选框); 打开 - 用户输入(文本框)。

  2. 每个元素的名称对应于相应的问题编号(问题旁边显示的编号是问题+ 1(因为它从0开始) 。[例如,问题14将以Radio [14]为名称。

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



支持问题:它也是可以验证这些问题以确保为每个问题选择了至少一个选项吗?(检查textbox!=很容易,但我怎样才能做到这一点的单选按钮/复选框?)



用于生成此表单的PHP代码可以在需要时提供!它基本上使用一个变量来存储问题编号($ qno),该编号用作计数器,同时循环语句以提取数据来自MySQL,找出答案的类型,并放置approp在表单上打开控件。 你的 $ _ POST 数组和 radio2 而不是 radio [2] 即使你的工作也是如此,或者在所有单选按钮中使用名称 radio [] ,您将获得包含所有选定单选按钮的数组。



此外,已检查的选项应位于同一个 $ _ POST 数组中的数组中

您为复选框使用了一个简单的名称,这只会将检查的最后一个值发送到您的php脚本中,即使更多 $ b

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



为了显示答案,您可以遍历 $ _ POST的值就像这样:

 <?php 

if($ _POST [的ubmit']){

foreach($ _ POST为$ key => $ value){
echo输入名称:$ key值:$ value; //添加要排除的条件你的按钮或隐藏字段
}
}
?>


I currently have a page which dynamically fills in a survey (Questions, Answers as Radio Buttons/Checkboxes) from a MySQL database. The generated HTML looks something like this :

  <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>

A few important things to note :

  1. There are 3 types of questions, "Choice" - Single choice(Radio Button); "Multiple" - Multiple Choice(Check box); "Open" - User Input (Text Box).
  2. Each element's name corresponds to the appropriate question number (The number shown next to the question is Question+1 (Since it starts at 0). [For example, Question 14 would have Radio[14] as the name.

My Main Question : How can you submit these fields to be stored into the Database? I am trying to figure out how to write code which will find out which option is selected for each question.

Side Question : Is it also possible to validate these questions to ensure atleast one option is selected for each question? (Checking that textbox!="" is easy, but how would I do this for Radio Button/Checkboxes?)

PHP Code used to generate this form can be provided if needed! It is essentially using one variable to store the question number ($qno), which is used as a counter while looping the statements to pull data from MySQL, Figure out the type of answer, and place the appropriate controls on the form.

解决方案

Option that is selected , will be in your $_POST array and radio2 instead of radio[2] even if yours works too, or use name radio[] in all of your radio buttons ,you will get array that contains all radio buttons that are selected.

Also , options that are checked should be in an array that is in the same $_POST array

You use a simple name for checkbox,this will only send the last value checked to your php script and will work as radio even if more than one value is checked so:

Instead of name="Check4" it must be name="Check4[]".

And for displaying answers , you can iterate over values of $_POST simply like this :

<?php

 if($_POST['submit']) {

    foreach($_POST as $key=>$value){  
      echo "Input name : $key Value:$value";//add condition to exclude your button or hidden fields
    }
  }
 ?>

这篇关于在表单中提交多个字段(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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