Collecting the Data
So now with our form in place, we need a way to grab that information and return it in one of two ways. Those two ways are:
* Database with Real-Time results
* Email Results
We are first going to email the results to an email address. I’ll go through the code, and then give an overview. First we will set up our arrays so we don’t email useless numbers, but rather the textual rates that the user gave:
<?php
$rate = Array(0=>’Very Dissatisfied’,
1=>’Dissatsifed’,
2=>’Satisfied’,
3=>’Very Satisfied’,
4=>’Neutral’);
$prate = Array(0=>’Not Very Personable’,
1=>’Somewhat Personable’,
2=>’Personable’,
3=>’Very Personable’,
4=>’Extremely Personable’);
We will use those arrays later in the email portion. Next, we need to move all the $_POST variables into our own variables. Rather than use the standard $var = $_POST[‘var’] syntax, we will write a short and sweet foreach loop and create the variables dynamically. There are security issues involved with this in other applications, but since we are not asking for the user to input any text, we can get away with it in this example.
// Get all $_POST variables foreach($_POST as $var => $val) { ${$var} = $val; }
Now that we’ve got our posted variable values in local variables, we can create our email. I used a basic template and separated the responses into demographics and results. This will help with classification and data collection purposes, but you can format the email however you please.
// Set up the email body
$body_body .= “Respondent Demographics:n”;
$body_body .= ” Gender————-| “.$gender.”n”;
$body_body .= ” Age Group———-| “.$age.”n”;
$body_body .= ” Support Type——-| “.$suptype.”n”;
$body_body .= ” Computer Knowledge-| “.$knowledge.”n”;
$body_body .= “—————————————————-n”;
$body_body .= “Survey Results:n”;
$body_body .= ” Overall Satisfaction-| “.$rate[$overall].”n”;
$body_body .= ” Speed of Support—–| “.$rate[$speed].”n”;
$body_body .= ” Rep. Knowledge——-| “.$rate[$repknowledge].”n”;
$body_body .= ” Result Satisfaction–| “.$rate[$result].”n”;
$body_body .= ” Recommend Us?——–| “.$recommend.”n”;
$body_body .= ” Personability of Rep?| “.$prate[$personability].”n”;
$body_body .= “—————————————————-n”;
$body_footer = “Survey Results provided as is from www.mywebpage.com”;
Okay, so as you can see that our arrays come into play in the “Survey Results” section in that we use them to turn 0 – 4 into actual textual ratings. The reason we don’t need to use an array for the “Recommend Us?” answer is because it is sent as a “Yes” or “No” reply.
Now that we’ve got our data and email set up, let’s define who to send it to, what the body will look like, and the subject of the email.
$to = ‘Brett Patterson <[email protected]>’;
$subject = ‘Website Survey Results’;
$body = $body_body.$body_footer;
The next section will either send the email, or display it on the screen–you can choose what to do with it depending on your specific needs. Whichever you choose, just uncomment the line or block of code and you’re good to go.
//mail($to, $subject, $body);
/*
echo ‘<div style=”white-space: pre”>’;
print_r(str_replace(“n”, “<br />”,$body));
echo ‘</div>’;
*/
?>
Here is the full code as it should look:
<?php
$rate = Array(0=>’Very Dissatisfied’,
1=>’Dissatsifed’,
2=>’Satisfied’,
3=>’Very Satisfied’,
4=>’Neutral’);
$prate = Array(0=>’Not Very Personable’,
1=>’Somewhat Personable’,
2=>’Personable’,
3=>’Very Personable’,
4=>’Extremely Personable’);
// Get all $_POST variables
foreach($_POST as $var => $val)
{
${$var} = $val;
}
// Set up the email body
$body_body .= “Respondent Demographics:n”;
$body_body .= ” Gender————-| “.$gender.”n”;
$body_body .= ” Age Group———-| “.$age.”n”;
$body_body .= ” Support Type——-| “.$suptype.”n”;
$body_body .= ” Computer Knowledge-| “.$knowledge.”n”;
$body_body .= “—————————————————-n”;
$body_body .= “Survey Results:n”;
$body_body .= ” Overall Satisfaction-| “.$rate[$overall].”n”;
$body_body .= ” Speed of Support—–| “.$rate[$speed].”n”;
$body_body .= ” Rep. Knowledge——-| “.$rate[$repknowledge].”n”;
$body_body .= ” Result Satisfaction–| “.$rate[$result].”n”;
$body_body .= ” Recommend Us?——–| “.$recommend.”n”;
$body_body .= ” Personability of Rep?| “.$prate[$personability].”n”;
$body_body .= “—————————————————-n”;
$body_footer = “Survey Results provided as is from www.mywebpage.com”;
$to = ‘Brett Patterson <[email protected]>’;
$subject = ‘Website Survey Results’;
$body = $body_body.$body_footer;
//mail($to, $subject, $body);
/*
echo ‘<div style=”white-space: pre”>’;
print_r(str_replace(“n”, “<br />”,$body));
echo ‘</div>’;
*/
?>
We’ve sent the email with our survey results, and in the next part of this article, we’ll see how we can adapt this to use a database and give us compiled results from all the visitors who have responded. Come back next week for the rest of this tutorial!!