後,我來到了我的想法是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_ ')和它們的特定號碼,以便我可以執行數據庫查詢來插入數據。我是否從字段輸入創建數組?還有別的嗎?
謝謝。我會試試這個。我對創建數組並不很熟練,但認爲這可能是解決問題的最佳方式,而不是嘗試創建一些複雜的機制來查找字段名稱值,並重新分配值。你假設這三個字段('ingredient_quantity_','ingredient_measure_','ingredient_name_')都會有相應的數據,這些數據在表單可以被驗證之前將被要求。所以這是一個很好的解決方案。但是,我不確定我是否理解上述代碼中的值16。 – 2012-03-27 11:05:14
前16位在那裏,因爲它告訴'strncmp'將字段名的前16個字符與「ingredient_name_」(16個字符長)進行比較。如果前16個字符匹配,該函數將返回零。查看一個字符串是否以其他字符串開頭是一個很好的方法。另外兩個16則告訴'substr'提取「ingredient_name_」後面的數字(忽略前16個字符)。切割字符串是PHP的麪包和黃油! – kijin 2012-03-27 15:46:29
感謝您的澄清。 – 2012-03-27 20:32:22