Hello all,
I just joined this forum. Before I ask my question let me introduce myself a little. I have been working for many years as a web designer. Although I have a familiarity with some code, I am mostly a designer. I am trying very very hard to teach myself PHP, but am having trouble because I am not use to thinking as a programmer/coder. But I am trying my best.
So recently, I am working on a project at work creating a PHP Quiz. I found a php script online that is working pretty well for me, but my issue is that I want the results of the quiz to be emailed to someone. After the quiz gets submitted, an email is successfully sent, but the email is blank without the results of the quiz sent. I want the email to include both the correct and the incorrect answers of the user. Please any help would be appreciated and remember I am a beginner. So please explain in baby-steps :o
The code is below. The only thing that I haven't done yet in the code is include the name and email fields. I will include those later when I get the email issue solved.
Also, the below code will go into a Wordpress template. While I know there are plugins for Wordpress to do what I am trying to do, I generally prefer not to use plugins and rather do it myself. Plus it's a good learning experience 🙂
Thank you very much.
<?php get_header();
/*
Template Name: PHP Quiz safety asmnt
*/
$Questions = array(
1 => array(
'Question' => 'The appropriate response during a health threatening emergency such as a fire or toxic spill is to:',
'Answers' => array(
'A' => 'Pull the fire alarm; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'B' => 'Notify lab; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'C' => 'Call EH&S'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'With proper use of wet bench and disposal procedures, the laboratory should be free of odors. In general, if you smell something, you should:',
'Answers' => array(
'A' => 'Call EH&S; Clear affected area if necessary; Notify staff and other users',
'B' => 'Do nothing; odd smells are normal when working in the cleanroom.',
'C' => 'Notify staff member; clear affected area; wait for instruction from staff who will investigate'
),
'CorrectAnswer' => 'C'
)
);
?>
<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
<h1 class="title"><?php the_title(); ?></h1>
<?php
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
echo '<br />';
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $QuestionNo. '. ' .$Value['Question'].'<blockquote>';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You entered incorrectly:<br />'.'<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
// echo '<br/>'.'The correct answer is:<br />'.'<span style="color: green;">'.$Value['CorrectAnswer'].'</span>';
} else {
echo 'You entered correctly:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
}
echo '</blockquote>';
}
} else {
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = 'email@email.com';
}
$subject = '[Safety Assessment] From '.$name;
$body .= "Name: $name \n\n";
$body .= "Email: $email \n\n";
$body .= "Answers: $Value[Answers] \n\n";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: . $email';
$headers .= "\r\n" . 'BCC: email@email.com' . "\r\n";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
?>
<form action="<?php the_permalink(); ?>" method="post" id="quiz">
<ol>
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h4><?php echo $Value['Question']; ?></h4>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><!--<?php echo $Letter; ?>)--> <?php echo $Answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
</ol>
<input id="button" type="submit" name="submit" value="Submit Quiz" />
</form>
<?php
}
?>
<!------- end body ------->
</div>
<?php get_footer(); ?>