Question

I'm using the following code (modified here a bit to remove secure items) to send email after my form processes and inserts data into the database. It works pretty good, but the one thing I'm really struggling with is the last for loop. On the form a person can select multiple classes that they are entering, and I need that added to the email. The rest of the $message variable is set. Works fine, I'm just not 100% how I can loop thru each $class[] and add that to the tail of the $message.

Any help would be greatly appreciated.

<?


include "Scripts/db.class.php";
include "Scripts/functions.class.php";

$db = new mysqldb();
$db->select_db();
$f = new functions();

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone = $_POST['phone'];
$emailaddress = $_POST['emailaddress'];
$racedate = $_POST['racedate'];
$number = $_POST['number'];

$sql1 = "SELECT date FROM schedule WHERE id = " . $racedate . " ";
    $result1 = $db->query($sql1);
    while ($r1 = $db->fetch_array($result1)) {
        $datemail = date_create($r1['date']);
    }


$sql ="INSERT INTO `riders` (`name`, `streetaddress`, `city`, `state`, `zipcode`, `phone`, `emailaddress`, `racedate`, `number`) VALUES ('" . $fname . " " . $lname . "', '" . $streetaddress . "', '" . $city . "', '" . $state . "', '" . $zipcode . "', '" . $phone . "', '" . $emailaddress . "', '" . $racedate . "', '" . $number . "')";
$result = $db->query($sql);
$inserted_user_id = $db->last_insert_id();

if (isset($_POST['class'])) {

    for ( $i=0;$i<count($_POST['class']);$i++) {
        $class = $_POST['class'][$i];
        $sql = "INSERT INTO rider_classes (rider, class) VALUES ('$inserted_user_id', '$class')"; //Insert into rider_classes table
        $result = $db->query($sql);
    }
}

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.server';  // Specify main and backup server
$mail->Port = "26";
$mail->From = 'email.address';
$mail->FromName = 'Mailer';
$mail->addAddress($emailaddress);  // Add a recipient

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = "Preentry for " . date_format($datemail, 'l F jS') . "";
$mail->Body    = $message;
$message =  "Name : " . $fname . " " . $lname . "\n<br />
                Race Date : " . date_format($datemail, 'l F jS') . "\n<br />
                Classes : ";
        for ( $i=0;$i<count($_POST['class']);$i++) {
        $class = $_POST['class'][$i];
        echo $f->getClassName($db, $class) . "<br />";
        }

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}

print "<meta http-equiv='refresh' content='0;URL=thanks.php?id=". $inserted_user_id ."'>";
?>
Was it helpful?

Solution

$message =  "Name : " . $fname . " " . $lname . "\n<br />
                Race Date : " . date_format($datemail, 'l F jS') . "\n<br />
                Classes : ";
        for ( $i=0;$i<count($_POST['class']);$i++) {
            $class = $_POST['class'][$i];
            $message .= ", " . $class;  <<<< adds the current $class in loop at the end of $message
            // ...
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top