·  PHP – Web Forms – Parameters – Multivalued form components

 

- Info:

  - PHP can treat multivalue form components as an array.

  - Name of such components shoulf have sufix '[]' like:

    name="launch[]"

 

- Example:

  - Create PHP Web Application Test.php

  - http://localhost/Test.php

  - Select launch food and press Order

  - Following HTTP GET Request will be sent to server:

    http://localhost/Test.php?launch%5B%5D=soup&launch%5B%5D=chicken&launch%5B%5D=coke&submit=Order

 

    Test.php

    <!-- DISPLAY FORM ------->

    <form action="Test.php" method="get">

      Order launch: <br/>

      <input type="checkbox" name="launch[]" value="soup"    />Soup   <br/>

      <input type="checkbox" name="launch[]" value="chicken" />Chicken<br/>

      <input type="checkbox" name="launch[]" value="coke"    />Coke   <br/>

      <input type="checkbox" name="launch[]" value="banana"  />Banana <br/>

      <input type="submit"   name="submit"   value="Order"   />

    </form>

    

    <!-- DISPLAY FORM DATA -->

    <?php

      if (isset($_REQUEST['submit'])) {

        echo "<b>You have ordered:</b><br/>";

        foreach($_REQUEST['launch'] as $launch) {

          $launch = htmlentities($launch);

          echo "$launch<br/>";

        }

      }

    ?>