亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

四季花海 2023-10-04 14:39:09
我有一個按鈕,可以將成分添加到數據庫中。單擊按鈕時,用戶可以使用 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);}我希望為添加的每種成分選擇單位。誰能告訴我如何使用 PHP 來做到這一點?我正在嘗試以下方法:$units = [];foreach ($_POST["ingredient_unit[]"] as $key => $unit) {        array_push($units, $unit);    }這不起作用,也不會給出錯誤。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

更改以下代碼:


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);


查看完整回答
反對 回復 2023-10-04
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號