Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include("MailChimp.php");
- $MailChimp = new \Drewm\MailChimp('MY-MAILCHIMP-API-KEY');
- $action = "";
- $actions = array(
- "Deep Tissue Laser Therapy",
- "Hyperbaric Chamber",
- "You may consider seeing one of our Active Release Technique Providers.",
- "Whole Body Cryotherapy",
- "GameReady Ice System",
- "Recovery Boots",
- "You should consult one of our physicians for medical clearance to use our Sports Recovery Center."
- );
- $recommendations = array(
- "1" => array(
- "0",
- "1",
- "2"
- ),
- "2" => array(
- "0",
- "3"
- ),
- "3" => array(
- "0",
- "2",
- "4"
- ),
- "4" => array(
- "0",
- "2",
- "3"
- ),
- "5" => array(
- "1",
- "3"
- ),
- "6" => array(
- "3",
- "5"
- ),
- "7" => array(
- "1",
- "4"
- ),
- "8" => array(
- "1",
- "3",
- "5"
- ),
- "9" => array(
- "1",
- "3",
- "4"
- ),
- "10" => array(
- "1",
- "3"
- ),
- "12" => array(
- "3",
- "5"
- ),
- "13" => array(
- "1",
- "3"
- )
- );
- $sumActions = array(
- "0" => 0,
- "1" => 0,
- "2" => 0,
- "3" => 0,
- "4" => 0,
- "5" => 0
- );
- if ((isset($_POST)) && (count($_POST) == 15)) {
- // Get the form fields and remove whitespace.
- $firstname = strip_tags(trim($_POST["firstname"]));
- $firstname = str_replace(array("\r","\n"),array(" "," "),$firstname);
- $emailaddress = filter_var(trim($_POST["emailaddress"]), FILTER_SANITIZE_EMAIL);
- $result = "submitted";
- if (filter_var($emailaddress, FILTER_VALIDATE_EMAIL) && ($firstname != "")) {
- $result = $MailChimp->call('lists/subscribe', array(
- 'id' => 'MAILCHIMP-DB-ID',
- 'email' => array('email'=>$emailaddress),
- 'merge_vars' => array('FNAME'=>$firstname),
- 'double_optin' => false,
- 'update_existing' => true,
- 'replace_interests' => false,
- 'send_welcome' => false,
- ));
- }
- // question 11: if yes -> "you should consult..." as only action, skip the rest of the form
- if ($_POST['11'] == "yes") {
- $action = $actions[6];
- } else { // question 11 = "no" -> do the rest of the form
- foreach ($_POST as $question => $answer) {
- if ($answer == "yes") {
- foreach ($recommendations[$question] as $recommendation) {
- $sumActions[$recommendation]++;
- }
- }
- }
- }
- }
- if ($action == "") { // q11 was "no"
- $yourRecommendedProducts = array();
- arsort($sumActions);
- // check the answers' order, but ignore "Laser" because if it's involved it's allways on top
- foreach ($sumActions as $key => $value) {
- if (($key != 0) && ($value > 0)) {
- $yourRecommendedProducts[] = $actions[$key];
- }
- }
- // "Deep Tissue Laser" is involved, put it at the top of the list
- if ($sumActions[0] > 0) {
- array_unshift($yourRecommendedProducts, $actions[0]);
- }
- foreach ($yourRecommendedProducts as $key => $product) {
- $key++;
- if ($key < 4) {
- $action .= $key . ". " . $product . "<br />\n";
- }
- }
- }
- if (!empty($action)) {
- if ($result != "submitted") {
- http_response_code(200);
- echo "Our recommendation: <br />\n" . $action;
- } else {
- http_response_code(400);
- echo "Your name, e-mail address and responses to all 13 questions are required.";
- }
- } else {
- http_response_code(400);
- echo "At least 1 answer must be yes to make a suggestion.";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement