birthday arrays and pulldowns with error handling


/ Published in: Smarty
Save to your folder(s)

it's an excerpt of a registration form with 3 pulldowns for birthday ( day, month, year )
check for empty vars and applying CSS classes to show error on "empty" fields.
there are probably better ways, but this works for me. any suggestions welcome.


Copy this code and paste it in your HTML
  1. ############ PHP PART
  2.  
  3.  
  4. ## build up 3 arrays with D M Y values for smarty
  5.  
  6. ##days
  7.  
  8. $bday_d[0]["name"] = "..";
  9. $bday_d[0]["value"] = "";
  10.  
  11. for ($d=1;$d<=31;$d++) {
  12. $bday_d[$d]["name"] = $d;
  13. $bday_d[$d]["value"] = $d;
  14. }
  15.  
  16. ##months
  17. $bday_m[0]["name"] = ".."; $bday_m[0]["value"] = "";
  18. $bday_m[1]["name"] = "Januar"; $bday_m[1]["value"] = "01";
  19. $bday_m[2]["name"] = "Februar"; $bday_m[2]["value"] = "02";
  20. $bday_m[3]["name"] = "März"; $bday_m[3]["value"] = "03";
  21. $bday_m[4]["name"] = "April"; $bday_m[4]["value"] = "04";
  22. $bday_m[5]["name"] = "Mai"; $bday_m[5]["value"] = "05";
  23. $bday_m[6]["name"] = "Juni"; $bday_m[6]["value"] = "06";
  24. $bday_m[7]["name"] = "Juli"; $bday_m[7]["value"] = "07";
  25. $bday_m[8]["name"] = "August"; $bday_m[8]["value"] = "08";
  26. $bday_m[9]["name"] = "September"; $bday_m[9]["value"] = "09";
  27. $bday_m[10]["name"] = "Oktober"; $bday_m[10]["value"] = "10";
  28. $bday_m[11]["name"] = "November"; $bday_m[11]["value"] = "11";
  29. $bday_m[12]["name"] = "Dezember"; $bday_m[12]["value"] = "12";
  30.  
  31. ##years
  32.  
  33. $maxAge = "70";
  34. $minAge = "14";
  35. $thisYear = strftime("%Y",time()) - $minAge;
  36. $thatYear = $thisYear - $maxAge;
  37.  
  38. $bday_y[0]["name"] = "..";
  39. $bday_y[0]["value"] = "";
  40. $c=1;
  41. for ($y=$thisYear;$y>=$thatYear;$y--) {
  42. $bday_y[$c]["name"] = $y;
  43. $bday_y[$c]["value"] = $y;
  44. $c++;
  45. }
  46.  
  47. $smarty->assign("bday_d", $bday_d );
  48. $smarty->assign("bday_m", $bday_m );
  49. $smarty->assign("bday_y", $bday_y );
  50.  
  51. ############ ERROR CHECKS
  52.  
  53. $pg = $_POST["reg"];
  54. if ($pg["birthday_day"] == "") { $errorcnt ++; $smarty->assign("error_birthday_day", 1); }
  55. if ($pg["birthday_month"] == "") { $errorcnt ++; $smarty->assign("error_birthday_month", 1); }
  56. if ($pg["birthday_year"] == "") { $errorcnt ++; $smarty->assign("error_birthday_year", 1); }
  57.  
  58.  
  59. ############ SMARTY CODE
  60.  
  61.  
  62. <select name="reg[birthday_day]" {if $error_birthday_day == 1}class="fielderror"{/if}>
  63. {section name=days loop=$bday_d}
  64. <option label="{$bday_d[days].name}" value="{$bday_d[days].value}" {if $post.birthday_day == $bday_d[days].value}SELECTED{/if}>{$bday_d[days].name}</option>
  65. {/section}
  66. </select>
  67.  
  68. <select name="reg[birthday_month]" {if $error_birthday_month == 1}class="fielderror"{/if}>
  69. {section name=months loop=$bday_m}
  70. <option label="{$bday_m[months].name}" value="{$bday_m[months].value}" {if $post.birthday_month == $bday_m[months].value}SELECTED{/if}>{$bday_m[months].name}</option>
  71. {/section}
  72. </select>
  73.  
  74. <select name="reg[birthday_year]" {if $error_birthday_year == 1}class="fielderror"{/if}>
  75. {section name=years loop=$bday_y}
  76. <option label="{$bday_y[years].name}" value="{$bday_y[years].value}" {if $post.birthday_year == $bday_y[years].value}SELECTED{/if}>{$bday_y[years].name}</option>
  77. {/section}
  78. </select>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.