Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] formularz kontaktowy
-kk-
post 17.05.2014, 13:00:58
Post #1





Goście







Witam, czy mogę prosić o pomoc, czy można wyświetlać komunikaty o wysłaniu maila lub o błędzie w tym samym formularzu a nie na nowej pustej stronie ?

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Dokument bez tytułu</title>
  5. </head>
  6.  
  7.  
  8.  
  9. <form name="contactform" method="post" action="send_form_email.php">
  10.  
  11. <table width="450px">
  12.  
  13. <tr>
  14.  
  15. <td valign="top">
  16.  
  17. <label for="first_name">First Name *</label>
  18.  
  19. </td>
  20.  
  21. <td valign="top">
  22.  
  23. <input type="text" name="first_name" maxlength="50" size="30">
  24.  
  25. </td>
  26.  
  27. </tr>
  28.  
  29. <tr>
  30.  
  31. <td valign="top"">
  32.  
  33. <label for="last_name">Last Name *</label>
  34.  
  35. </td>
  36.  
  37. <td valign="top">
  38.  
  39. <input type="text" name="last_name" maxlength="50" size="30">
  40.  
  41. </td>
  42.  
  43. </tr>
  44.  
  45. <tr>
  46.  
  47. <td valign="top">
  48.  
  49. <label for="email">Email Address *</label>
  50.  
  51. </td>
  52.  
  53. <td valign="top">
  54.  
  55. <input type="text" name="email" maxlength="80" size="30">
  56.  
  57. </td>
  58.  
  59. </tr>
  60.  
  61. <tr>
  62.  
  63. <td valign="top">
  64.  
  65. <label for="telephone">Telephone Number</label>
  66.  
  67. </td>
  68.  
  69. <td valign="top">
  70.  
  71. <input type="text" name="telephone" maxlength="30" size="30">
  72.  
  73. </td>
  74.  
  75. </tr>
  76.  
  77. <tr>
  78.  
  79. <td valign="top">
  80.  
  81. <label for="comments">Comments *</label>
  82.  
  83. </td>
  84.  
  85. <td valign="top">
  86.  
  87. <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  88.  
  89. </td>
  90.  
  91. </tr>
  92.  
  93. <tr>
  94.  
  95. <td colspan="2" style="text-align:center">
  96.  
  97. <input type="submit" value="Submit">
  98.  
  99. </td>
  100.  
  101. </tr>
  102.  
  103.  
  104. </form>
  105.  
  106.  
  107. </body>
  108. </html>
  109.  


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Dokument bez tytułu</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10.  
  11. <?php
  12.  
  13. if(isset($_POST['email'])) {
  14.  
  15.  
  16.  
  17. // EDIT THE 2 LINES BELOW AS REQUIRED
  18.  
  19. $email_to = "aa@wp.pl";
  20.  
  21. $email_subject = "Your email subject line";
  22.  
  23.  
  24.  
  25.  
  26.  
  27. function died($error) {
  28.  
  29. // your error code can go here
  30.  
  31. echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  32.  
  33. echo "These errors appear below.<br /><br />";
  34.  
  35. echo $error."<br /><br />";
  36.  
  37. echo "Please go back and fix these errors.<br /><br />";
  38.  
  39. die();
  40.  
  41. }
  42.  
  43.  
  44.  
  45. // validation expected data exists
  46.  
  47. if(!isset($_POST['first_name']) ||
  48.  
  49. !isset($_POST['last_name']) ||
  50.  
  51. !isset($_POST['email']) ||
  52.  
  53. !isset($_POST['telephone']) ||
  54.  
  55. !isset($_POST['comments'])) {
  56.  
  57. died('We are sorry, but there appears to be a problem with the form you submitted.');
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. $first_name = $_POST['first_name']; // required
  67.  
  68. $last_name = $_POST['last_name']; // required
  69.  
  70. $email_from = $_POST['email']; // required
  71.  
  72. $telephone = $_POST['telephone']; // not required
  73.  
  74. $comments = $_POST['comments']; // required
  75.  
  76.  
  77.  
  78. $error_message = "";
  79.  
  80. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  81.  
  82. if(!preg_match($email_exp,$email_from)) {
  83.  
  84. $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  85.  
  86. }
  87.  
  88. $string_exp = "/^[A-Za-z .'-]+$/";
  89.  
  90. if(!preg_match($string_exp,$first_name)) {
  91.  
  92. $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  93.  
  94. }
  95.  
  96. if(!preg_match($string_exp,$last_name)) {
  97.  
  98. $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  99.  
  100. }
  101.  
  102. if(strlen($comments) < 2) {
  103.  
  104. $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  105.  
  106. }
  107.  
  108. if(strlen($error_message) > 0) {
  109.  
  110. died($error_message);
  111.  
  112. }
  113.  
  114. $email_message = "Form details below.\n\n";
  115.  
  116.  
  117.  
  118. function clean_string($string) {
  119.  
  120. $bad = array("content-type","bcc:","to:","cc:","href");
  121.  
  122. return str_replace($bad,"",$string);
  123.  
  124. }
  125.  
  126.  
  127.  
  128. $email_message .= "First Name: ".clean_string($first_name)."\n";
  129.  
  130. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  131.  
  132. $email_message .= "Email: ".clean_string($email_from)."\n";
  133.  
  134. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  135.  
  136. $email_message .= "Comments: ".clean_string($comments)."\n";
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. // create email headers
  144.  
  145. $headers = 'From: '.$email_from."\r\n".
  146.  
  147. 'Reply-To: '.$email_from."\r\n" .
  148.  
  149. 'X-Mailer: PHP/' . phpversion();
  150.  
  151. @mail($email_to, $email_subject, $email_message, $headers);
  152.  
  153. ?>
  154.  
  155.  
  156.  
  157. <!-- include your own success html here -->
  158.  
  159.  
  160.  
  161. Thank you for contacting us. We will be in touch with you very soon.
  162.  
  163. <?php
  164.  
  165. }
  166.  
  167. ?>
  168.  
  169.  
  170.  
  171.  
  172. </body>
  173. </html>
Go to the top of the page
+Quote Post

Posty w temacie
- kk   [PHP] formularz kontaktowy   17.05.2014, 13:00:58
- - Kshyhoo   Formularz musi przekierowywać na siebie, czyli umi...   17.05.2014, 13:13:21
- - kk   Wielkie dzięki, wyświetla poprawnie.   18.05.2014, 00:58:22
- - kk   Mam jeszcze jeden problem umieściłem formularz w s...   19.05.2014, 19:27:52
- - johny_s   skoro zabijasz skrypt w połowie strony to co się d...   19.05.2014, 19:34:56
- - kk   Funkcja wykonuję się do końca, w każdym wariancie ...   20.05.2014, 19:40:41
- - kk   Dopisałem na końcu warunek że jeśli nie ma błędów ...   21.05.2014, 22:34:12
- - kk   Dlaczego pomimo niespełnienia warunku maile są wys...   27.05.2014, 19:41:53
- - IProSoft   Ponieważ mimo, że formularz ma błędy, pozwalasz na...   27.05.2014, 21:36:02
- - Gość   Dziękuję bardzo teraz działa prawidłowo   27.05.2014, 22:07:59
- - kk   Chciałem jeszcze dodać możliwość wysyłania plików,...   2.06.2014, 02:21:38
- - kk   Użyłem phpmailer, załączniki są dostarczane. W jak...   3.06.2014, 08:50:29
- - Turson   Bo wysyłasz 2 razy. Raz phpmailerem a raz mail()   3.06.2014, 09:04:02
- - kk   dlaczego wyskakuje błąd po ustawieniu [PHP] pobie...   3.06.2014, 11:05:45
- - kk   Co jest błędnego w takim zapisie [PHP] pobierz, ...   5.06.2014, 13:37:43
- - kk   Ok poradziłem sobie z widocznością maila z formula...   5.06.2014, 23:47:14
- - kk   Nikt nie podpowie jak to osiągnąć   8.06.2014, 18:24:40
- - johny_s   dodaj sobie warunek żeby nie wyświetlało danych je...   8.06.2014, 18:33:01
- - kk   Próbuję coś takiego, ale to chyba nie do końca jes...   10.06.2014, 22:47:34
- - kk   Gdzie jest błąd takiego zapisu ? jak mail zostanie...   11.06.2014, 08:16:22
- - Turson   Ale jak odświeżysz stronę to formularz przesle się...   11.06.2014, 08:20:39
- - kk   czy teraz będzie zabezpieczone przed odświeżaniem ...   11.06.2014, 09:17:37
- - Turson   Jeżeli died oznacza po prostu przerwanie wykonywan...   11.06.2014, 09:23:08
- - kk   Dziękuję, zabezpieczenie działa. Jak rozwiązać pro...   11.06.2014, 09:41:48
- - kk   Chyba wszystko działa, dziękuję za pomoc.   11.06.2014, 09:45:34
- - kk   Jeszcze jednak jest mały mankament, a mianowicie t...   11.06.2014, 10:38:44
- - nospor   Zapisz komunikat do sesji, na stronie na ktorą prz...   11.06.2014, 10:43:29
- - kk   coś w tym stylu ?, wszystko jest na jednej stronie...   11.06.2014, 11:06:35
- - Turson   [PHP] pobierz, plaintext if(isset($_SESSIO...   11.06.2014, 11:08:03
- - nospor   Tak, cos w tym stylu   11.06.2014, 11:15:40
- - kk   Nie wiem za bardzo czy źle wstawiłem kod sesji, cz...   11.06.2014, 11:24:22
- - kk   Co zrobić aby po odświeżeniu pozostawał komunikat ...   11.06.2014, 19:09:50
- - byervie has   Nie trzymac go w sesji.   11.06.2014, 19:15:37
- - kk   sesja miała właśnie służyć do wyświetlenia komunik...   11.06.2014, 20:11:25
- - gosc   to nie usuwaj tej sesji session_destroyem() moze p...   11.06.2014, 20:46:15
- - kk   nie pomogło   11.06.2014, 20:51:24
- - kk   W jaki inny sposób wyświetlić komunikat po odśwież...   12.06.2014, 06:07:07
- - kk   Próbuje cały czas rozwiązać problem wyświetlania k...   14.06.2014, 16:12:14
- - Turson   [PHP] pobierz, plaintext exit();echo $_SE...   14.06.2014, 16:14:08
- - kk   teraz komunikat się pojawia ale znika po odświeżen...   14.06.2014, 16:44:12
- - Turson   Chyba ma zniknąć przecież po wyświetleniu się, co ...   14.06.2014, 16:48:40
- - kk   Tak, chcę wyświetlić komunikat po odświeżeniu. Zap...   14.06.2014, 17:08:16
- - Turson   [PHP] pobierz, plaintext if(!$mail->Send...   14.06.2014, 17:14:23
- - kk   naprawdę wielkie dzięki za pomoc ale nadal nie wyś...   14.06.2014, 17:26:34
- - Turson   Daj na samym początku kodu [PHP] pobierz, plaintex...   14.06.2014, 17:29:04
- - kk   jak wstawię na początku skryptu to pokazuje null c...   14.06.2014, 17:54:47
- - Turson   Aaaa session_start(); powinno być na samym początk...   14.06.2014, 17:56:08
- - kk   niestety efekt jest jak poprzednio   14.06.2014, 18:02:36


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 12:18