Results 1 to 2 of 2

Thread: Contact Form W Required Fields

  1. #1
    Join Date
    Apr 2005
    Posts
    54
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Contact Form W Required Fields

    I Like To Add To My Contact Form

    A Menu Tree Of Checkboxes

    Option A
    1
    2
    3
    Option B
    1
    2

    And Have Checkboxes To Be Required Fields

    Please Help Me Improve My Contact Form. Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by miradoro
    [...] Checkboxes

    Option A
       1
       2
       3
    I'll assume you created your checkboxes along the lines of:

    HTML Code:
    <input name="option-a[]" type="checkbox" value="1">
    <input name="option-a[]" type="checkbox" value="2">
    <input name="option-a[]" type="checkbox" value="3">
    The specifics aren't that important, however each group should share the same name, and that name should be followed a pair of square brackets.

    When one or more of the checkboxes above are selected, an array will be created in the $_GET or $_POST superglobals (I should think you'll be using the POST transfer method) with the name of that checkbox group. Using the example above, the name would be 'option-a'.

    If, for example, the second and third checkbox above was selected, the first element of the array (index 0) will have the value '2', and the second element (index 1) will have the value '3'. If the first and third checkboxes were selected, the first element will have the value '1', and the second element will have the value '3'.

    If no checkboxes are selected, a browser won't send any form for them, so the array within $_POST won't exist.

    So, to check if any data was submitted, we can use the isset function to test whether the array exists:

    PHP Code:
    if(isset($_POST['option-a'])) {
      
    /* Element exists
       *
       * We can optionally check that the element is
       * actually an array using the is_array function.
       */

    You can now loop through the array with foreach, for instance, to obtain the values it contains:

    PHP Code:
    foreach($_POST['option-a'] as $value) {
      
    /* ... */

    Hope that helps,
    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •