Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: formularz php + js na stronie www - gdzie błąd?
Forum PHP.pl > Forum > PHP
lukaszk8807
Witajcie,

mam formularz kontakowy w php odpowiedzialny za wysyłanie maila ze strony. Coś w nim nie działa: mianowicie po uzupełnieniu wszystkich pól formularza wysyła informację "wiadomość nie została wysłana. Spróuj ponownie". Wszystkie pola formularza są wypałnione poprawnie.

Ktoś mógłby podpowiedzieć, gdzie jest błąd w kodzie?


Załaczam kod php
  1. <?php
  2.  
  3. // Your email address
  4. $to = 'moj.mail@mail.pl';
  5.  
  6. $subject = $_POST['subject'];;
  7.  
  8. // Don't edit below unless you know what you're doing
  9. if($to) {
  10. $name = $_POST['name'];
  11. $email = $_POST['email'];
  12.  
  13. $fields = array(
  14. 0 => array(
  15. 'text' => 'Name',
  16. 'val' => $_POST['name']
  17. ),
  18. 1 => array(
  19. 'text' => 'Email address',
  20. 'val' => $_POST['email']
  21. ),
  22. 2 => array(
  23. 'text' => 'Message',
  24. 'val' => $_POST['message']
  25. )
  26. );
  27.  
  28.  
  29. $message = "";
  30.  
  31. foreach($fields as $field) {
  32. $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
  33. }
  34.  
  35. /*
  36. $header.="Mime-Version: 1.0rn";
  37. $header.="Content-type: text/html; charset=UTF-8";
  38. $header.="Content-Transfer-Encoding: 8bitrn";
  39. $subject=iconv("UTF-8","ISO-8859-2", $subject);
  40. $subject='=?UTF-8?B?'.base64_encode($subject).'?=';
  41.  
  42. $headers .= "From: \"" . $name . "\" \r\n";
  43. $headers .= "Reply-To: " . $email . "\r\n";
  44. $message = utf8_decode($message);
  45. */
  46.  
  47. $naglowki = "Reply-to: ".$email."<".$email.">".PHP_EOL;
  48. $naglowki .= "From: <".$name.">".PHP_EOL;
  49. $naglowki .= "To: ".$to." <".$to.">".PHP_EOL;
  50. $naglowki .= "Content-Type: text/html; charset=utf-8".PHP_EOL;
  51. $naglowki .= "MIME-Version: 1.0".PHP_EOL;
  52. $naglowki .= "Content-Transfer-Encoding: 7bit".PHP_EOL;
  53.  
  54. $temat= "=?UTF-8?B?".base64_encode($subject)."?=";
  55.  
  56. //$content=iconv("UTF-8","ISO-8859-2", $content.$footer);
  57.  
  58. mail($to, $temat, $message, $naglowki);
  59.  
  60.  
  61. if ($message){
  62. echo 'sent';
  63. }else{
  64. echo 'failed';
  65. }
  66. } else {
  67. echo "Don't access this file directly";
  68. }
  69. ?>




oraz kod java script:

  1. (function($){
  2. $(document).ready(function() {
  3. $('#submit-form').click(function(e){
  4.  
  5. e.preventDefault();
  6. var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  7. var name = $('#rp_name').val(),
  8. email = $('#rp_email').val(),
  9. subject = $('#rp_subject').val(),
  10. message = $('#rp_message').val(),
  11. data_html,
  12. success = $('#success');
  13.  
  14. if(name == "")
  15. $('#rp_name').val('Prosimy, wpisz swoje imię');
  16.  
  17. if(subject == "")
  18. $('#rp_subject').val('Podaj temat wiadomości');
  19.  
  20. if(email == ""){
  21. $('#rp_email').val('Uzupełnij adres e-mail');
  22. }else if(reg.test(email) == false){
  23. $('#rp_email').val('Niepoprawny adres e-mail');
  24. }
  25.  
  26. if(message == "")
  27. $('#rp_message').val('Wpisz treść wiadomości');
  28.  
  29. if(message != "" && name != "" && reg.test(email) != false) {
  30. data_html = "name=" + name + "&email="+ email + "&message=" + message + "&subject="+ subject;
  31.  
  32. //alert(data_html);
  33. $.ajax({
  34. type: 'POST',
  35. url: 'php_helpers/contact_form.php',
  36. data: data_html,
  37. success: function(msg){
  38.  
  39. if (msg == 'sent'){
  40. success.html('<div class="alert alert-success">Wiadomość <strong> została wysłana!</strong> Dziękujemy!</div>') ;
  41. $('#rp_name').val('');
  42. $('#rp_email').val('');
  43.  
  44. $('#rp_message').val('');
  45. }else{
  46. success.html('<div class="alert alert-error">Wiadomość <strong>nie została wysłana </strong> Spróbuj ponownie!</div>') ;
  47. }
  48. }
  49. });
  50.  
  51. }
  52. return false;
  53. });
  54. });
  55. })(jQuery);
  56.  

Pyton_000
co mówi konsola przeglądarki
lukaszk8807
że wszystko ok
Cytat(Pyton_000 @ 25.11.2014, 09:27:56 ) *
co mówi konsola przeglądarki

Turson
Daj var_dump($message) na linii 60
lukaszk8807
Cytat(Turson @ 25.11.2014, 09:58:27 ) *
Daj var_dump($message) na linii 60

Bez zmian. Dzięki za pomoc
Turson
Bez zmian gdzie? Wiesz co w ogóle robisz? var_dump nie rozwiaze problemu ale moze pomoc w analizie, bo najwyrazniej ten warunek nie jest spelniony
  1. if ($message){
  2. echo 'sent';
  3. }

http://turson.pl/blog/jquery-ajax-analiza-...rkowej-konsoli/
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.