Example: How to change charset

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms example 7
  5. 5:  *
  6. 6:  * - custom charset encoding (Forms internally works in UTF-8 encoding!)
  7. 7:  */
  8. 8:  
  9. 9:  
  10. 10: require '../../Nette/loader.php';
  11. 11:  
  12. 12:  
  13. 13:  
  14. 15:  
  15. 16:  
  16. 17: $countries array(
  17. 18:     'Select your country',
  18. 19:     'Europe' => array(
  19. 20:         'CZ' => 'Česká republika',
  20. 21:         'SK' => 'Slovakia',
  21. 22:     ),
  22. 23:     'AU' => 'Australia',
  23. 24:     'CA' => 'Canada',
  24. 25:     'EG' => 'Egypt',
  25. 26:     'JP' => 'Japan',
  26. 27:     'US' => 'United States',
  27. 28:     '?'  => 'other',
  28. 29: );
  29. 30:  
  30. 31:  
  31. 32:  
  32. 33: // Step 1: Define form with validation rules
  33. 34: $form new Form;
  34. 35: $form->encoding 'ISO-8859-1';
  35. 36:  
  36. 37: // group Personal data
  37. 38: $form->addGroup('Personal data');
  38. 39: $form->addText('name''Your name:'35);
  39. 40:  
  40. 41: $form->addMultiSelect('country''Country:')
  41. 42:     ->skipFirst()
  42. 43:     ->setItems($countriesFALSE);
  43. 44:  
  44. 45: $form->addHidden('userid');
  45. 46:  
  46. 47: $form->addTextArea('note''Comment:'305);
  47. 48:  
  48. 49:  
  49. 50: // group for buttons
  50. 51: $form->addGroup();
  51. 52:  
  52. 53: $form->addSubmit('submit1''Send');
  53. 54:  
  54. 55:  
  55. 56:  
  56. 57: // Step 2: Check if form was submitted?
  57. 58: if ($form->isSubmitted()) {
  58. 59:  
  59. 60:     // Step 2c: Check if form is valid
  60. 61:     if ($form->isValid()) {
  61. 62:         header('Content-type: text/html; charset=utf-8');
  62. 63:  
  63. 64:         echo '<h2>Form was submitted and successfully validated</h2>';
  64. 65:  
  65. 66:         $values $form->getValues();
  66. 67:         Debug::dump($values);
  67. 68:  
  68. 69:         // this is the end, my friend :-)
  69. 70:         if (empty($disableExit)) exit;
  70. 71:     }
  71. 72:  
  72. 73: else {
  73. 74:     // not submitted, define default values
  74. 75:     $defaults array(
  75. 76:         'name'    => 'Žluťoučký kůň',
  76. 77:         'userid'  => 'kůň',
  77. 78:         'note' => 'жед',
  78. 79:         'country' => 'Česká republika'// Czech Republic
  79. 80:     );
  80. 81:  
  81. 82:     $form->setDefaults($defaults);
  82. 83: }
  83. 84:  
  84. 85:  
  85. 86:  
  86. 87: // Step 3: Render form
  87. 88: ?>
  88. 89: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  89. 90: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  90. 91: <head>
  91. 92:     <meta http-equiv="content-type" content="text/html; charset=<?= $form->encoding ?>" />
  92. 93:     <meta http-equiv="content-language" content="en" />
  93. 94:  
  94. 95:     <title>Nette\Forms example 7 | Nette Framework</title>
  95. 96:  
  96. 97:     <style type="text/css">
  97. 98:     <!--
  98. 99:     .required {
  99. 100:         color: darkred
  100. 101:     }
  101. 102:  
  102. 103:     fieldset {
  103. 104:         padding: .5em;
  104. 105:         margin: .3em 0;
  105. 106:         background: #EAF3FA;
  106. 107:         border: 1px solid #b2d1eb;
  107. 108:     }
  108. 109:  
  109. 110:     input.button {
  110. 111:         font-size: 120%;
  111. 112:     }
  112. 113:  
  113. 114:     th {
  114. 115:         width: 8em;
  115. 116:         text-align: right;
  116. 117:     }
  117. 118:     -->
  118. 119:     </style>
  119. 120: </head>
  120. 121:  
  121. 122: <body>
  122. 123:     <h1>Nette\Forms example 7</h1>
  123. 124:  
  124. 125:     <?php echo $form ?>
  125. 126: </body>
  126. 127: </html>