Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Polskie znaki w formularzu kontaktowym
Forum PHP.pl > Forum > Przedszkole
platyna
Hej,
mam problem z polskimi znakami w formularzu kontaktowym.
Strona ma znacznik meta:
  1. <meta http-equiv="content-type" content="text/html; charset=utf-8">


a w skrypcie kombinuję z headerem na UTF-8, ale coś robię źle.
Będę bardzo wdzięczny za pomoc w tym pewnie głupim i banalnym problemie, z którym niestety sam nie mogę sobie poradzić.

  1. <?php
  2. if (isset($_POST["submit"])) {
  3.  
  4. $name = $_POST['name'];
  5. $email = $_POST['email'];
  6. $message = $_POST['message'];
  7. $header = "Content-Type: text/html; charset=utf-8";
  8. $human = intval($_POST['human']);
  9. $from = '';
  10. $to = 'poprawny@mail.com';
  11. $subject = '';
  12.  
  13. $body ="$header\n Od: $name\n E-mail: $email\n Wiadomość:\n $message";
  14.  
  15. if (!$_POST['name']) {
  16. $errName = '';
  17. }
  18.  
  19.  
  20. if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  21. $errEmail = '';
  22. }
  23.  
  24.  
  25. if (!$_POST['message']) {
  26. $errMessage = '';
  27. }
  28.  
  29. if ($human !== 4) {
  30. $errHuman = '';
  31. }
  32.  
  33. if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
  34. if (mail ($to, $subject, $body, $from)) {
  35. $result='<div class="alert alert-success">';
  36. } else {
  37. $result='<div class="alert alert-danger">';
  38. }
  39. }
  40. }
  41. ?>
patwoj98
Pierwsze.
Przed znacznikiem html wstaw: <!DOCTYPE html>
Potem zamień swoje kododawnie na <meta charset="utf-8" />
Błędne kodowanie masz pewnie w mailu musisz wstawić header:
Przykład #4
platyna
Doctype wygląda następująco:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


Jeżeli do kodu dodaję:
  1. header('Content-Type: text/html; charset=utf-8');

to dostaję błąd:
Kod
Warning: Cannot modify header information - headers already sent by (output started at /home/users/...


Kod wtedy w całości wygląda następująco:
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. if (isset($_POST["submit"])) {
  4.  
  5. $name = $_POST['name'];
  6. $email = $_POST['email'];
  7. $message = $_POST['message'];
  8. $header = "Content-Type: text/html; charset=utf-8";
  9. $human = intval($_POST['human']);
  10. $from = '';
  11. $to = 'poprawny@mail.com';
  12. $subject = '';
  13.  
  14. $body ="$header\n Od: $name\n E-mail: $email\n Wiadomość:\n $message";
  15.  
  16. if (!$_POST['name']) {
  17. $errName = '';
  18. }
  19.  
  20.  
  21. if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  22. $errEmail = '';
  23. }
  24.  
  25.  
  26. if (!$_POST['message']) {
  27. $errMessage = '';
  28. }
  29.  
  30. if ($human !== 4) {
  31. $errHuman = '';
  32. }
  33.  
  34. if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
  35. if (mail ($to, $subject, $body, $from)) {
  36. $result='<div class="alert alert-success">';
  37. } else {
  38. $result='<div class="alert alert-danger">';
  39. }
  40. }
  41. }
  42. ?>


Dodam, że w mailu dostaję krzaki i "Content-Type: text/html; charset=UTF-8" doczepione do treści maila.

Będę bardzo wdzięczny za pomoc. Męczę się z tym od wczoraj :/
DonPolaczek
Spróbuj dodać:

a jeżeli masz dostęp do php.ini to dodaj lub zmień:
  1. output_buffering = On
