0

I have a button which allows an ingredient to be added to a database. When the button is clicked, the user can add the name, measurement and unit for the ingredient which is done using 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);
}

I am looking to get the unit selected for every ingredient added.

Can anyone tell me how I could do this using PHP?

I am trying the following way:

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

This does not work nor does it give an error.

Thanks.

5
  • What's the error? Commented Mar 23, 2020 at 22:27
  • It's not giving an error. It isn't adding to the array at all. Commented Mar 23, 2020 at 22:29
  • foreach ($_POST["unitList"] < where is that coming from? Commented Mar 23, 2020 at 22:33
  • Sorry, fixed it to what it is. Still no error. Commented Mar 23, 2020 at 22:38
  • 2
    foreach ($_POST["ingredient_unit[]"] < The (inside)[] brackets don't belong in there, they belong in the input(s). Commented Mar 23, 2020 at 22:45

1 Answer 1

2

Change the following code:

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

And use this instead:

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

And you should be getting a notice when you try to access $_POST["ingredient_unit[]"] since its not defined. If you are not seeing a notice, maybe the error_reporting level is too low. You could use something like the following to active all errors except the deprecated ones:

error_reporting(E_ALL &~ E_DEPRECATED);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.