获得每个动态文本框的值并投入每数值列 [英] Get the value of each dynamic text box and put into array per number

查看:101
本文介绍了获得每个动态文本框的值并投入每数值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //所有选项的值放在一个阵列每个号码
$ arr_options =阵列();
$ arr_options = $ _ POST ['域'] ['选项'];
后续代码var_dump($ arr_options);

输出:

 阵列(大小= 5)
 0 =>字符串'狗'(长度= 3)
 1 =>字符串'猫'(长度= 3)
 2 =>字符串'红'(长度= 3)
 3 =>字符串'蓝'(长度= 4)
 4 =>字符串'白'(长度= 5)

指数0.1对于问题#1和选择;指数2,3,4对于问题2

  $ number_of_fields =计数($ _ POST ['域'] ['问题']);为($ I = 0; $ I< $ number_of_fields; $ I ++)
{
  $字段=阵列
    (
       '问题'=> $ _ POST ['域'] ['问题'] [$ i],
       '选项'=> $ _ POST ['域'] ['选项'] [$ i],
    )
   后续代码var_dump($场)
}

输出:

 阵列(大小= 2)
'问题'=>字符串'问题1'(长度= 10)
'选项'=>字符串'狗'(长度= 3)阵列(尺寸= 2)的
'问题'=>字符串'问题2'(长度= 10)
'选项'=>字符串'猫'(长度= 3)

期望的输出:

 阵列(大小= 2)
'问题'=>字符串'问题1
'选项'=>字符串'狗','猫'//选择的问题#1阵列(尺寸= 2)的
'问题'=>字符串'问题2'
'选项'=>字符串'红','蓝','白'//对于问题2个选择

<青霉>注意:我的选择是动态的,因此这意味着每一个问题都可以有不同数目的选择的

这是我的HTML code:

 &LT;形式方法=邮报行动=insert_question.php&GT;    &LT; textarea的名字=域[问题] []&GT;&LT; / textarea的&GT;    &LT; textarea的名字=字段[选项] []&GT;&LT; / textarea的&GT;&LT; /表及GT;


解决方案

 &LT;形式方法=邮报行动=insert_question.php&GT;
    &LT; textarea的名字=问题[]&GT;&LT; / textarea的&GT;
    &LT; textarea的名字=选项[]&GT;&LT; / textarea的&GT;
&LT; /表及GT;

新版本的PHP code:

  $字段=阵列();
$ number_of_fields =计数($ _ POST ['场']);
为($ I = 0; $ I&LT; $ number_of_fields; $ I ++){
    $场[$ i] [问题] = $ _ POST [字段] [问题] [$ i];
    的foreach($ _ POST [字段] [选项] [$ i]为$选项){
        $场[$ i] [选项] [] = $选项;
    }
}

// ALL OPTIONS VALUES PUT INTO ONE ARRAY PER NUMBER
$arr_options = array();
$arr_options = $_POST['fields']['options'];
var_dump($arr_options);

OUTPUT:

 array (size=5)
 0 => string 'dog' (length=3)
 1 => string 'cat' (length=3)
 2 => string 'red' (length=3)
 3 => string 'blue' (length=4)
 4 => string 'white' (length=5)

Index 0,1 are choices for Question # 1 & Index 2,3,4 are for Question # 2

 $number_of_fields = count($_POST['fields']['questions']);

for ($i = 0; $i < $number_of_fields ; $i++) 
{
  $field = array
    (
       'question' => $_POST['fields']['question'][$i],
       'options' =>  $_POST['fields']['options'][$i],
    )
   var_dump($field)
}

OUTPUT:

array (size=2)
'question' => string 'question 1' (length=10)
'options' => string 'dog' (length=3)

array (size=2)
'question' => string 'question 2' (length=10)
'options' => string 'cat' (length=3)

EXPECTED OUTPUT:

array (size=2)
'question' => string 'question 1' 
'options' =>  string 'dog', 'cat' //choices for question #1

array (size=2)
'question' => string 'question 2' 
'options' => string 'red','blue','white' //choices for question #2

Note: My options are dynamic, so it means that every question can have a different number of options

This is my HTML code:

<form method="post" action="insert_question.php">

    <textarea name="fields[question][]"></textarea>

    <textarea name="fields[options][]"></textarea>

</form>

解决方案

<form method="post" action="insert_question.php">
    <textarea name="question[]"></textarea>
    <textarea name="options[]"></textarea>
</form>

the new PHP code:.

$field = array();
$number_of_fields = count($_POST['fields']);
for ($i = 0; $i < $number_of_fields ; $i++) {
    $field[$i]["question"] = $_POST["fields"]["question"][$i];
    foreach($_POST["fields"]["options"][$i] as $option){
        $field[$i]["options"][] = $option;
    }
}

这篇关于获得每个动态文本框的值并投入每数值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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