0

I have a (Coffee) form that contains 60 select tags and each select tag has 16 options that contains numbers from 0 to 15. like this :

<label><select name="item[0][breakfast_blend]">
                              <option value="0">0</option>
                              <option value="1">1</option>
                              <option value="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                              <option value="6">6</option>
                              <option value="7">7</option>
                              <option value="8">8</option>
                              <option value="9">9</option>
                              <option value="10">10</option>
                              <option value="11">11</option>
                              <option value="12">12</option>
                              <option value="13">13</option>
                              <option value="14">14</option>
                              <option value="15">15</option>
</select>Breakfast Blend</label>
<label><select name="item[1][tea]">
                              <option value="0">0</option>
                              <option value="1">1</option>
                              <option value="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                              <option value="5">5</option>
                              <option value="6">6</option>
                              <option value="7">7</option>
                              <option value="8">8</option>
                              <option value="9">9</option>
                              <option value="10">10</option>
                              <option value="11">11</option>
                              <option value="12">12</option>
                              <option value="13">13</option>
                              <option value="14">14</option>
                              <option value="15">15</option>
</select>Tea</label>

There are 60 other selects like this with other labels.

now I'm trying to set a condition to check to see which items are selected by the user, then take the value of the option and the label of that select.

so i create a if condition to check the value of the selected item but i don't know how to get the label of that item (I mean i don't know how to contain it in the loop). here is my code in php part :

for ($i = 1; $i <= count($_POST['item']); $i++) {
    if (isset($_POST['item'][$i])) {
        echo $_POST['item'][$i];
    }
}

some of my friends told that Foreach function could help but still i don't know how :/

any help to contain the label in the loop ??

6
  • 2
    Why are you making them all 1 array? If you give them each a unique nameit will be much easier for you. Commented Aug 30, 2017 at 15:36
  • 1
    the label value does not get posted back to the server. Your server will need to understand based on the name attribute what the human-readable description should be. Either you have to hard-code it, or you can use some kind of lookup table. Commented Aug 30, 2017 at 15:37
  • @GrumpyCrouton then how i should use it in loop ? @_@ Commented Aug 30, 2017 at 15:40
  • 1
    foreach($_POST as $value) Commented Aug 30, 2017 at 15:40
  • @ADyson i just add a simply code but important part of the code, i had post it to the server and its working fine Commented Aug 30, 2017 at 15:42

2 Answers 2

1

You can do something like that

for ($i = 1; $i <= count($_POST['item']); $i++) {
if (isset($_POST['item'][$i])) {
    foreach($_POST['item'][$i] as $cle => $value){
     $label =  ucwords(str_replace("_"," ",$cle));
     $item = $value;
     echo  $label." ".$item;
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the breakfast blend and tea from the name if you include it you have to echo the value of item[0][breakfast_blend] now in your code it is echoing item[0] which not give you value

<select name="item[0]">
<select name="item[1]">

2 Comments

so how i can get each 'label' of the selected items ? the point is, i want to send it an email.
Within your php create array which hold the name of label index 0 is breakfast_blend and index 1is tea and so on

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.