我有一个按钮,可以将成分添加到数据库中。单击按钮时,用户可以使用 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);
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报
0/150
提交
取消