Example: How to change charset

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