Advertisement
Guest User

srcform.php

a guest
Apr 19th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. include("MailChimp.php");
  5. $MailChimp = new \Drewm\MailChimp('MY-MAILCHIMP-API-KEY');
  6.  
  7. $action          = "";
  8. $actions         = array(
  9.     "Deep Tissue Laser Therapy",
  10.     "Hyperbaric Chamber",
  11.     "You may consider seeing one of our Active Release Technique Providers.",
  12.     "Whole Body Cryotherapy",
  13.     "GameReady Ice System",
  14.     "Recovery Boots",
  15.     "You should consult one of our physicians for medical clearance to use our Sports Recovery Center."
  16. );
  17. $recommendations = array(
  18.     "1" => array(
  19.         "0",
  20.         "1",
  21.         "2"
  22.     ),
  23.     "2" => array(
  24.         "0",
  25.         "3"
  26.     ),
  27.     "3" => array(
  28.         "0",
  29.         "2",
  30.         "4"
  31.     ),
  32.     "4" => array(
  33.         "0",
  34.         "2",
  35.         "3"
  36.     ),
  37.     "5" => array(
  38.         "1",
  39.         "3"
  40.     ),
  41.     "6" => array(
  42.         "3",
  43.         "5"
  44.     ),
  45.     "7" => array(
  46.         "1",
  47.         "4"
  48.     ),
  49.     "8" => array(
  50.         "1",
  51.         "3",
  52.         "5"
  53.     ),
  54.     "9" => array(
  55.         "1",
  56.         "3",
  57.         "4"
  58.     ),
  59.     "10" => array(
  60.         "1",
  61.         "3"
  62.     ),
  63.     "12" => array(
  64.         "3",
  65.         "5"
  66.     ),
  67.     "13" => array(
  68.         "1",
  69.         "3"
  70.     )
  71. );
  72.  
  73. $sumActions = array(
  74.     "0" => 0,
  75.     "1" => 0,
  76.     "2" => 0,
  77.     "3" => 0,
  78.     "4" => 0,
  79.     "5" => 0
  80. );
  81.  
  82. if ((isset($_POST)) && (count($_POST) == 15)) {
  83.  
  84.     // Get the form fields and remove whitespace.
  85.     $firstname = strip_tags(trim($_POST["firstname"]));
  86.         $firstname = str_replace(array("\r","\n"),array(" "," "),$firstname);
  87.     $emailaddress = filter_var(trim($_POST["emailaddress"]), FILTER_SANITIZE_EMAIL);
  88.     $result = "submitted";
  89.    
  90.     if (filter_var($emailaddress, FILTER_VALIDATE_EMAIL) && ($firstname != "")) {
  91.         $result = $MailChimp->call('lists/subscribe', array(
  92.                         'id'                => 'MAILCHIMP-DB-ID',
  93.                         'email'             => array('email'=>$emailaddress),
  94.                         'merge_vars'        => array('FNAME'=>$firstname),
  95.                         'double_optin'      => false,
  96.                         'update_existing'   => true,
  97.                         'replace_interests' => false,
  98.                         'send_welcome'      => false,
  99.                     ));
  100.     }
  101.    
  102.     // question 11: if yes -> "you should consult..." as only action, skip the rest of the form
  103.     if ($_POST['11'] == "yes") {
  104.         $action = $actions[6];
  105.     } else { // question 11 = "no" -> do the rest of the form
  106.         foreach ($_POST as $question => $answer) {
  107.             if ($answer == "yes") {
  108.                 foreach ($recommendations[$question] as $recommendation) {
  109.                     $sumActions[$recommendation]++;
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. if ($action == "") { // q11 was "no"
  117.     $yourRecommendedProducts = array();
  118.     arsort($sumActions);
  119.    
  120.     // check the answers' order, but ignore "Laser" because if it's involved it's allways on top
  121.     foreach ($sumActions as $key => $value) {
  122.         if (($key != 0) && ($value > 0)) {
  123.             $yourRecommendedProducts[] = $actions[$key];
  124.         }
  125.     }
  126.    
  127.     // "Deep Tissue Laser" is involved, put it at the top of the list
  128.     if ($sumActions[0] > 0) {
  129.         array_unshift($yourRecommendedProducts, $actions[0]);
  130.     }
  131.    
  132.     foreach ($yourRecommendedProducts as $key => $product) {
  133.         $key++;
  134.         if ($key < 4) {
  135.             $action .= $key . ". " . $product . "<br />\n";
  136.         }
  137.     }
  138. }
  139.  
  140. if (!empty($action)) {
  141.    
  142.      if ($result != "submitted") {
  143.    
  144.          http_response_code(200);
  145.          echo "Our recommendation: <br />\n" . $action;
  146.      } else {
  147.         http_response_code(400);
  148.         echo "Your name, e-mail address and responses to all 13 questions are required.";
  149.      }
  150. } else {
  151.     http_response_code(400);
  152.     echo "At least 1 answer must be yes to make a suggestion.";
  153. }
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement