2012-03-27 88 views
0

後,我來到了我的想法是JavaScript的一個很好的解決有一個表格,將允許用戶添加和刪除一組字段。這裏是JavaScript函數:重新定義字段名

var fieldcount = 2; 
var count = 2; 
window.createinput = function (field1, field2, field3, area, limit) { 
    if (count > limit) return; 
    field_area = document.getElementById(area); 
    var qty = document.createTextNode("Quantity"); 
    var msr = document.createTextNode(" Measure"); 
    var ing = document.createTextNode(" Ingredient Name"); 
    var li = document.createElement("li"); 
    var input = document.createElement("input"); 
    input.id = field1 + fieldcount; 
    input.name = field1 + fieldcount; 
    input.size = "10"; 
    input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc. 
    li.appendChild(qty); 
    li.appendChild(input); 
    var input2 = document.createElement("input"); 
    input2.id = field2 + fieldcount; 
    input2.name = field2 + fieldcount; 
    input2.size = "20"; 
    input2.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc. 
    li.appendChild(msr); 
    li.appendChild(input2); 
    var input3 = document.createElement("input"); 
    input3.id = field3 + fieldcount; 
    input3.name = field3 + fieldcount; 
    input3.size = "30"; 
    input3.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc. 
    li.appendChild(ing); 
    li.appendChild(input3); 
    field_area.appendChild(li); 
    //create the removal link 
    var removalLink = document.createElement('a'); 
    removalLink.className = "remove"; 
    removalLink.onclick = function() { 
     this.parentNode.parentNode.removeChild(this.parentNode); 
     count = count - 1; 
    } 
    var removalText = document.createTextNode('Remove Field Group'); 
    removalLink.appendChild(removalText); 
    li.appendChild(removalLink); 
    fieldcount++ //loop in php to handle renaming variables for database entry based on total number of field groups 
    count++ 
} 

引用這個功能應該是這樣的HTML代碼:

<ul id="ingredients"> 
    <li>Quantity<input type="text" name="ingredient_quantity_1" id="ingredient_quantity_1" size="10" /> Measure<input type="text" name="ingredient_measure_1" id="ingredient_measure_1" size="20" /> Ingredient Name<input type="text" name="ingredient_name_1" id="ingredient_name_1" size="30" /></li> 
</ul> 
<input type="button" value="Add Ingredient" onclick="createinput('ingredient_quantity_','ingredient_measure_','ingredient_name_','ingredients', 5)" /> 

您還可以查看在http://jsfiddle.net/wtX7Y/82/

的問題是,當我想這些表單值在PHP中,字段名稱只能達到函數調用中定義的限制,但這些值可能在序列中存在空位(例如,ingredient_name_1,ingredient_name_3,ingredient_name_4),因爲它們能夠刪除表單中的字段日e removeLink.onclick函數。

我或者需要在PHP中編寫一些機制,可以在某些類型的序列中檢索/重新定義這些字段名稱值,以發送到數據庫或重新調整JavaScript函數以完全消除該需求,併爲PHP發佈正常序列處理一個簡單的迭代循環來檢索值。我在搜索的任何地方都沒有遇到這個特定的問題,但我已經看到人們建議使用數組來處理命名變量變量,並且想知道這是否是正確的方法。

與我現有的代碼 - 我一直在嘗試用PHP來解決我的問題的for循環如下,但我無法弄清楚如何有兩個分開計數的循環(一個內持續了定義新變量名,以及一個用於檢索的字段名稱值重新定義):

$field_count = 1; 
for($i = 1; $i <= 25; $i++) 
{ 
    if(isset($_REQUEST['ingredient_quantity_' . $field_count])) 
    { 
     ${'ingredient_quantity_' . $i} = $_REQUEST['ingredient_quantity_' . $field_count]; 
     ${'ingredient_measure_' . $i} = $_REQUEST['ingredient_measure_' . $field_count]; 
     ${'ingredient_name_' . $i} = $_REQUEST['ingredient_name_' . $field_count]; 
     echo $i . ". Field Count:" . $field_count . " - " . ${'ingredient_quantity_' . $i} . " " . ${'ingredient_measure_' . $i} . " " . ${'ingredient_name_' . $i} . "<br />"; 
    } 
    $field_count++; 
} 

底線是,我只是需要一種方法具有一定的字段名稱前綴來算的字段集的數量(例如,「ingredient_quantity_ ')和它們的特定號碼,以便我可以執行數據庫查詢來插入數據。我是否從字段輸入創建數組?還有別的嗎?

回答

1

您可以添加[]到輸入元素的name(未標識),其中PHP將轉換成一個整齊的陣列,無論你有多少個輸入元素都有。

但是這種方法存在問題。您需要跟蹤哪個ingredient_name對應哪個ingredient_quantity和ingredient_measure。將數組中的所有東西都傾倒,可能會導致您將一種成分的名稱與另一種成分的數量混在一起。這不好,是嗎?

所以,你最好的選擇將是這樣的:

$ingredients = array(); // Initialize array 
foreach ($_POST as $key => $value) { 
    if (!strncmp($key, 'ingredient_name_', 16)) { // If field name begins with "ingredient_name_" 
     $ingredients[] = array(
      'name' => $_POST[$key], 
      'quantity' => $_POST[('ingredient_quantity_'.substr($key, 16))], 
      'measure' => $_POST[('ingredient_measure_'.substr($key, 16))], 
     ); 
    } 
} 

總之,答案是通過循環在提交的數據,以建立自己的$ingredients陣列。任何其他固定名稱的字段都可以正常處理。如果你想知道有多少食材,你只需要做count($ingredients)。即使有數百種成分,這也是可行的,與目前的人工限制爲25的代碼不同。

此解決方案假設您每當有一個名爲ingredient_name_(number)的字段時,也會有名爲ingredient_quantity_ (相同數字)和ingredient_measure_(相同數字)存儲相應的數據。如果不是這種情況(例如,如果某些字段是可選的),只需選擇一個必填字段,在$_POST數組中查找該字段,並添加一些處理缺失字段的條件。

+0

謝謝。我會試試這個。我對創建數組並不很熟練,但認爲這可能是解決問題的最佳方式,而不是嘗試創建一些複雜的機制來查找字段名稱值,並重新分配值。你假設這三個字段('ingredient_quantity_','ingredient_measure_','ingredient_name_')都會有相應的數據,這些數據在表單可以被驗證之前將被要求。所以這是一個很好的解決方案。但是,我不確定我是否理解上述代碼中的值16。 – 2012-03-27 11:05:14

+1

前16位在那裏,因爲它告訴'strncmp'將字段名的前16個字符與「ingredient_name_」(16個字符長)進行比較。如果前16個字符匹配,該函數將返回零。查看一個字符串是否以其他字符串開頭是一個很好的方法。另外兩個16則告訴'substr'提取「ingredient_name_」後面的數字(忽略前16個字符)。切割字符串是PHP的麪包和黃油! – kijin 2012-03-27 15:46:29

+0

感謝您的澄清。 – 2012-03-27 20:32:22