I have a form that users fill out to send denial letters to customers. I have been struggling to figure out how to pass CHECKED checkboxes from a form to be passed to the $mail_body of the email.

Here is what i'm working with:

<?php
  require_once "formvalidator.php";
  $error_hash = 'no';
  $show_form = true;
  if (isset($_REQUEST['Submit'])) {
      $validator = new FormValidator();
      $validator->addValidation("Email", "req", "<B>Please provide an Email Address.</B>");
      $validator->addValidation("Email", "email", "<B>Email address is invalid.</B>");
      $validator->addValidation("Name", "req", "<B>Please provide a Recipient.</B>");
      if ($validator->ValidateForm()) {
          $show_form = false;
      } else {
          echo "<center><font color='#CC0000'><B>Validation Errors:</B></font></center>";
          $error_hash = $validator->GetErrors();
          foreach ($error_hash as $inpname => $inp_err) {
              echo "<center><p>$inpname : $inp_err</p></center>\n";
              $show_form = true;
          }
      }
          if ($show_form === false){
          // Grab the form vars
          $From = "companyemail@email.com";
          $Email = $_REQUEST['Email'];
          $address ="Address of company";
          $Date = date("Y/m/d");
          $Name = $_REQUEST['Name'];
          $Addy = $_REQUEST['Addy'];
          $City = $_REQUEST['City'];
          $State = $_REQUEST['State'];
          $Zip = $_REQUEST['Zip'];
          $num = $_REQUEST['Num'];
          $message = "You have been denied for the following reasons:";
          $reason1 = $_REQUEST['ch1'];
          $reason2 = $_REQUEST['ch2'];
          $reason3 = $_REQUEST['ch3'];
          $reason4 = $_REQUEST['ch4'];
          $reason5 = $_REQUEST['ch5'];

        //message body
        $mail_body .= "$address"."\r\n";

        $mail_body .= "$Date"."\r\n";
        $mail_body .= "$message"."\r\n";
        $mail_body .= "$reason1"."\r\n";
        $mail_body .= "$reason2"."\r\n";
        $mail_body .= "$reason3"."\r\n";
        $mail_body .= "$reason4"."\r\n";
        $mail_body .= "$reason5"."\r\n";
        $mail_body .= "$Name"."\r\n";
        $mail_body .= "$Addy"."\r\n";
        $mail_body .= "$City,". " $State". " $Zip"."\r\n";
        $mail_body .= "RE: ".$num."\r\n";
        //sending to
        $recipient = "$Email";
        $subject = "Credit Information";
        //optional headerfields
        $header = "From: " . $From . " <" . $From . ">\r\n";
        //mail command
        mail($recipient, $subject, $mail_body, $header);
                                   }
          echo "<div style='width:400px; margin:0 auto; border:1px solid #1e1e1e'>Your message has been sent successfully.<br>You will be redirected to the home page in a few moments...</div>";
                        //page redirect to home
                                         echo "<meta http-equiv=\"refresh\" content=\"4;url=";
                          echo "http://lmtl-linux/credit\" />";
}
  if (true == $show_form) {
?>
<div style="width:600px; margin:0 auto; border:1px solid black; background-color:#333333">
<p style="color:#FAFAFA; font-size:22px;">Customer Denial Emailer</p>
<form action="" method="GET" id="form">
        <table>
                 <tr><td valign="top" class="title">Email:</td><td><input type="text" id="Email" name="Email" class="required email" value="<?php echo $_REQUEST['Email'];?>"></td></tr>
                 <tr><td valign="top" class="title">Name:</td><td><input type="text" id="Name" name="Name" class="required" value="<?php echo $_REQUEST['Name'];?>"></td></tr>
                 <tr><td valign="top" class="title">Address:</td><td><input type="text" id="Addy" name="Addy" class="required" value="<?php echo $_REQUEST['Addy'];?>"></td></tr>
                 <tr><td valign="top" class="title">City:</td><td><input type="text" id="City" name="City" class="required" value="<?php echo $_REQUEST['City'];?>"></td></tr>
                 <tr><td valign="top" class="title">State:</td><td><input type="text" id="State" name="State" class="required" value="<?php echo $_REQUEST['State'];?>"></td></tr>
                 <tr><td valign="top" class="title">Zip:</td><td><input type="text" id="Zip" name="Zip" class="required" value="<?php echo $_REQUEST['Zip'];?>"></td></tr>
                 <tr><td valign="top" class="title">App #:</td><td><input type="text" id="Num" name="Num" class="required" value="<?php echo $_REQUEST['Num'];?>"></td></tr>
                 <tr><td valign="top" class="title">Reason:</td></tr>
                 <tr><td colspan="2" class="reason"><input type="checkbox" id="ch1" name="ch1" value="Income insufficient to sustain payments" <?PHP if(isset($_REQUEST['ch1'])){print "checked";}?>>Income insufficent</td></tr>
                 <tr><td colspan="2" class="reason"><input type="checkbox" id="ch2" name="ch2" value="Employment is not of sufficient length to qualify" <?PHP if(isset($_REQUEST['ch2'])){print "checked";}?>>Employment lenght insufficent</td></tr>
                 <tr><td colspan="2" class="reason"><input type="checkbox" id="ch3" name="ch3" value="Limited or no credit history" <?PHP if(isset($_REQUEST['ch3'])){print "checked";}?>>Limited or no credit history</td></tr>
                 <tr><td colspan="2" class="reason"><input type="checkbox" id="ch4" name="ch4" value="Derogatory credit reporting" <?PHP if(isset($_REQUEST['ch4'])){print "checked";}?>>Derogatory credit reporting</td></tr>
                 <tr><td colspan="2" class="reason"><input type="checkbox" id="ch5" name="ch4" value="Bankruptcy, judgments, repossession, liens" <?PHP if(isset($_REQUEST['ch5'])){print "checked";}?>>Bankruptcy, reposession, etc.</td></tr>
                </table>
</div>
<div style="width:650px; margin:0 auto;">
<input type="submit" name="Submit" id="Submit" value="Send Email" style="margin-left:22px; width:100px;">
 </form>
</div>

<?php
      }//true == $show_form
