Chapter 33. PHP and HTML

PHP and HTML interact a lot : PHP generate HTML, and HTML has informations that will be sent to PHP.

1. How do I create arrays in a HTML <form>?

1. How do I create arrays in a HTML <form>?

To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:

      <input name="MyArray[]">
      <input name="MyArray[]">
      <input name="MyArray[]">
      <input name="MyArray[]">
      
Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:

      <input name="MyArray[]">
      <input name="MyArray[]">
      <input name="MyOtherArray[]">
      <input name="MyOtherArray[]">
      
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script.

Note that you must not use indices with arrays in HTML! The array gets filled in the order the elements appear in the form. For functions you can use to process these arrays once you get them into your scripts, please see the Arrays section in the manual.