Contact Jan Thomas Consulting

January 12, 2010

Use Cforms in WordPress to create a short quiz or questionnaire

Filed under: HomePage, WordPress Techniques — AdminJan @ 8:22 pm

Do you want to vary the CFormsII success message? Maybe you want to create a short quiz that can be scored and reported upon completion. CFormsII provides the ability to alter the success message before the form is processed. For something simpler, create a questionnaire with no scoring involved. Learn how to set these up.

[The quiz and survey solutions requires knowledge of php programming for scoring. After being submitted, the form is scored in a php function.]

View the Questionnaire Demo

View the Quiz Demo (Scoring: one correct answer)

View the Health Survey Demo (Scoring: answers weighted from 0 to 3)

Questionnaire Instructions

  1. Create a Form in CformsII. Add the form to a WordPress page or post. Click for more information on creating a CFormsII form.

    Here’s what the demo Questionnaire looks like in the CForms Designer: WebsiteSurvey
  2. The survey results will be sent to the admin’s email account and the user’s email with the CC option.

Quiz Instructions

  1. Create a Form in CformsII. Add the form to a WordPress page or post. Click for more information.

    Remember the form number and field numbers.

    This form below is form #8 and the quiz includes fields 4 through 8.

    Each of the radio button answers are set up with a value = 1 for the correct answer and a value = 0 for the incorrect answer.

    tvquestionnaire

  2. Edit my-functions.php .
    • Use the editor of your choice to edit wp-content/plugins/cforms/my-functions.php.
      OR
      Go into Plugins->Editor. Select plugin to edit: cforms. Click the cforms/my-functions.php file near the bottom of the long list of files.
    • The my-functions.php file has sample code for numerous actions. For example setting the redirection link, or changing an email address. By default, these functions are commented out.
    • Define my_cforms_logic to change the success message. In this example, the values for fields 4, 5, 6, 7, 8 are totaled. Correct answers have a value of 1 and incorrect answers have a value of 0 so the total represents the score.
    • Within the my_cforms_logic function, there are several ways to access the field data. In this example, the field value is referenced using it’s field number when combined with $$$.
    • To see the variables that hold field data use the @email function. The results will be sent to email.
  3. Save Changes to the server
  4. Run the form.

Here’s the code:

 
function my_cforms_logic($cformsdata,$oldvalue,$setting) {
    ### it's only changed though for form #8
    ### example: changes redirection address based on user input field
    if (( $cformsdata['id']==8 ) && ( $setting == "successMessage" )) {
	### note: '$$$mypick' references the ID of the HTML element and has been assigned
             ### to the drop down field in the form configuration, with [id:mypick] !
	$form   = $cformsdata['data'];	
	$totalCorrect = 0;
	$total = 0;
 	for ($i = 4; $i <= 8; $i++) {
                   $curField = '$$$' . $i;
                   $totalCorrect += $form[$form[$curField]];
                   $total++;
 	}
 
            $percent = ($totalCorrect / $total) * 100;
            $yourScore = "<p>You scored " . $totalCorrect . " out of " . $total . ".  That's " . $percent . "%!</p>";
	if ( $total >= 2 )
	       return $yourScore . ' <p>Good Work!</p> ' . $oldvalue ;
	else
                    return $yourScore . ' <p>Better Luck Next Time!</p>';
 
	//@mail('thomasja1@earthlink.net', 'cforms my_filter_nonAjax test', 'Form data array (nonAjax):'.print_r($cformsdata,1), 'From: jan@janbthomas.com');
	}
	return $oldvalue;
}

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress