Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][JavaScript]Formularz kontaktowy – polskie kodowanie utf-8
Forum PHP.pl > Forum > Przedszkole
preibx
Witam.
Proszę o pomoc w uzupełnieniu poniższego skryptu o wysyłanie listów z polskimi znakami.
Z góry serdecznie dziękuje.
Plik contact.php
  1. <?php
  2. $to = "adres@firma.pl";
  3. $subject = "List wysłany z formularza";
  4.  
  5. if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["message"])) {
  6.  
  7. $content = "Nazywam się: " . $_POST["name"] . "\r\n";
  8. $content .= "Mój adres email: " . $_POST["email"] . "\r\n";
  9. $content .= "Treść wiadomości " . "\r\n" . $_POST["message"];
  10.  
  11. if (mail($to, $subject, $content, $_POST["email"])) {
  12.  
  13. $result = array(
  14. "message" => "Listy wysłany.",
  15. "sendstatus" => 1
  16. );
  17.  
  18. echo json_encode($result);
  19.  
  20. } else {
  21.  
  22. $result = array(
  23. "message" => "Niestety, coś jest nie tak.",
  24. "sendstatus" => 0
  25. );
  26.  
  27. echo json_encode($result);
  28.  
  29. }
  30.  
  31. }
  32.  
  33. ?>

Plik contact.js
  1. (function($){
  2.  
  3. $(document).ready(function() {
  4.  
  5. /* ---------------------------------------------- /*
  6. * Contact form ajax
  7. /* ---------------------------------------------- */
  8.  
  9. $('#contact-form').find('input,textarea').jqBootstrapValidation({
  10. preventSubmit: true,
  11. submitError: function($form, event, errors) {
  12. // additional error messages or events
  13. },
  14. submitSuccess: function($form, event) {
  15. event.preventDefault();
  16.  
  17. var submit = $('#contact-form submit');
  18. var ajaxResponse = $('#contact-response');
  19.  
  20. var name = $("input#cname").val();
  21. var email = $("input#cemail").val();
  22. var message = $("textarea#cmessage").val();
  23.  
  24. $.ajax({
  25. type: 'POST',
  26. url: 'assets/php/contact.php',
  27. dataType: 'json',
  28. data: {
  29. name: name,
  30. email: email,
  31. message: message,
  32. },
  33. cache: false,
  34. beforeSend: function(result) {
  35. submit.empty();
  36. submit.append('<i class="fa fa-cog fa-spin"></i> Wait...');
  37. },
  38. success: function(result) {
  39. if(result.sendstatus == 1) {
  40. ajaxResponse.html(result.message);
  41. $form.fadeOut(500);
  42. } else {
  43. ajaxResponse.html(result.message);
  44. }
  45. }
  46. });
  47. }
  48. });
  49.  
  50. });
  51.  
  52. })(jQuery);

robertpiaty
Sprawdź kodowanie i ustaw odpowiedni header w funkcji mail content-type i charset (czwarty parametr) Jak to zrobić masz pokazane na stronie http://php.net/manual/en/function.mail.php np. w Example #4
preibx
Dziękuje.
Temat listu rzeczywiście się poprawił.
Ale treść jest źle kodowana.
Wydaje mi się, że chodzi o złe kodowanie AJAXU.
Próbowałem, wstawić kodowanie, ale wtedy blokuje się cały skrypt.
Obecnie plik contakt.php wygląda tak:
  1. <?php
  2. $subject = "=?UTF-8?B?".base64_encode("Wiadomość z mojej witryny od ".$_POST['name'])."?=";
  3. $naglowki ="From: name <email>\r\n".
  4. "MIME-Version: 1.0" . "\r\n" .
  5. "Content-type: text/html; charset=UTF-8" . "\r\n";
  6. $to = "adres@firma.pl";
  7. if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["message"])) {
  8. $content = "Nazywam sie: " . $_POST["name"] . "\r\n";
  9. $content .= "Moj adres email: " . $_POST["email"] . "\r\n";
  10. $content .= "Wiadomosc: " . "\r\n" . $_POST["message"];
  11. if (mail($to, $subject, $content, $_POST["email"], $naglowki)) {
  12. $result = array(
  13. "message" => "Listy wysłany.",
  14. "sendstatus" => 1
  15. );
  16. echo json_encode($result);
  17. } else {
  18.  
  19. $result = array(
  20. "message" => "Niestety, coś jest nie tak.",
  21. "sendstatus" => 0
  22. );
  23. echo json_encode($result);
  24. }
  25. }
  26. ?>

A plik contact.js
  1. (function($){
  2.  
  3. $(document).ready(function() {
  4.  
  5. /* ---------------------------------------------- /*
  6. * Contact form ajax
  7. /* ---------------------------------------------- */
  8.  
  9. $('#contact-form').find('input,textarea').jqBootstrapValidation({
  10. preventSubmit: true,
  11. submitError: function($form, event, errors) {
  12. // additional error messages or events
  13. },
  14. submitSuccess: function($form, event) {
  15. event.preventDefault();
  16. // encoding:"UTF-8",
  17. // dataType:"html",
  18. // contentType: "text/plain; charset=UTF-8",
  19.  
  20. // dataType:"html", contentType: "text/plain; charset=UTF-8" } )
  21. var submit = $('#contact-form submit');
  22. var ajaxResponse = $('#contact-response');
  23.  
  24. var name = $("input#cname").val();
  25. var email = $("input#cemail").val();
  26. var message = $("textarea#cmessage").val();
  27.  
  28. $.ajax({
  29. type: 'POST',
  30. url: 'assets/php/contact.php',
  31. // contentType: "application/json; charset=utf-8",
  32. dataType: 'json',
  33. data: {
  34. name: name,
  35. email: email,
  36. message: message,
  37. },
  38. cache: false,
  39. beforeSend: function(result) {
  40. submit.empty();
  41. submit.append('<i class="fa fa-cog fa-spin"></i> Wait...');
  42. },
  43. success: function(result) {
  44. if(result.sendstatus == 1) {
  45. ajaxResponse.html(result.message);
  46. $form.fadeOut(500);
  47. } else {
  48. ajaxResponse.html(result.message);
  49. }
  50. }
  51. });
  52. }
  53. });
  54.  
  55. });
  56.  
  57. })(jQuery);
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.