SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using mail() function to submit form with checkboxes

    Hello.

    I have an HTML form with checkboxes:

    <input type="checkbox" name="vehicle" value="Accent" />Accent<br />
    <input type="checkbox" name="vehicle" value="Elantra" />Elantra<br />
    ...

    I want to send the information gathered from the form via the php mail() command, but have yet to figure out how to 'pull' the information from the checkboxes into a variable that I can then output in the email.

    Here's what I have right now (It doesn't work!):

    $vehicle = $_REQUEST['vehicle'];

    (this is what I do for everything other than the checkboxes...)

    and then:

    $message .= "Vehicles: $vehicle";

    What am I doing wrong? I am assuming that the values are stored as array; how can I access them?

    THANK YOU very much for your help!

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Do you want to allow multiple choices?

    If so add brackets to the name:

    Code HTML4Strict:
    <input type="checkbox" name="vehicle[]" value="Accent" />Accent<br />
    <input type="checkbox" name="vehicle[]" value="Elantra" />Elantra<br />

    And in that case:
    Code php:
    $vehicle = implode(',',$_POST['vehicle']);

    Also, depending on where in the email this data goes (body, header) and the content type, you might wanna validate it.
    Saul

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, it works great! I was so close with my other attempts, but didn't understand that I needed to use $_POST inside the implode() function. Can you tell me why POST instead of REQUEST?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $_POST has data sent to the server via HTTP POST, while $_REQUEST combines GET, POST and COOKIE data. It's a matter of good practice to take data from where you expect it to come, in this case POST.
    Saul


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
  •