如何在 php 中存储添加输入的顺序


How to store the order of added inputs in php?

我有一个表格:

<form id="myForm" method="post" action="" enctype="multipart/form-data">
    <button type="button" onClick="addFile()">Add file</button>
    <button type="button" onClick="addText()">Add text</button>
    <input type="submit" value="submit">
</form>

我通过javascript向表单添加输入:

var form;
window.onload=function(){
    form=document.getElementById("myForm");
    }
function addFile(){
    var input=document.createElement("input");
    input.type="file";
    input.name="files[]";
    form.appendChild(input);
    }
function addText(){
    var input=document.createElement("input");
    input.type="text";
    input.name="texts[]";
    form.appendChild(input);
    }

我想存储添加输入的顺序。 所以在 PHP 中,我可以做这样的事情:

foreach($_POST["texts"] as $text){
    $db -> insert("table", $text, $index);
    }
foreach($_POST["file_paths"] as $path){
    $db -> insert("table", $path, $index);
    }

所以在数据库中,我可以有这样的东西:

0 "文本1">
1 "数据/上传/图像.jpg">
2 "文本2">

我尝试将所有内容添加到 $_POST 中的数组中,然后 array_search(( 以获取索引,但是当两个输入的内容相同时,它不起作用,而且看起来很草率。

那么,您将如何完成这样的任务呢?

每次向

表单添加输入时添加一个隐藏的输入,并为其提供一个变量的值,您将随着每次添加而递增:

var index = 0;
function addHidden(){
    var input=document.createElement("input");
    input.type="hidden";
    input.value=index;
    input.name="hidden" + (index++);
    form.appendChild(input);
}

PHP 端:

var $index = 0;
foreach($_POST["texts"] as $text){
    $db -> insert("table", $text, $_POST["hidden" + ($index++)]);
}
嗨,我

不确定我是否理解这个问题。 默认情况下,它们将有一个添加顺序。 在循环中获取正确的索引是没有问题的。

foreach($_POST["texts"] as $index => $text){
    $db -> insert("table", $text, $index)
}

为什么你不能简单地像这样做索引

默认情况下,您的数据将如下所示

array(
    0 => 'some_text',
    1 => 'some_text',
    2 => 'some_other_text',
    3 => 'some_text'

你的循环将生成这个

 $index = 0;
 $text = 'some_text';
 //-----------------
 $index = 1;
 $text = 'some_text';
 //-----------------
 $index = 2;
 $text = 'some_other_text';
 //-----------------
 $index = 3;
 $text = 'some_text';
 //-----------------

也许我错过了一些东西,但在 foreach 循环中使用编号索引似乎很简单。

在评论中交谈后更新,我会做这样的事情(尚未测试此代码(

Javascript:

var index = 0;
function addFile(){
    var input=document.createElement("input");
    input.type="file";
    input.name="inp_file_" + index;
    form.appendChild(input);
    ++index;
}
function addText(){
    var input=document.createElement("input");
    input.type="text";
    input.name="inp_text_" + index;
    form.appendChild(input);
    ++index;
}

在 PHP 端:

//merge $_POST and $_FILES ~ 
//I don't feel like sorting that out as it should be easy to do for you, also you may 
//be doing other things with the $_FILES array, but what you want is something like this 
// for the input array.
// one last note as I saw you said you tried merging the $_FILES into the $_POST array, it's generally a bad practice to modify the $_POST array, and is better to create a new array with the data you want.
$input = array( 
    'inp_text_0' => 'some_text',
    'inp_text_2' => 'some_other_text',
    'inp_file_1' => 'filepath.txt'
);
foreach ($input as $key => $value ){
    if( preg_match('/^inp_(?P<type>text|file)_(?P<index>[0-9]+)$/', $key, $match) ){
        $db -> insert("table", $value, $match['index'], $match['type']);    
    }
}

对于 Regx (?P<name> .. ) 是一个命名的捕获组,这不是必需的,但它有助于使示例清晰。 基本上,您正在创建一个输入列表,其名称将与此模式匹配。

  • ^ - 字符串的开头
  • inp_ - 从字面上匹配文本"inp_">
  • (?P<type>text|file) - 将捕获命名为"文本"或"文件"的类型
  • _ - 从字面上匹配文本"_"~添加只是为了清楚起见。 它看起来更好
  • (?P<index>[0-9]+) - 将捕获命名为从 0 到 9 的一个或多个数字的索引
  • $匹配字符串的结尾

正则表达式演示 https://regex101.com/r/nZ7iO5/1