4

我正在用 PHP 编写简单的远程投票系统,但在以 php 形式控制动态选项和组时遇到问题。

我想要这样的东西,但是....(在我的帖子底部询问):

http://imageshack.us/photo/my-images/803/97450434.png/

我有以下代码:

html:

<fieldset style="width:99%; float:left">
         <div id="dynamicInput">
              Group 1<br><input type="text" name="myGroupInputs[]">
                 <div id="dynamicInput" style="margin-left:50px;">
                      Option 1<br><input type="text" name="myOptionsInputs[]">
                 </div>
         </div>
             <input type="button" value="Add another group" onClick="addGroup('dynamicInput');">
             <input type="button" value="Add another option" onClick="addInput('dynamicInput');">         
</fieldset>

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​js:

var counter_option = 1;
var limit_option = 10;
var counter_group = 1;
var limit_group = 10;
function addInput(divName){

     if (counter_option == limit_option)  {
           alert('You cannot add more options.');
     }
    else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<font style='margin-left:50px;'>Option</font> " + (counter_option+ 1) + " <br><input type='text' style='margin-left:50px;' name='myOptionsInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter_option++;
     }
}
function addGroup(divName){

     if (counter_group == limit_group)  {
           alert('You cannot add more groups.');
     }
    else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Group " + (counter_group + 1) + " <br><input type='text' name='myGroupInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter_group++;
     }
}

1.如果我要添加另一个组,如何编辑此代码以使每个选项都依赖于正确的组并将选项计数器重置为0?
组 1、选项 1、选项 2;
组 2,选项 1,选项 2;等等

2. 稍后如何将这些值放入 php 变量、数组或…… - 我想将这些值插入数据库(php 脚本)。脚本必须知道该选项对应于正确的组。

我的数据库有两个表:组和选项(FK 到组)

链接到 jsfiddle 编辑:http: //jsfiddle.net/mnyyK/

请帮忙....

4

1 回答 1

1

您可以使用多维数组。

将组的名称更改为:

<div id="dynamicInput">
  Group 1
  <br>
  <input type="text" name="groups[0][name]">
  <div id="dynamicInput" style="margin-left:50px;">
    Option 1
    <br>
    <input type="text" name="groups[0][options][]">
  </div>
</div>

在添加新组时增加groups' 索引,例如

"<input type='text' name='groups[" + counter_group + "][name]'>"

"<input type='text' name='groups[" + counter_group + "][options][]'>"

您应该能够以这种方式在 PHP 中处理组:

foreach ($_POST['groups'] as $group) {
  $name = $group['name'];
  $options = $group['options'];
  ...
}

更新

为了消除混淆,这里是实际的代码:

var counter_group = 0;
var limit_group = 10;
var counter_option = 0;
var limit_option = 10;

var addInput = function(divName) {
  if (counter_option == limit_option - 1) {
    alert('You cannot add more options.');
  } else {
    counter_option++;
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "<font style='margin-left:50px;'>Option</font> " + (counter_option + 1) + "<input type='text' name='groups[" + counter_group + "][options][]'>";
    document.getElementById(divName).appendChild(newdiv);
  }
}

var addGroup = function(divName) {
  if (counter_group == limit_group - 1) {
    alert('You cannot add more groups.');
  } else {
    counter_group++;
    counter_option = -1;
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "Group " + (counter_group + 1) + "<input type='text' name='groups[" + counter_group + "][name]'>";
    document.getElementById(divName).appendChild(newdiv);
  }
}
于 2012-12-16T13:12:55.187 回答