多选捕获,后置方法

时间:2015-12-24 07:02:09

标签: php html

这里我试图使用post方法捕获数组值。任何人都可以帮我找到我错的地方吗?

<html>
   <body>
      <form action="test1.php" method="post">
         <ul>
            <li class="fltlft newDesgnCal" style="margin-left:0px;margin-top:0px">
               <select id="filterCal"  multiple="multiple" name="it_budget[]">
                  <option value="0-50" <?php if((isset($_POST["it_budget"][0])) && ($_POST["it_budget"][0] == '0-50')){?> selected <?php } ?>>&euro;0-&euro;50Mn</option>
                  <option value="50-100" <?php if((isset($_POST["it_budget"][0]) == '50-100' || isset($_POST["it_budget"][1]) == '50-100') ){?> selected <?php } ?>>&euro;50-&euro;100Mn</option>
                  <option value="100-1150" <?php if((isset($_POST["it_budget"][0]) == '100-1150' || isset($_POST["it_budget"][1]) == '100-1150' || isset($_POST["it_budget"][2]) == '100-1150') ){?> selected <?php } ?>>>&euro;100Mn</option>
               </select>
            </li>
         </ul>
         </td>
         <td>
            <ul>
               <li class="fltlft newDesgnCal" style="margin-left:0px;margin-top:0px">
                  <select id="filterCal"  multiple="multiple" name="Contract_Size[]">
                     <option value="0-25" <?php if(isset($_POST["Contract_Size"][0]) && ($_POST["Contract_Size"][0] == '0-25')){?> selected <?php } ?>>&euro;0-&euro;25Mn</option>
                     <option value="25-50" <?php if(isset($_POST["Contract_Size"][0]) || isset($_POST["Contract_Size"][1]) && ($_POST["Contract_Size"][0] == '25-50' || $_POST["Contract_Size"][1] == '25-50') ){?> selected <?php } ?>>&euro;25-&euro;50Mn</option>
                     <option value="50-100" <?php if(isset($_POST["Contract_Size"][0]) || isset($_POST["Contract_Size"][1]) || isset($_POST["Contract_Size"][2]) && ($_POST["Contract_Size"][0] == '50-100' || $_POST["Contract_Size"][1] == '50-100' || $_POST["Contract_Size"][2] == '50-100')  ){?> selected <?php } ?>>&euro;50-&euro;100Mn</option>
                     <option value="100-300" <?php if(isset($_POST["Contract_Size"][0]) || isset($_POST["Contract_Size"][1]) || isset($_POST["Contract_Size"][2]) || isset($_POST["Contract_Size"][3]) && ($_POST["Contract_Size"][0] == '100-300' || $_POST["Contract_Size"][1] == '100-300' || $_POST["Contract_Size"][2] == '100-300' || $_POST["Contract_Size"][3] == '100-300')  ){?> selected <?php } ?>>&euro;100-&euro;300Mn</option>
                     <option value="300-10000" <?php if(isset($_POST["Contract_Size"][0]) || isset($_POST["Contract_Size"][1]) || isset($_POST["Contract_Size"][2]) || isset($_POST["Contract_Size"][3]) || isset($_POST["Contract_Size"][4]) && ($_POST["Contract_Size"][0] == '300-10000' || $_POST["Contract_Size"][1] == '300-10000' || $_POST["Contract_Size"][2] == '300-10000' || $_POST["Contract_Size"][3] == '300-10000' || $_POST["Contract_Size"][3] == '300-10000')  ){?> selected <?php } ?>>>&euro;300Mn</option>
                  </select>
               </li>
            </ul>
            <input type="submit"/>
      </form>
   </body>
</html>
</ul>

1 个答案:

答案 0 :(得分:1)

试试这个:

使用foreach循环itrate所有具有选项值的数组元素并检查它。如果它匹配则break循环。

<select id="filterCal"  multiple="multiple" name="it_budget[]">
  <option value="0-50" <?php foreach($_POST['it_budget'] as $b) { if($b == "0-50") { echo "selected='selected'"; break; } } ?> >&euro;0-&euro;50Mn</option>
  <option value="50-100" <?php foreach($_POST['it_budget'] as $b) { if($b == "50-100") { echo "selected='selected'"; break; } } ?> >&euro;50-&euro;100Mn</option>
  <option value="100-1150" <?php foreach($_POST['it_budget'] as $b) { if($b == "100-1150") { echo "selected='selected'"; break; } } ?> >&euro;100Mn</option>
</select>