在文本框组中输入array


Inputing as array in textbox group

我已经使用JavaScript创建了一个文本框组,即在单击添加按钮时,新的文本框集将被添加到页面中。

var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" value="" >' );
newTextBoxDiv.appendTo("#TextBoxesGroup");

我的PHP页面的DIV类是.

    <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <label>Track #1 : </label>
    <input name="textbox1[]" type='textbox' ><input name="textbox2[]" type='textbox'>
    </div>
    </div>

现在我想创建一个数组,这样,很容易输入这组文本框的值到数据库。我想使用单个数组,而不是使用多维数组,因此textbox1#存储集的值作为数组的键,textbox2#存储集的值作为数组的相应值。

当您通过POST提交表单时,它不会为您的输入字段创建多维数组。相反,它使用字段的name属性作为$_POST数组中的键。

当你创建文本框的时候,让textbox2成为一个数组名,这样你就可以有多个字段,像这样:

var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" id="textbox1' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" id="textbox2' + counter + '" value="" >' );
newTextBoxDiv.appendTo("#TextBoxesGroup");

然后您可以使用$_POST['textbox1']获取键,值将存储在$_POST['textbox2']

然后使用它,假设您想要获得第一个textbox1/textbox2对,您将使用$_POST['textbox1'][0]$_POST['textbox2'][]访问该值。

要循环遍历两个数组并提取所有值(假设textbox1和textbox2字段的数量相同),可以这样做:

$inputs;
$length = count($_POST['textbox1']);
for($i=0; $i< $length; $i++){
$inputs[$_POST['textbox1'][$i]] = $_POST['textbox2'][$i];
}

我认为你不能直接这么做。您必须处理HTML将表单传递给PHP的方式。您不应该为所有文本框使用相同的名称。我认为最简单的方法是使用'textbox1[]'和'textbox2[]'作为名称,所以你从所有的文本框中获得一个数组。然后,通过循环,您可以使用textbox1和textbox2数组以您想要的方式构建一个新数组。

我编写了一些示例代码,向您展示如何使用复选框的不同属性将键映射到复选框数组中的值:

所以你可以控制什么值映射到哪个键…如果你想在复选框数组中插入一个键值,只需执行以下操作:

<input type="checkbox" id="somewhat1" name="checkboxes[key1]" value="value1"/>其中"checkboxes"将是一个包含key1和value1

的数组
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
    <?php
    echo print_r($_POST) . "<br/>";
    $checkboxvalues = $_POST['checkboxes'];
    echo "key1 = " . $checkboxvalues["key1"] . "<br/>";
    ?>
    <form action="index.php" method="post">
        asd1<input type="checkbox" id="somewhat1" name="checkboxes[key1]" value="value1"/><br/>
        asd2<input type="checkbox" id="somewhat2" name="checkboxes[key2]" value="value2"/><br/>
        asd3<input type="checkbox" id="somewhat3" name="checkboxes[key3]" value="value3"/><br/>
        <input type="submit" value="ok">
    </form>
</body>

文档

$()时()函数{

var counter = 5;
$("#addButton").click(function () {
if(counter>50){
        alert("Only 50 textboxes allow");
        return false;
}   
var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<div class="form-group" id="TextBoxesGroup" ><div class="col-md-5" id="TextBoxDiv'+ counter + '"><input type="text" name="Features['+ counter + ']" id="textbox' + counter + '" placeholder="Features type" class="form-control " value="" ></div> ' +
      '<div class="col-md-6" id="TextBoxDiv'+ counter + '"><input type="text" name="Features_v[' + counter + 
      ']" id="textbox' + counter + '" <input type="textbox" placeholder="Features description" class="form-control " value="" ></div>'+ '<div class="col-md-1" id="TextBoxDiv'+ counter + '"><i class="fa fa-trash btn" id="removeButton" ></i></div></div>');
newTextBoxDiv.appendTo("#TextBoxesGroup");

counter++;
 });

});