2014-12-13 34 views
0

我有一個表格:如何在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 「text1」 中
1 「數據/上傳/ image.jpg的」
2「text2」

我嘗試將所有內容添加到$ _POST中的數組中,然後array_search()獲取索引,但當兩個輸入的內容相同時它不起作用,並且似乎馬虎。

那麼你將如何去做這樣的任務?

+0

您已檢查實際的HTTP交易一個或多個數字的指數? – 2014-12-13 03:16:44

+0

沒有。你能詳細說明一下嗎? – 2014-12-13 03:21:22

+0

'$ index'應該是什麼?你沒有定義它! – 2014-12-13 06:23:28

回答

0

添加一個隱藏的輸入每次你輸入添加到您的形式,並給它一個變量的值,你將每次加增量:

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++)]); 
} 
0

嗨,我不知道我明白這個問題。默認情況下,它們將會添加一個訂單。在循環中獲取正確的索引是沒有問題的。

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循環中使用編號索引。

UPDATE在評論說話時,我會做這樣的事情後(沒有測試此代碼)

的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
  • $比賽結束

至REGx演示https://regex101.com/r/nZ7iO5/1

+0

它可以與文本輸入一起使用,但不適用於這些文件。所有文件的路徑都被傳遞給這個腳本,它們在不同的數組中,並且它們的索引不會按照正確的順序排列。 – 2014-12-13 03:35:45

+0

你是不是按照正確的順序排列,它們將按照它們的創建順序排列。 – ArtisticPhoenix 2014-12-13 03:37:55

+0

如果我有2個數組傳遞,一個是$ texts []另一個是$ files []。 循環槽$文本[]會插入: 0 「text1」 中 1 「文本2」 等。 循環槽$文件[]會插入: 0 「文件下載1」 1 「files2」 等。 這我可以有多個內容與相同的索引 – 2014-12-13 03:45:16

相關問題