?>

</body>
</html>

if i check a couple "reasons" none of those reasons get passed to the email. Any ideas?

The checkbox is passed by name as a type of bool. If you checked "ch4", then $_REQUEST is going to be "yes". If it is unchecked, ch4 will not be set at all.

Secondly, your checkboxes should be grouped with the same name, unless you just want to do it this way. They should all be name="reason" or something, with their individual value="here is the reason". But in order to handle with php, the name has to use a bracket, so it would be name="reason[]". This creates a PHP array that contains the values themselves, rather than a yes/no toggle. In other words:

name="reason[]" value="1"
name="reason[]" value="2"
name="reason[]" value="3"

Then php gets an array:

$_REQUEST[reason]

which contains the checked values, so perhaps it equals ["1", "3"]

To put it all together. Give your checkboxes the same name with the bracket.

In PHP you can cycle through the array like so:

<?php
  $reason = $_REQUEST['reason'];
  if(empty($reason)) 
  {
    'do something if none checked
  } 
  else 
  {
    $r = count($reason); ' how many were checked?
    for($i=0; $i < $r; $i++)
    {
      echo "Reason $i was: " . $reason[$i] . "\r\n";
    }
  }
?>

Lastly if you use the array then you'll have to change "<?PHP if(isset($_REQUEST)){print "checked";}?>" because it won't work that way any more. Also you should echo the full property "checked='yes'" rather than just "checked", it's better ;)

If you don't want to use this whole array thing at all, then just remember that ch1,2,3,4 will either be "yes" or not assigned. So you can't say "$mail_body .= "$reason3"."\r\n";" You'd have to check if variable == "yes" than echo "you suck" or what have you.


I can help further if you need to get more specific.

Good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.