platyna
Dodałem ob_start(); i dalej mam ten sam błąd. Niestety nie mam dostępu do php.ini
Początek kodu wygląda następująco:
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. if (isset($_POST["submit"])) {


Coś jest z moim kodem. Przy użyciu innego, prostrzego kodu, ale z uwzględnieniem polskich znaków przy jego tworzeniu, są one wysyłane prawidłowo.
nospor
Skad pomysl, ze naglowek (określenie kodowania maila) dodaje sie do body maila?questionmark.gif Naglowek czyli headers, nadaje sie w oddzielnym parametrze dla mail(). Wszystko to masz opisane i podane na przykladach w dokumentacji funkcji mail().
platyna
Dodałem $headers do mail() i teraz w ogołe maile nie są wysyłane.

  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. if (isset($_POST["submit"])) {
  4.  
  5. $name = $_POST['name'];
  6. $email = $_POST['email'];
  7. $message = $_POST['message'];
  8. $headers =("Content-Type: text/html; charset=UTF-8");
  9. $human = intval($_POST['human']);
  10. $from = '';
  11. $to = 'poprawny@mail.com';
  12. $subject = '';
  13.  
  14.  
  15. $body ="Od: $name\n E-mail: $email\n Wiadomość:\n $message";
  16.  
  17. if (!$_POST['name']) {
  18. $errName = '';
  19. }
  20.  
  21.  
  22. if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  23. $errEmail = '';
  24. }
  25.  
  26.  
  27. if (!$_POST['message']) {
  28. $errMessage = '';
  29. }
  30.  
  31. if ($human !== 4) {
  32. $errHuman = '';
  33. }
  34.  
  35. if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
  36. if (mail ($to, $subject, $body, $from, $headers)) {
  37. $result='<div class="alert alert-success"></div>';
  38. } else {
  39. $result='<div class="alert alert-danger"></div>';
  40. }
  41. }
  42. }
  43.  
  44. ?>
nospor
Prosze po raz ostatni: zajrzy do manuala, zobacz jakie funkcja mail() przyjmuje parametry a nie strzelasz na slepo. Wszystko robisz źle. A wystarczy zajrzec do dokumentacji. Tam masz wszystko opisane i podane na przykladach

http://uk1.php.net/manual/en/function.mail.php
DonPolaczek
Zobacz np tutaj masz wszystko podane:

  1.  
  2. <?php
  3. // multiple recipients
  4. $to = 'aidan@example.com' . ', '; // note the comma
  5. $to .= 'wez@example.com';
  6.  
  7. // subject
  8. $subject = 'Birthday Reminders for August';
  9.  
  10. // message
  11. $message = '
  12. <html>
  13. <head>
  14. <title>Birthday Reminders for August</title>
  15. </head>
  16. <body>
  17. <p>Here are the birthdays upcoming in August!</p>
  18. <table>
  19. <tr>
  20. <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
  21. </tr>
  22. <tr>
  23. <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
  24. </tr>
  25. <tr>
  26. <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
  27. </tr>
  28. </table>
  29. </body>
  30. </html>
  31. ';
  32.  
  33. // To send HTML mail, the Content-type header must be set
  34. $headers = 'MIME-Version: 1.0' . "\r\n";
  35. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  36.  
  37. // Additional headers
  38. $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
  39. $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
  40. $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
  41. $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
  42.  
  43. // Mail it
  44. mail($to, $subject, $message, $headers);
  45. ?>
  46.  
platyna
Dodałem wspomnianego headera i mail pod koniec kodu i częściowo działa, tzn. dostaję dwa maile: jeden z krzakami, drugi tylko z tresścią $message z polskimi znakami.

  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. if (isset($_POST["submit"])) {
  4.  
  5. $name = $_POST['name'];
  6. $email = $_POST['email'];
  7. $message = $_POST['message'];
  8. $headers =("Content-Type: text/html; charset=UTF-8");
  9. $human = intval($_POST['human']);
  10. $from = '';
  11. $to = 'poprawny@mail.com';
  12. $subject = '';
  13.  
  14.  
  15. $body ="Od: $name\n E-mail: $email\n Wiadomość:\n $message";
  16.  
  17. if (!$_POST['name']) {
  18. $errName = '';
  19. }
  20.  
  21.  
  22. if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  23. $errEmail = '';
  24. }
  25.  
  26.  
  27. if (!$_POST['message']) {
  28. $errMessage = '';
  29. }
  30.  
  31. if ($human !== 4) {
  32. $errHuman = '';
  33. }
  34.  
  35. if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
  36. if (mail ($to, $subject, $body, $from)) {
  37. $result='<div class="alert alert-success"></div>';
  38. } else {
  39. $result='<div class="alert alert-danger"></div>';
  40. }
  41. }
  42. $headers = 'Content-type: text/html; charset=UTF-8' . "\r\n";
  43.  
  44. mail($to, $subject, $message, $headers);
  45. }
  46.  
  47. ?>
patwoj98
Dalej nie wiem po co Ci ten header u góry "header('Content-Type: text/html; charset=utf-8');". Strona jest zakodowana i bez tego, więc ta linia jest zbędna.
Według manuala tak powinieneś dodać header:
  1. $headers = "MIME-Version: 1.0" . "\r\n" .
  2. "Content-type: text/html; charset=UTF-8" . "\r\n";
nospor
NAdal nie dodajesz zmiennej $headers do mail(). ALbo podajesz to jako zly argument, albo nie podajesz tego wcale...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.