簡體   English   中英

獲取PHP中多個選擇框的值並將它們添加到數組中

[英]Get values of multiple select boxes in PHP and add them to an array

我有一個按鈕,可以將一種成分添加到數據庫中。 單擊按鈕時,用戶可以添加使用 Javascript 完成的成分的名稱、度量和單位:

function addIngredient() {
  var area = document.getElementById("addedIngredient");
  var num = document.createElement("p");
  numText = document.createTextNode(countIngredient + ". ");
  num.appendChild(numText);
  area.appendChild(num);

  //Ingredient Name
  var ingredientNameLabel = document.createElement("p");
  ingredientNameLabelText = document.createTextNode("Name");
  ingredientNameLabel.appendChild(ingredientNameLabelText);
  area.appendChild(ingredientNameLabel);
  countIngredient++;

  var ingredientNameInput = document.createElement("INPUT");
  ingredientNameInput.setAttribute("name", "ingredient_name[]");
  ingredientNameInput.setAttribute("type", "text");
  ingredientNameInput.setAttribute("class", "form-control");
  ingredientNameInput.setAttribute("class", "ingName");
  area.appendChild(ingredientNameInput);

  //Ingredient Measure
  var ingredientMeasureLabel = document.createElement("p");
  ingredientMeasureLabelText = document.createTextNode("Measure");
  ingredientMeasureLabel.appendChild(ingredientMeasureLabelText);
  area.appendChild(ingredientMeasureLabel);

  var ingredientMeasureInput = document.createElement("INPUT");
  ingredientMeasureInput.setAttribute("name", "ingredient_measure[]");
  ingredientMeasureInput.setAttribute("type", "text");
  ingredientMeasureInput.setAttribute("class", "form-control");
  ingredientMeasureInput.setAttribute("class", "ingMeasure");
  area.appendChild(ingredientMeasureInput);

   //Ingredient Unit
   var ingredientUnitLabel = document.createElement("p");
   ingredientUnitLabelText = document.createTextNode("Unit");
   ingredientUnitLabel.appendChild(ingredientUnitLabelText);
   area.appendChild(ingredientUnitLabel);

   var select = document.createElement("SELECT");
  select.setAttribute("name", "ingredient_unit[]");
  area.appendChild(select);

  var option = document.createElement("option");
  option.setAttribute("value", "grams");
  var value = document.createTextNode("g");
  option.appendChild(value);
  select.appendChild(option);

  var option = document.createElement("option");
  option.setAttribute("value", "milimeters");
  var value = document.createTextNode("ml");
  option.appendChild(value);
  select.appendChild(option);

  var option = document.createElement("option");
  option.setAttribute("value", "kilograms");
  var value = document.createTextNode("kg");
  option.appendChild(value);
  select.appendChild(option);
  var option = document.createElement("option");
  option.setAttribute("value", "litres");
  var value = document.createTextNode("litre(s)");
  option.appendChild(value);
  select.appendChild(option);

  var option = document.createElement("option");
  option.setAttribute("value", "slice");
  var value = document.createTextNode("slice");
  option.appendChild(value);
  select.appendChild(option);

  var option = document.createElement("option");
  option.setAttribute("value", "whole");
  var value = document.createTextNode("whole");
  option.appendChild(value);
  select.appendChild(option);

  var option = document.createElement("option");
  option.setAttribute("value", "pinch");
  var value = document.createTextNode("pinch");
  option.appendChild(value);
  select.appendChild(option);
}

我希望為添加的每種成分選擇單位。

誰能告訴我如何使用 PHP 做到這一點?

我正在嘗試以下方式:

$units = [];
foreach ($_POST["ingredient_unit[]"] as $key => $unit) {
        array_push($units, $unit);
    }

這不起作用,也不會出錯。

謝謝。

更改以下代碼:

foreach ($_POST["ingredient_unit[]"] as $key => $unit) {
    array_push($units, $unit);
}

並使用它來代替:

foreach ($_POST["ingredient_unit"] as $key => $unit) {
    array_push($units, $unit);
}

當您嘗試訪問$_POST["ingredient_unit[]"]時,您應該會收到通知,因為它未定義。 如果您沒有看到通知,則可能是 error_reporting 級別太低。 您可以使用類似以下內容來激活除已棄用錯誤之外的所有錯誤:

error_reporting(E_ALL &~ E_DEPRECATED);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM