Checkbox array passes to script fine but doesn't mail right

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JackM

    Checkbox array passes to script fine but doesn't mail right

    Okay, I'm starting to get a little ticked off because I've worked for
    hours on this and I can't seem to find the cause. I'm using PHP 5.1.6.
    I'm trying to get the values of some form checkboxes along with another
    fixed variable and pass them along in an email. For demonstration
    purposes, I have checked the Wed and Fri checkboxes and set the time for
    9:00 am.

    <FORM ACTION="test.ph p" METHOD=POST>
    <TABLE border="1" width="70%" >
    <TR>
    <td><input type="checkbox" name="NewClass[]" value="Mon" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Tue" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Wed" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Thu" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Fri" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Sat" size="5"
    maxlength="2"</td>
    <td><input type="checkbox" name="NewClass[]" value="Sun" size="5"
    maxlength="2"</td>
    <TD><input type="text" name="NewClass-Time" size="5" maxlength="10"> </TD>
    </TR>
    </TABLE>
    <input type="submit" name="ClassList " value="Submit Survey Info"
    class="button">
    </form>

    When I print out print_r($_POST) ;, I get everything passed correctly as:

    Array
    (
    [NewClass] =Array
    (
    [0] =Wed
    [1] =Fri
    )

    [NewClass-Time] =9:00 am
    )

    But when it gets emailed, the results listed on the email I get are as
    follows:

    NewClass: Array

    NewClass-Time: 9:00 am

    I stripped the processing script down to the barest of essentials. All
    that's on it right now is:

    foreach($_POST as $key =>
    $value){if(!(!i sset($value))){ $set=1;}$messag e = $message . "$key:
    $value\n\n";} if($set!==1){he ader("location: $_SERVER[HTTP_REFERER]");exit;}

    $message = stripslashes($m essage);

    $headers = "From: " . $_POST['name'] . " <" .$_POST['email'] . ">\n" .
    "Return-Path: " . $_POST['email'] . "\n" . "Reply-To: " .
    $_POST['email'] . "\n";

    mail($my_email, $subject,$messa ge,$headers);

    Does anyone see what I'm obviously missing? How can I get the NewClass
    array to pass each chosen value on in the email instead of printing out
    "array"?
  • Rik

    #2
    Re: Checkbox array passes to script fine but doesn't mail right

    JackM <notme@earthlin k.netwrote:
    Okay, I'm starting to get a little ticked off because I've worked for
    hours on this and I can't seem to find the cause. I'm using PHP 5.1.6.
    I'm trying to get the values of some form checkboxes along with another
    fixed variable and pass them along in an email. For demonstration
    purposes, I have checked the Wed and Fri checkboxes and set the time for
    9:00 am.
    When I print out print_r($_POST) ;, I get everything passed correctly as:
    >
    Array
    (
    [NewClass] =Array
    (
    [0] =Wed
    [1] =Fri
    )
    >
    [NewClass-Time] =9:00 am
    )
    >
    But when it gets emailed, the results listed on the email I get are as
    follows:
    >
    NewClass: Array
    >
    NewClass-Time: 9:00 am
    >
    I stripped the processing script down to the barest of essentials. All
    that's on it right now is:
    >
    foreach($_POST as $key =
    $value){if(!(!i sset($value))){ $set=1;}$messag e = $message . "$key:
    $value\n\n";}
    $value will always be set, even if it's an empty string (a lot of browsers
    default to on though). So what the "if(!isset($val ue))" does?

    foreach($_POST as $key =value){
    $message = $key . ': '.((is_array($v alue)? implode(',',$va lue): $value);

    }

    --
    Rik Wasmus

    Comment

    • JackM

      #3
      Re: Checkbox array passes to script fine but doesn't mail right

      Rik wrote:
      JackM <notme@earthlin k.netwrote:
      >
      >
      >
      >When I print out print_r($_POST) ;, I get everything passed correctly as:
      >>
      >Array
      >(
      > [NewClass] =Array
      > (
      > [0] =Wed
      > [1] =Fri
      > )
      >>
      > [NewClass-Time] =9:00 am
      >)
      >>
      >But when it gets emailed, the results listed on the email I get are
      >as follows:
      >>
      > NewClass: Array
      >>
      > NewClass-Time: 9:00 am
      >>
      >I stripped the processing script down to the barest of essentials.
      >All that's on it right now is:
      >>
      > foreach($_POST as $key =>
      >$value){if(!(! isset($value))) {$set=1;}$messa ge = $message . "$key:
      >$value\n\n"; }
      >
      >
      $value will always be set, even if it's an empty string (a lot of
      browsers default to on though). So what the "if(!isset($val ue))" does?
      >
      foreach($_POST as $key =value){
      $message = $key . ': '.((is_array($v alue)? implode(',',$va lue):
      $value);
      >
      }
      So if I'm reading that correctly, that's another way of saying:

      foreach($_POST as $key =$value){
      if(is_array($va lue)){
      $message = $message . "$key: ".implode( ', ', $value)."\n\n";
      } else {
      $message = $message . "$key: $value\n\n";
      }
      }

      Close interpretation, maybe? At least the code works. ;-)

      Comment

      • Rik

        #4
        Re: Checkbox array passes to script fine but doesn't mail right

        JackM <notme@earthlin k.netwrote:
        Rik wrote:
        > foreach($_POST as $key =value){
        > $message = $key . ': '.((is_array($v alue)? implode(',',$va lue):
        >$value);
        > }
        >
        So if I'm reading that correctly, that's another way of saying:
        >
        foreach($_POST as $key =$value){
        if(is_array($va lue)){
        $message = $message . "$key: ".implode( ', ', $value)."\n\n";
        } else {
        $message = $message . "$key: $value\n\n";
        }
        }
        >
        Close interpretation, maybe? At least the code works. ;-)
        Yup, except my code obviously didn't have the "\n\n" :-)
        It's called the ternary operator, and saves a lot of lines/if-else blocks
        for simple conditionals.

        Also, '.=', will save some typing here:
        $message .= "$key: ".implode( ', ', $value)."\n\n";
        --
        Rik Wasmus

        Comment

        Working...