Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] formularz kontaktowy
-kk-
post
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
3 Stron V   1 2 3 >  
Start new topic
Odpowiedzi (1 - 47)
Kshyhoo
post
Post #2





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Formularz musi przekierowywać na siebie, czyli umieść kod w jednym pliku. Najprościej.
Go to the top of the page
+Quote Post
-kk-
post
Post #3





Goście







Wielkie dzięki, wyświetla poprawnie.
Go to the top of the page
+Quote Post
-kk-
post
Post #4





Goście







Mam jeszcze jeden problem umieściłem formularz w szablonie strony, jeśli mail zostanie wysłany poprawnie wyświetla się prawidłowo zielony komunikat. Natomiast w przypadku braku wypełnienia jakiegoś pola wyświetla się komunikat ale jednocześnie znika tło szablonu i dolny pasek. O o co chodzi ? proszę o pomoc.

szablon z formularzem
(IMG:https://www.dropbox.com/s/p9wzl1tsgszoimd/1.gif)

1

po naciśnięciu przycisku wyślij w formularzu w przypadku braku wypełnionych pól
(IMG:https://www.dropbox.com/s/xb33ko1p0j6kcuh/2.gif)
2

Wszystkie pliki można pobrać z:
https://www.dropbox.com/sh/e1ly1rhq9o9y14o/...e6bG6pZNZB1ouma

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  5. <head>
  6. <title></title>
  7.  
  8.  
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  10. <meta name="keywords" content="" />
  11. <meta name="description" content="" />
  12. <link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
  13.  
  14. <link rel="SHORTCUT ICON" href="favicon.ico" />
  15. </head>
  16.  
  17. <script>
  18. <!--
  19. function displayWindow(url, width, height) {
  20. var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
  21. }
  22. //-->
  23. </SCRIPT>
  24.  
  25.  
  26.  
  27. <body>
  28.  
  29. <div id="outer">
  30. <div id="wrapper">
  31. <div id="header">
  32.  
  33. <div id="menu">
  34.  
  35. <p id="m">&nbsp;</p>
  36. <p id="m1">&nbsp;</p>
  37. <p id="m2">&nbsp;</p>
  38. <p id="m3">&nbsp;</p>
  39. </div>
  40.  
  41. <div id="aaa">
  42.  
  43. <A HREF="java script:displayWindow('aaa.html',450,400)">
  44. <button style="color: red; font-size:16px; width:120px; height:35px; " name="button1"></button></a>
  45.  
  46.  
  47. </div>
  48. </div>
  49. <div id="nav">
  50. <div></div>
  51. </div>
  52. <div></div>
  53. <div class="twitter" id="newsletter_input">
  54. <p>&nbsp;</p>
  55. <p></p>
  56. </div>
  57. <div id="body">
  58. <div id="body-nner">
  59. <div id="body-left">
  60.  
  61. <div id="naglowek_aa">
  62.  
  63. <font color="#008BD2">Kontakt</font>
  64.  
  65. </div>
  66. <br>
  67.  
  68.  
  69. <form name="contactform" method="post" action="">
  70.  
  71. <table width="450px" align="center">
  72.  
  73. <tr>
  74.  
  75. <td valign="top">
  76.  
  77. <label for="first_name"><br>
  78. &nbsp;&nbsp;&nbsp;Imię *</label>
  79.  
  80. </td>
  81.  
  82. <td valign="top">
  83.  
  84. <br /> <input name="first_name" type="text" class="i1" size="30" maxlength="50">
  85.  
  86. </td>
  87.  
  88. </tr>
  89.  
  90. <tr>
  91.  
  92. <td height="26" valign="top"">
  93.  
  94. <label for="last_name">&nbsp;&nbsp;&nbsp;Nazwisko *</label>
  95.  
  96. </td>
  97.  
  98. <td valign="top">
  99.  
  100. <input class="i1" type="text" name="last_name" maxlength="50" size="30">
  101.  
  102. </td>
  103.  
  104. </tr>
  105.  
  106. <tr>
  107.  
  108. <td valign="top">
  109.  
  110. <label for="email">&nbsp;&nbsp;&nbsp;Email Address *</label>
  111.  
  112. </td>
  113.  
  114. <td valign="top">
  115.  
  116. <input class="i1" type="text" name="email" maxlength="80" size="30">
  117.  
  118. </td>
  119.  
  120. </tr>
  121.  
  122. <tr>
  123.  
  124. <td valign="top">
  125.  
  126. <label for="telephone">&nbsp;&nbsp;&nbsp;Numer telefonu</label>
  127.  
  128. </td>
  129.  
  130. <td valign="top">
  131.  
  132. <input class="i1" type="text" name="telephone" maxlength="30" size="30">
  133.  
  134. </td>
  135.  
  136. </tr>
  137.  
  138. <tr>
  139.  
  140. <td valign="top">
  141.  
  142. <label for="comments">&nbsp;&nbsp;&nbsp;Zapytanie *</label>
  143.  
  144. </td>
  145.  
  146. <td valign="top">
  147.  
  148. <textarea class="i1" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  149.  
  150. </td>
  151.  
  152. </tr>
  153.  
  154. <tr>
  155.  
  156. <td colspan="2" style="text-align:center">
  157.  
  158. <input class="i2" type="submit" value="wyślij" style="width: 80px; height: 35px"></td>
  159.  
  160. </tr>
  161.  
  162. </table>
  163.  
  164. </form>
  165.  
  166.  
  167. <?php
  168.  
  169. if(isset($_POST['email'])) {
  170.  
  171.  
  172.  
  173. // EDIT THE 2 LINES BELOW AS REQUIRED
  174.  
  175. $email_to = "aa@wp.pl";
  176.  
  177. $email_subject = "Your email subject line";
  178.  
  179. function died($error) {
  180.  
  181. // your error code can go here
  182. echo "<br />";
  183. echo '<span style="font-size:10px" "color:#FF0000;">We are very sorry, but there were error(s) found with the form you submitted.</span> ';
  184. echo "<br />";
  185.  
  186.  
  187. echo '<span style="font-size:10px" "color:#FF0000;">These errors appear below.<br /><br /></span> ';
  188.  
  189. echo $error."<br /><br />";
  190.  
  191. echo '<span style="font-size:10px" color:#FF0000;">Please go back and fix these errors.<br /><br /></span> ';
  192.  
  193. die();
  194.  
  195. }
  196.  
  197.  
  198.  
  199. // validation expected data exists
  200.  
  201. if(!isset($_POST['first_name']) ||
  202.  
  203. !isset($_POST['last_name']) ||
  204.  
  205. !isset($_POST['email']) ||
  206.  
  207. !isset($_POST['telephone']) ||
  208.  
  209. !isset($_POST['comments'])) {
  210.  
  211. died('We are sorry, but there appears to be a problem with the form you submitted.');
  212.  
  213. }
  214.  
  215. $first_name = $_POST['first_name']; // required
  216.  
  217. $last_name = $_POST['last_name']; // required
  218.  
  219. $email_from = $_POST['email']; // required
  220.  
  221. $telephone = $_POST['telephone']; // not required
  222.  
  223. $comments = $_POST['comments']; // required
  224.  
  225.  
  226.  
  227. $error_message = "";
  228.  
  229. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  230.  
  231. if(!preg_match($email_exp,$email_from)) {
  232.  
  233. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  234.  
  235. }
  236.  
  237. $string_exp = "/^[A-Za-z .'-]+$/";
  238.  
  239. if(!preg_match($string_exp,$first_name)) {
  240.  
  241. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  242.  
  243. }
  244.  
  245. if(!preg_match($string_exp,$last_name)) {
  246.  
  247. $error_message .='<span style="font-size: 10px;" color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  248.  
  249. }
  250.  
  251. if(strlen($comments) < 2) {
  252.  
  253. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  254.  
  255. }
  256.  
  257. if(strlen($error_message) > 0) {
  258.  
  259. died($error_message);
  260.  
  261. }
  262.  
  263. $email_message = "Form details below.\n\n";
  264.  
  265.  
  266.  
  267. function clean_string($string) {
  268.  
  269. $bad = array("content-type","bcc:","to:","cc:","href");
  270.  
  271. return str_replace($bad,"",$string);
  272.  
  273. }
  274.  
  275. $email_message .= "First Name: ".clean_string($first_name)."\n";
  276.  
  277. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  278.  
  279. $email_message .= "Email: ".clean_string($email_from)."\n";
  280.  
  281. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  282.  
  283. $email_message .= "Comments: ".clean_string($comments)."\n";
  284.  
  285. // create email headers
  286.  
  287. $headers = 'From: '.$email_from."\r\n".
  288.  
  289. 'Reply-To: '.$email_from."\r\n" .
  290.  
  291. 'X-Mailer: PHP/' . phpversion();
  292.  
  293. @mail($email_to, $email_subject, $email_message, $headers);
  294.  
  295. ?>
  296.  
  297.  
  298.  
  299. <!-- include your own success html here -->
  300.  
  301.  
  302. <br />
  303. <span class="gg">Thank you for contacting us. We will be in touch with you very soon.</span>
  304. <?php
  305.  
  306. }
  307.  
  308. ?>
  309.  
  310.  
  311.  
  312.  
  313.  
  314. <br>
  315.  
  316. <center>
  317. </center>
  318.  
  319.  
  320.  
  321.  
  322. </div>
  323. <div class="clear"></div>
  324. </div>
  325. <div id="copyright">
  326. <div id="copyright-left"> </div>
  327. </div>
  328. <div class="clear">&nbsp;</div>
  329. </div>
  330. </div>
  331. </div>
  332.  
  333.  
  334. <div id="pasek_dolny">
  335. <div id="copyright">
  336. <div id="copyright-left"></div>
  337. </div>
  338. <div id="kontakt"></div>
  339. </div>
  340. </body>
  341. </html>
  342.  


Go to the top of the page
+Quote Post
johny_s
post
Post #5





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

Ostrzeżenie: (0%)
-----


skoro zabijasz skrypt w połowie strony to co się dziwisz że Ci całości nie wyświetla, wyświetl komunikat o błędzie ale zrezygnuj z zabijania skrypt tylko przerób sobie warunek na wysyłanie maila
Go to the top of the page
+Quote Post
-kk-
post
Post #6





Goście







Funkcja wykonuję się do końca, w każdym wariancie oprócz komunikatu o błędzie wyświetla do tego komunikat o spełnieniu warunku. Który warunek ? jak przerobić żeby nie pokazywał zawsze informacji o spełnieniu warunków.


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  5. <head>
  6. <title></title>
  7.  
  8.  
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  10. <meta name="keywords" content="" />
  11. <meta name="description" content="" />
  12. <link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
  13.  
  14. <link rel="SHORTCUT ICON" href="favicon.ico" />
  15. </head>
  16.  
  17. <script>
  18. <!--
  19. function displayWindow(url, width, height) {
  20. var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
  21. }
  22. //-->
  23. </SCRIPT>
  24.  
  25.  
  26.  
  27. <body>
  28.  
  29. <div id="outer">
  30. <div id="wrapper">
  31. <div id="header">
  32.  
  33. <div id="menu">
  34.  
  35. <p id="m">&nbsp;</p>
  36. <p id="m1">&nbsp;</p>
  37. <p id="m2">&nbsp;</p>
  38. <p id="m3">&nbsp;</p>
  39. </div>
  40.  
  41. <div id="aaa">
  42.  
  43. <A HREF="java script:displayWindow('aaa.html',450,400)">
  44. <button style="color: red; font-size:16px; width:120px; height:35px; " name="button1"></button></a>
  45.  
  46.  
  47. </div>
  48. </div>
  49.  
  50.  
  51.  
  52.  
  53. <div id="nav">
  54. <div></div>
  55. </div>
  56. <div></div>
  57. <div class="twitter" id="newsletter_input">
  58. <p>&nbsp;</p>
  59. <p></p>
  60. </div>
  61. <div id="body">
  62. <div id="body-nner">
  63. <div id="body-left">
  64.  
  65. <div id="naglowek_aa">
  66.  
  67. <font color="#008BD2">Kontakt</font>
  68.  
  69. </div>
  70. <br>
  71.  
  72.  
  73. <form name="contactform" method="post" action="">
  74.  
  75. <table width="450px" align="center">
  76.  
  77. <tr>
  78.  
  79. <td valign="top">
  80.  
  81. <label for="first_name"><br>
  82. &nbsp;&nbsp;&nbsp;Imię *</label>
  83.  
  84. </td>
  85.  
  86. <td valign="top">
  87.  
  88. <br /> <input name="first_name" type="text" class="i1" size="30" maxlength="50">
  89.  
  90. </td>
  91.  
  92. </tr>
  93.  
  94. <tr>
  95.  
  96. <td height="26" valign="top"">
  97.  
  98. <label for="last_name">&nbsp;&nbsp;&nbsp;Nazwisko *</label>
  99.  
  100. </td>
  101.  
  102. <td valign="top">
  103.  
  104. <input class="i1" type="text" name="last_name" maxlength="50" size="30">
  105.  
  106. </td>
  107.  
  108. </tr>
  109.  
  110. <tr>
  111.  
  112. <td valign="top">
  113.  
  114. <label for="email">&nbsp;&nbsp;&nbsp;Email Address *</label>
  115.  
  116. </td>
  117.  
  118. <td valign="top">
  119.  
  120. <input class="i1" type="text" name="email" maxlength="80" size="30">
  121.  
  122. </td>
  123.  
  124. </tr>
  125.  
  126. <tr>
  127.  
  128. <td valign="top">
  129.  
  130. <label for="telephone">&nbsp;&nbsp;&nbsp;Numer telefonu</label>
  131.  
  132. </td>
  133.  
  134. <td valign="top">
  135.  
  136. <input class="i1" type="text" name="telephone" maxlength="30" size="30">
  137.  
  138. </td>
  139.  
  140. </tr>
  141.  
  142. <tr>
  143.  
  144. <td valign="top">
  145.  
  146. <label for="comments">&nbsp;&nbsp;&nbsp;Zapytanie *</label>
  147.  
  148. </td>
  149.  
  150. <td valign="top">
  151.  
  152. <textarea class="i1" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  153.  
  154. </td>
  155.  
  156. </tr>
  157.  
  158. <tr>
  159.  
  160. <td colspan="2" style="text-align:center">
  161.  
  162. <input class="i2" type="submit" value="wyślij" style="width: 80px; height: 35px"></td>
  163.  
  164. </tr>
  165.  
  166. </table>
  167.  
  168. </form>
  169.  
  170.  
  171. <?php
  172.  
  173. if(isset($_POST['email'])) {
  174.  
  175.  
  176.  
  177. // EDIT THE 2 LINES BELOW AS REQUIRED
  178.  
  179. $email_to = "aa@wp.pl";
  180.  
  181. $email_subject = "Your email subject line";
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. function died($error) {
  190.  
  191. // your error code can go here
  192.  
  193. echo "<br />";
  194.  
  195.  
  196.  
  197. echo $error."<br /><br />";
  198.  
  199.  
  200. }
  201.  
  202.  
  203.  
  204. // validation expected data exists
  205.  
  206. if(!isset($_POST['first_name']) ||
  207.  
  208. !isset($_POST['last_name']) ||
  209.  
  210. !isset($_POST['email']) ||
  211.  
  212. !isset($_POST['telephone']) ||
  213.  
  214. !isset($_POST['comments']))
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. $first_name = $_POST['first_name']; // required
  222.  
  223. $last_name = $_POST['last_name']; // required
  224.  
  225. $email_from = $_POST['email']; // required
  226.  
  227. $telephone = $_POST['telephone']; // not required
  228.  
  229. $comments = $_POST['comments']; // required
  230.  
  231.  
  232.  
  233. $error_message = "";
  234.  
  235. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  236.  
  237. if(!preg_match($email_exp,$email_from)) {
  238.  
  239. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  240.  
  241. }
  242.  
  243. $string_exp = "/^[A-Za-z .'-]+$/";
  244.  
  245. if(!preg_match($string_exp,$first_name)) {
  246.  
  247. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  248.  
  249. }
  250.  
  251. if(!preg_match($string_exp,$last_name)) {
  252.  
  253. $error_message .='<span style="font-size: 10px;" color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  254.  
  255. }
  256.  
  257. if(strlen($comments) < 2) {
  258.  
  259. $error_message .= '<span style="font-size: 10px;" color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  260.  
  261. }
  262.  
  263. if(strlen($error_message) > 0) {
  264.  
  265. died($error_message);
  266.  
  267.  
  268. }
  269.  
  270. $email_message = "Form details below.\n\n";
  271.  
  272.  
  273.  
  274. function clean_string($string) {
  275.  
  276. $bad = array("content-type","bcc:","to:","cc:","href");
  277.  
  278. return str_replace($bad,"",$string);
  279.  
  280. }
  281.  
  282.  
  283.  
  284. $email_message .= "First Name: ".clean_string($first_name)."\n";
  285.  
  286. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  287.  
  288. $email_message .= "Email: ".clean_string($email_from)."\n";
  289.  
  290. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  291.  
  292. $email_message .= "Comments: ".clean_string($comments)."\n";
  293.  
  294.  
  295.  
  296.  
  297.  
  298. // create email headers
  299.  
  300. $headers = 'From: '.$email_from."\r\n".
  301.  
  302. 'Reply-To: '.$email_from."\r\n" .
  303.  
  304. 'X-Mailer: PHP/' . phpversion();
  305.  
  306. @mail($email_to, $email_subject, $email_message, $headers);
  307.  
  308. ?>
  309.  
  310.  
  311.  
  312. <!-- include your own success html here -->
  313.  
  314.  
  315. <br />
  316. <span class="gg">Thank you for contacting us. We will be in touch with you very soon.</span>
  317. <?php
  318.  
  319. }
  320.  
  321. ?>
  322.  
  323.  
  324.  
  325.  
  326.  
  327. <br>
  328.  
  329. <center>
  330. </center>
  331.  
  332.  
  333.  
  334.  
  335. </div>
  336. <div class="clear"></div>
  337. </div>
  338. <div id="copyright">
  339. <div id="copyright-left"> </div>
  340. </div>
  341. <div class="clear">&nbsp;</div>
  342. </div>
  343. </div>
  344. </div>
  345.  
  346.  
  347. <div id="pasek_dolny">
  348. <div id="copyright">
  349. <div id="copyright-left"></div>
  350. </div>
  351. <div id="kontakt"></div>
  352. </div>
  353. </body>
  354. </html>
  355.  
Go to the top of the page
+Quote Post
-kk-
post
Post #7





Goście







Dopisałem na końcu warunek że jeśli nie ma błędów to wyświetlany jest nowy komunikat. Ale teraz dostaję maile nawet jeśli jest błędnie wypełniony formularz. Proszę o pomoc w czym jest problem ?

  1. $error_message = "";
  2.  
  3. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  4.  
  5. if(!preg_match($email_exp,$email_from)) {
  6.  
  7. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  8.  
  9. }
  10.  
  11. $string_exp = "/^[A-Za-z .'-]+$/";
  12.  
  13. if(!preg_match($string_exp,$first_name)) {
  14.  
  15. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  16.  
  17. }
  18.  
  19. if(!preg_match($string_exp,$last_name)) {
  20.  
  21. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  22.  
  23. }
  24.  
  25. if(strlen($comments) < 2) {
  26.  
  27. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  28.  
  29. }
  30.  
  31. if(strlen($error_message) > 0) {
  32.  
  33. died($error_message);
  34. }
  35.  
  36. if (strlen($error_message) == 0) {
  37. echo "<br />";
  38. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  39. }
  40.  
  41. $email_message = "Form details below.\n\n";
Go to the top of the page
+Quote Post
-kk-
post
Post #8





Goście







Dlaczego pomimo niespełnienia warunku maile są wysyłane. Komunikaty wyświetlają się prawidłowo ale maile zawsze dochodzą, nawet puste dlaczego (IMG:style_emoticons/default/questionmark.gif) (IMG:style_emoticons/default/questionmark.gif) pomocy


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  5. <head>
  6. <title></title>
  7.  
  8.  
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  10. <meta name="keywords" content="" />
  11. <meta name="description" content="" />
  12. <link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
  13.  
  14. <link rel="SHORTCUT ICON" href="favicon.ico" />
  15. </head>
  16.  
  17. <script>
  18. <!--
  19. function displayWindow(url, width, height) {
  20. var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
  21. }
  22. //-->
  23. </SCRIPT>
  24.  
  25.  
  26.  
  27. <body>
  28.  
  29. <div id="outer">
  30. <div id="wrapper">
  31. <div id="header">
  32.  
  33. <div id="menu">
  34.  
  35. <p id="m">&nbsp;</p>
  36. <p id="m1">&nbsp;</p>
  37. <p id="m2">&nbsp;</p>
  38. <p id="m3">&nbsp;</p>
  39. </div>
  40.  
  41. <div id="aaa">
  42.  
  43. <A HREF="java script:displayWindow('aaa.html',450,400)">
  44. <button style="color: red; font-size:16px; width:120px; height:35px; " name="button1"></button></a>
  45.  
  46.  
  47. </div>
  48. </div>
  49.  
  50.  
  51.  
  52.  
  53. <div id="nav">
  54. <div></div>
  55. </div>
  56. <div></div>
  57. <div class="twitter" id="newsletter_input">
  58. <p>&nbsp;</p>
  59. <p></p>
  60. </div>
  61. <div id="body">
  62. <div id="body-nner">
  63. <div id="body-left">
  64.  
  65. <div id="naglowek_aa">
  66.  
  67. <font color="#008BD2">Kontakt</font>
  68.  
  69. </div>
  70. <br>
  71.  
  72.  
  73. <form name="contactform" method="post" action="">
  74.  
  75. <table width="450px" align="center">
  76.  
  77. <tr>
  78.  
  79. <td valign="top">
  80.  
  81. <label for="first_name"><br>
  82. &nbsp;&nbsp;&nbsp;Imię *</label>
  83.  
  84. </td>
  85.  
  86. <td valign="top">
  87.  
  88. <br /> <input name="first_name" type="text" class="i1" size="30" maxlength="50">
  89.  
  90. </td>
  91.  
  92. </tr>
  93.  
  94. <tr>
  95.  
  96. <td height="26" valign="top">
  97.  
  98. <label for="last_name">&nbsp;&nbsp;&nbsp;Nazwisko *</label>
  99.  
  100. </td>
  101.  
  102. <td valign="top">
  103.  
  104. <input class="i1" type="text" name="last_name" maxlength="50" size="30">
  105.  
  106. </td>
  107.  
  108. </tr>
  109.  
  110. <tr>
  111.  
  112. <td valign="top">
  113.  
  114. <label for="email">&nbsp;&nbsp;&nbsp;Email Address *</label>
  115.  
  116. </td>
  117.  
  118. <td valign="top">
  119.  
  120. <input class="i1" type="text" name="email" maxlength="80" size="30">
  121.  
  122. </td>
  123.  
  124. </tr>
  125.  
  126. <tr>
  127.  
  128. <td valign="top">
  129.  
  130. <label for="telephone">&nbsp;&nbsp;&nbsp;Numer telefonu</label>
  131.  
  132. </td>
  133.  
  134. <td valign="top">
  135.  
  136. <input class="i1" type="text" name="telephone" maxlength="30" size="30">
  137.  
  138. </td>
  139.  
  140. </tr>
  141.  
  142. <tr>
  143.  
  144. <td valign="top">
  145.  
  146. <label for="comments">&nbsp;&nbsp;&nbsp;Zapytanie *</label>
  147.  
  148. </td>
  149.  
  150. <td valign="top">
  151.  
  152. <textarea class="i1" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  153.  
  154. </td>
  155.  
  156. </tr>
  157.  
  158. <tr>
  159.  
  160. <td colspan="2" style="text-align:center">
  161.  
  162. <input class="i2" type="submit" value="wyślij" style="width: 80px; height: 35px"></td>
  163.  
  164. </tr>
  165.  
  166. </table>
  167.  
  168. </form>
  169.  
  170.  
  171. <?php
  172.  
  173. function died($error) {
  174.  
  175. echo "<br />";
  176. echo $error."<br /><br />";
  177.  
  178. }
  179.  
  180. if(isset($_POST['email'])) {
  181. $email = $_POST['email'];
  182.  
  183.  
  184.  
  185. // EDIT THE 2 LINES BELOW AS REQUIRED
  186.  
  187. $email_to = "aa@wp.pl";
  188.  
  189. $email_subject = "Your email subject line";
  190.  
  191.  
  192.  
  193. // validation expected data exists
  194.  
  195. if(!isset($_POST['first_name']) ||
  196.  
  197. !isset($_POST['last_name']) ||
  198.  
  199. !isset($_POST['email']) ||
  200.  
  201. !isset($_POST['telephone']) ||
  202.  
  203. !isset($_POST['comments'])){
  204.  
  205. died('We are sorry, but there appears to be a problem with the form you submitted.');
  206.  
  207. }
  208.  
  209.  
  210.  
  211. $first_name = $_POST['first_name']; // required
  212.  
  213. $last_name = $_POST['last_name']; // required
  214.  
  215. $email_from = $_POST['email']; // required
  216.  
  217. $telephone = $_POST['telephone']; // not required
  218.  
  219. $comments = $_POST['comments']; // required
  220.  
  221.  
  222.  
  223.  
  224. $error_message = "";
  225.  
  226. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  227.  
  228. if(!preg_match($email_exp,$email_from)) {
  229.  
  230. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  231.  
  232. }
  233.  
  234. $string_exp = "/^[A-Za-z .'-]+$/";
  235.  
  236. if(!preg_match($string_exp,$first_name)) {
  237.  
  238. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  239.  
  240. }
  241.  
  242. if(!preg_match($string_exp,$last_name)) {
  243.  
  244. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  245.  
  246. }
  247.  
  248. if(strlen($comments) < 2) {
  249.  
  250. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  251.  
  252. }
  253.  
  254. if(strlen($error_message) > 0) {
  255.  
  256. died($error_message);
  257. }
  258. if(strlen($error_message) == 0) {
  259. echo "<br />";
  260. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  261. }
  262.  
  263.  
  264. $email_message = "Form details below.\n\n";
  265.  
  266.  
  267.  
  268.  
  269. function clean_string($string) {
  270.  
  271. $bad = array("content-type","bcc:","to:","cc:","href");
  272.  
  273. return str_replace($bad,"",$string);
  274.  
  275. }
  276.  
  277.  
  278.  
  279. $email_message .= "First Name: ".clean_string($first_name)."\n";
  280.  
  281. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  282.  
  283. $email_message .= "Email: ".clean_string($email_from)."\n";
  284.  
  285. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  286.  
  287. $email_message .= "Comments: ".clean_string($comments)."\n";
  288.  
  289.  
  290.  
  291.  
  292.  
  293. // create email headers
  294.  
  295. $headers = 'From: '.$email_from."\r\n".
  296.  
  297. 'Reply-To: '.$email_from."\r\n" .
  298.  
  299. 'X-Mailer: PHP/' . phpversion();
  300.  
  301. mail($email_to, $email_subject, $email_message, $headers);
  302.  
  303. ?>
  304.  
  305.  
  306.  
  307. <!-- include your own success html here -->
  308.  
  309.  
  310.  
  311.  
  312. <?php
  313.  
  314. }
  315.  
  316. ?>
  317.  
  318.  
  319.  
  320.  
  321.  
  322. <br>
  323.  
  324. <center>
  325. </center>
  326.  
  327.  
  328.  
  329.  
  330. </div>
  331. <div class="clear"></div>
  332. </div>
  333. <div id="copyright">
  334. <div id="copyright-left"> </div>
  335. </div>
  336. <div class="clear">&nbsp;</div>
  337. </div>
  338. </div>
  339. </div>
  340.  
  341.  
  342. <div id="pasek_dolny">
  343. <div id="copyright">
  344. <div id="copyright-left"></div>
  345. </div>
  346. <div id="kontakt"></div>
  347. </div>
  348. </body>
  349. </html>
  350.  
Go to the top of the page
+Quote Post
IProSoft
post
Post #9





Grupa: Zarejestrowani
Postów: 479
Pomógł: 97
Dołączył: 6.09.2011
Skąd: php.net :)

Ostrzeżenie: (0%)
-----


Ponieważ mimo, że formularz ma błędy, pozwalasz na wysłanie..

  1.  
  2. <?php
  3.  
  4. function died($error) {
  5.  
  6. echo "<br />";
  7. echo $error."<br /><br />";
  8.  
  9. }
  10.  
  11. if(isset($_POST['email'])) {
  12. $email = $_POST['email'];
  13.  
  14.  
  15.  
  16. // EDIT THE 2 LINES BELOW AS REQUIRED
  17.  
  18. $email_to = "aa@wp.pl";
  19.  
  20. $email_subject = "Your email subject line";
  21.  
  22.  
  23.  
  24. // validation expected data exists
  25.  
  26. if(!isset($_POST['first_name']) ||
  27.  
  28. !isset($_POST['last_name']) ||
  29.  
  30. !isset($_POST['email']) ||
  31.  
  32. !isset($_POST['telephone']) ||
  33.  
  34. !isset($_POST['comments'])){
  35.  
  36. died('We are sorry, but there appears to be a problem with the form you submitted.');
  37.  
  38. }
  39. else
  40. {
  41.  
  42.  
  43.  
  44. $first_name = $_POST['first_name']; // required
  45.  
  46. $last_name = $_POST['last_name']; // required
  47.  
  48. $email_from = $_POST['email']; // required
  49.  
  50. $telephone = $_POST['telephone']; // not required
  51.  
  52. $comments = $_POST['comments']; // required
  53.  
  54.  
  55.  
  56.  
  57. $error_message = "";
  58.  
  59. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  60.  
  61. if(!preg_match($email_exp,$email_from)) {
  62.  
  63. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  64.  
  65. }
  66.  
  67. $string_exp = "/^[A-Za-z .'-]+$/";
  68.  
  69. if(!preg_match($string_exp,$first_name)) {
  70.  
  71. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  72.  
  73. }
  74.  
  75. if(!preg_match($string_exp,$last_name)) {
  76.  
  77. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  78.  
  79. }
  80.  
  81. if(strlen($comments) < 2) {
  82.  
  83. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  84.  
  85. }
  86.  
  87. if(strlen($error_message) > 0) {
  88.  
  89. died($error_message);
  90. }
  91. else
  92. {
  93.  
  94.  
  95. $email_message = "Form details below.\n\n";
  96.  
  97.  
  98.  
  99.  
  100. function clean_string($string) {
  101.  
  102. $bad = array("content-type","bcc:","to:","cc:","href");
  103.  
  104. return str_replace($bad,"",$string);
  105.  
  106. }
  107.  
  108.  
  109.  
  110. $email_message .= "First Name: ".clean_string($first_name)."\n";
  111.  
  112. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  113.  
  114. $email_message .= "Email: ".clean_string($email_from)."\n";
  115.  
  116. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  117.  
  118. $email_message .= "Comments: ".clean_string($comments)."\n";
  119.  
  120.  
  121.  
  122.  
  123.  
  124. // create email headers
  125.  
  126. $headers = 'From: '.$email_from."\r\n".
  127.  
  128. 'Reply-To: '.$email_from."\r\n" .
  129.  
  130. 'X-Mailer: PHP/' . phpversion();
  131.  
  132. mail($email_to, $email_subject, $email_message, $headers);
  133.  
  134. echo "<br />";
  135. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  136. }
  137.  
  138. }
  139.  
  140. ?>
  141.  
  142.  
  143.  
  144. <!-- include your own success html here -->
  145.  
  146.  
  147.  
  148.  
  149. <?php
  150.  
  151. }
  152.  
  153. ?>
  154.  
  155.  
  156.  
Go to the top of the page
+Quote Post
-Gość-
post
Post #10





Goście







Dziękuję bardzo teraz działa prawidłowo
Go to the top of the page
+Quote Post
-kk-
post
Post #11





Goście







Chciałem jeszcze dodać możliwość wysyłania plików, plik uploduje się na serwer ale się nie wysyła. w jaki sposób wysłać plik razem z wiadomością (IMG:style_emoticons/default/questionmark.gif) a po wysłaniu usunąć z serwera (IMG:style_emoticons/default/questionmark.gif)

  1. <form name="contactform" method="post" action="" enctype="multipart/form-data">
  2. <input type=file name='myFile'>


  1. <?php
  2.  
  3. function died($error) {
  4.  
  5. echo "<br />";
  6. echo $error."<br /><br />";
  7.  
  8. }
  9.  
  10. if(isset($_POST['email'])) {
  11. $email = $_POST['email'];
  12.  
  13.  
  14.  
  15. // EDIT THE 2 LINES BELOW AS REQUIRED
  16.  
  17. $email_to = "aa@wp.pl";
  18.  
  19. $email_subject = "Your email subject line";
  20.  
  21.  
  22. $myFile = $_FILES['myFile'];
  23. $save_path = dirname( __FILE__ ) . "/" . $myFile['name'];
  24. copy( $myFile['tmp_name'], $save_path );
  25.  
  26.  
  27.  
  28. // validation expected data exists
  29.  
  30. if(!isset($_POST['first_name']) ||
  31.  
  32. !isset($_POST['last_name']) ||
  33.  
  34. !isset($_POST['email']) ||
  35.  
  36. !isset($_POST['telephone']) ||
  37.  
  38. !isset($_POST['comments'])){
  39.  
  40. died('We are sorry, but there appears to be a problem with the form you submitted.');
  41.  
  42. }
  43. else
  44. {
  45.  
  46.  
  47.  
  48. $first_name = $_POST['first_name']; // required
  49.  
  50. $last_name = $_POST['last_name']; // required
  51.  
  52. $email_from = $_POST['email']; // required
  53.  
  54. $telephone = $_POST['telephone']; // not required
  55.  
  56. $comments = $_POST['comments']; // required
  57.  
  58.  
  59.  
  60.  
  61. $error_message = "";
  62.  
  63. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  64.  
  65. if(!preg_match($email_exp,$email_from)) {
  66.  
  67. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  68.  
  69. }
  70.  
  71. $string_exp = "/^[A-Za-z .'-]+$/";
  72.  
  73. if(!preg_match($string_exp,$first_name)) {
  74.  
  75. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  76.  
  77. }
  78.  
  79. if(!preg_match($string_exp,$last_name)) {
  80.  
  81. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  82.  
  83. }
  84.  
  85. if(strlen($comments) < 2) {
  86.  
  87. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  88.  
  89. }
  90.  
  91. if(strlen($error_message) > 0) {
  92.  
  93. died($error_message);
  94. }
  95. else
  96. {
  97.  
  98.  
  99. $email_message = "Form details below.\n\n";
  100.  
  101.  
  102.  
  103.  
  104. function clean_string($string) {
  105.  
  106. $bad = array("content-type","bcc:","to:","cc:","href");
  107.  
  108. return str_replace($bad,"",$string);
  109.  
  110. }
  111.  
  112.  
  113.  
  114. $email_message .= "First Name: ".clean_string($first_name)."\n";
  115.  
  116. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  117.  
  118. $email_message .= "Email: ".clean_string($email_from)."\n";
  119.  
  120. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  121.  
  122. $email_message .= "Comments: ".clean_string($comments)."\n";
  123.  
  124.  
  125.  
  126.  
  127.  
  128. // create email headers
  129.  
  130. $headers = 'From: '.$email_from."\r\n".
  131.  
  132. 'Reply-To: '.$email_from."\r\n" .
  133.  
  134. 'X-Mailer: PHP/' . phpversion();
  135.  
  136. mail($email_to, $email_subject, $email_message, $headers);
  137.  
  138. echo "<br />";
  139. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  140. }
  141.  
  142. }
  143.  
  144. ?>
  145.  
  146.  
  147.  
  148. <!-- include your own success html here -->
  149.  
  150.  
  151.  
  152.  
  153. <?php
  154.  
  155. }
  156.  
  157. ?>
  158.  
  159.  
  160.  
Go to the top of the page
+Quote Post
-kk-
post
Post #12





Goście







Użyłem phpmailer, załączniki są dostarczane. W jaki sposób przerobić kod żeby cała wiadomość razem z załącznikiem była wysyłana przez phpmailer. W tej chwili otrzymuję dwie wiadomości jedna z załącznikiem, druga z treścią formularza jak to połączyć (IMG:style_emoticons/default/questionmark.gif)

  1.  
  2. <input class="i2" type="submit" value="wyślij" style="width: 80px; height: 35px">
  3.  
  4. <input type="file" name='myFile'>
  5. <input type="hidden" name="MAX_FILE_SIZE" value="5200000" />



  1. <?php
  2.  
  3. function died($error) {
  4.  
  5. echo "<br />";
  6. echo $error."<br /><br />";
  7.  
  8. }
  9.  
  10. if(isset($_POST['email'])) {
  11. $email = $_POST['email'];
  12.  
  13.  
  14.  
  15. // EDIT THE 2 LINES BELOW AS REQUIRED
  16.  
  17. $email_to = "aaa@wp.pl";
  18.  
  19. $email_subject = "Your email subject line";
  20.  
  21.  
  22. require_once($_SERVER['DOCUMENT_ROOT'].'/b/lib/phpmailer/class.phpmailer.php');
  23.  
  24. $mail = new PHPMailer();
  25.  
  26. $mail->PluginDir = "/b/";
  27. $mail->From = "aaa@wp.pl"; //adres naszego konta
  28. $mail->FromName = "naglowek wiadomosci";//nagłówek From
  29. $mail->Host = "smtp.wp.pl";//adres serwera SMTP
  30. $mail->Mailer = "smtp";
  31. $mail->Username = "aaa@wp.pl";//nazwa użytkownika
  32. $mail->Password = "xxxxx";//nasze hasło do konta SMTP
  33. $mail->SMTPAuth = true;
  34. $mail->SetLanguage("pl", "./b/lib/phpmailer/language/");
  35.  
  36. $mail -> SMTPSecure = true;
  37. $mail -> Port = 465;
  38.  
  39. $mail->Subject = "Mail testowy";//temat maila
  40.  
  41. // w zmienną $text_body wpisujemy treść maila
  42. $text_body = "Cześć, chyba phpMailer działa \n\n";
  43. $text_body .= "Na zawsze Twój, \n";
  44. $text_body .= "PHPMailer";
  45.  
  46. $mail->Body = $text_body;
  47. // adresatów dodajemy poprzez metode 'AddAddress'
  48. $mail->AddAddress("aaa@wp.pl","Kolega");
  49.  
  50.  
  51. $myFile = $_FILES['myFile'];
  52.  
  53. $save_path = dirname( __FILE__ ) . "/" . $myFile['name'];
  54.  
  55. copy( $myFile['tmp_name'], $save_path );
  56.  
  57.  
  58.  
  59. $mail->AddAttachment(@$_FILES['myFile']['name'],@$_FILES['myFile']['name']);
  60.  
  61. if(!$mail->Send())
  62. echo "There has been a mail error <br>";
  63. echo $mail->ErrorInfo."<br>";
  64.  
  65. // Clear all addresses and attachments
  66. $mail->ClearAddresses();
  67. $mail->ClearAttachments();
  68. echo "mail sent <br>";
  69.  
  70.  
  71. // validation expected data exists
  72.  
  73. if(!isset($_POST['first_name']) ||
  74.  
  75. !isset($_POST['last_name']) ||
  76.  
  77. !isset($_POST['email']) ||
  78.  
  79. !isset($_POST['telephone']) ||
  80.  
  81. !isset($_POST['comments'])){
  82.  
  83. died('We are sorry, but there appears to be a problem with the form you submitted.');
  84.  
  85. }
  86. else
  87. {
  88.  
  89.  
  90.  
  91. $first_name = $_POST['first_name']; // required
  92.  
  93. $last_name = $_POST['last_name']; // required
  94.  
  95. $email_from = $_POST['email']; // required
  96.  
  97. $telephone = $_POST['telephone']; // not required
  98.  
  99. $comments = $_POST['comments']; // required
  100.  
  101.  
  102.  
  103.  
  104. $error_message = "";
  105.  
  106. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  107.  
  108. if(!preg_match($email_exp,$email_from)) {
  109.  
  110. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  111.  
  112. }
  113.  
  114. $string_exp = "/^[A-Za-z .'-]+$/";
  115.  
  116. if(!preg_match($string_exp,$first_name)) {
  117.  
  118. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  119.  
  120. }
  121.  
  122. if(!preg_match($string_exp,$last_name)) {
  123.  
  124. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  125.  
  126. }
  127.  
  128. if(strlen($comments) < 2) {
  129.  
  130. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  131.  
  132. }
  133.  
  134. if(strlen($error_message) > 0) {
  135.  
  136. died($error_message);
  137. }
  138. else
  139. {
  140.  
  141.  
  142. $email_message = "Form details below.\n\n";
  143.  
  144.  
  145.  
  146.  
  147. function clean_string($string) {
  148.  
  149. $bad = array("content-type","bcc:","to:","cc:","href");
  150.  
  151. return str_replace($bad,"",$string);
  152.  
  153. }
  154.  
  155.  
  156.  
  157. $email_message .= "First Name: ".clean_string($first_name)."\n";
  158.  
  159. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  160.  
  161. $email_message .= "Email: ".clean_string($email_from)."\n";
  162.  
  163. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  164.  
  165. $email_message .= "File: ".clean_string($_FILES['myFile']['name'])."\n";
  166.  
  167. $email_message .= "Comments: ".clean_string($comments)."\n";
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // create email headers
  174.  
  175. $headers = 'From: '.$email_from."\r\n".
  176.  
  177. 'Reply-To: '.$email_from."\r\n" .
  178.  
  179. 'X-Mailer: PHP/' . phpversion();
  180.  
  181. mail($email_to, $email_subject, $email_message, $headers);
  182.  
  183. echo "<br />";
  184. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  185. }
  186.  
  187. }
  188.  
  189. ?>
  190.  
  191.  
  192.  
  193. <!-- include your own success html here -->
  194.  
  195.  
  196.  
  197.  
  198. <?php
  199.  
  200. }
  201.  
  202. ?>
Go to the top of the page
+Quote Post
Turson
post
Post #13





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Bo wysyłasz 2 razy. Raz phpmailerem a raz mail()
Go to the top of the page
+Quote Post
-kk-
post
Post #14





Goście







dlaczego wyskakuje błąd po ustawieniu
  1. $mail->From = $_POST['email'];

jak wpiszę swojego maila to go dostaje wiadomość ale wtedy nie mam malia odbiorcy z formularza.
Usunąłem mail(), jak przypisać sprawdzanie poszczególnych pół formularza do phpmailer (IMG:style_emoticons/default/questionmark.gif)


  1. require_once($_SERVER['DOCUMENT_ROOT'].'/b/lib/phpmailer/class.phpmailer.php');
  2.  
  3. $mail = new PHPMailer();
  4.  
  5. $mail->SetLanguage("pl");
  6. $mail->PluginDir = "/b/";
  7. $mail->From = $_POST['email'];
  8. $mail->FromName = $_POST['first_name']." ".$_POST['last_name'];
  9. $mail->Host = "smtp.wp.pl";//adres serwera SMTP
  10. $mail->Mailer = "smtp";
  11. $mail->Username = "aaa@wp.pl";//nazwa użytkownika
  12. $mail->Password = "xxxx";//nasze hasło do konta SMTP
  13. $mail->SMTPAuth = true;
  14. $mail->SetLanguage("pl", "./b/lib/phpmailer/language/");
  15.  
  16. $mail -> SMTPSecure = true;
  17. $mail -> Port = 465;
  18.  
  19. $mail->Subject = "Mail testowy";//temat maila
  20.  
  21. // w zmienną $text_body wpisujemy treść maila
  22.  
  23.  
  24. $text_body = "Od: ".$_POST['email']."\nfirst_name: ".$_POST['first_name']."\nlast_name: ".$_POST['last_name']."\nfile: ".$_FILES['myFile']['name']."\ntelephone: ".$_POST['telephone']."\ncomments: ".$_POST['comments'];
  25.  
  26. $mail->Body = $text_body;
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. // adresatów dodajemy poprzez metode 'AddAddress'
  34. $mail->AddAddress("aaa@wp.pl","Kolega");
  35.  
  36.  
  37. $myFile = $_FILES['myFile'];
  38.  
  39. $save_path = dirname( __FILE__ ) . "/" . $myFile['name'];
  40.  
  41. copy( $myFile['tmp_name'], $save_path );
  42.  
  43.  
  44.  
  45. $mail->AddAttachment(@$_FILES['myFile']['name'],@$_FILES['myFile']['name']);
  46.  
  47. if(!$mail->Send())
  48. echo "There has been a mail error <br>";
  49. echo $mail->ErrorInfo."<br>";
  50.  
  51. // Clear all addresses and attachments
  52. $mail->ClearAddresses();
  53. $mail->ClearAttachments();
  54. echo "mail sent <br>";
Go to the top of the page
+Quote Post
-kk-
post
Post #15





Goście







Co jest błędnego w takim zapisie (IMG:style_emoticons/default/questionmark.gif)

  1. $mail->From = $_POST['email'];


Chciałbym widzieć maila odbiorcy jak dostaję wiadomość. Proszę o pomoc.
Go to the top of the page
+Quote Post
-kk-
post
Post #16





Goście







Ok poradziłem sobie z widocznością maila z formularza. Ale mam jeszcze jeden problem. Po wciśnięciu wyślij kasowane są wszystkie wprowadzone dane do formularza, chcą je zachować zrobiłem tak
  1. <input name="first_name" type="text" class="i1" size="30" maxlength="50" value="<?=$_POST['first_name']?>">

Tylko że w ten sposób nawet po poprawnym wysłaniu dane pozostają w polach formularza. Jak po poprawnym wysłaniu formularza wyczyścić go ?
Go to the top of the page
+Quote Post
-kk-
post
Post #17





Goście







Nikt nie podpowie jak to osiągnąć (IMG:style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
johny_s
post
Post #18





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

Ostrzeżenie: (0%)
-----


dodaj sobie warunek żeby nie wyświetlało danych jeśli formularz został poprawnie obsłużony, ew. dobrą praktyką jest przeładowanie strony aby unikać ponownego wysyłania danych jeśli użytkownik odświeży stronę
Go to the top of the page
+Quote Post
-kk-
post
Post #19





Goście







Próbuję coś takiego, ale to chyba nie do końca jest dobry kierunek (IMG:style_emoticons/default/questionmark.gif)

  1. if(!$mail->Send()) {
  2.  
  3. echo "There has been a mail error <br>";
  4. echo $mail->ErrorInfo."<br>";
  5. }
  6.  
  7. if(empty($error_message)){
  8.  
  9. $_POST = "";
  10. }
Go to the top of the page
+Quote Post
-kk-
post
Post #20





Goście







Gdzie jest błąd takiego zapisu ? jak mail zostanie wysłany wyczyść POST, czyli rozumiem, że powinien wyczyścić formularz po wysłaniu ?

  1. if(!$mail->Send()) {
  2. $_POST = array();
  3.  
  4. echo "There has been a mail error <br>";
  5. echo $mail->ErrorInfo."<br>";
  6. }
Go to the top of the page
+Quote Post
Turson
post
Post #21





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Ale jak odświeżysz stronę to formularz przesle się ponownie
Go to the top of the page
+Quote Post
-kk-
post
Post #22





Goście







czy teraz będzie zabezpieczone przed odświeżaniem ? niestety formularz nie jest czyszczony po wysłaniu.

  1. if(!$mail->Send()) {
  2. $_POST = array();
  3.  
  4. echo "There has been a mail error <br>";
  5. echo $mail->ErrorInfo."<br>";
  6. }
  7.  
  8. died($error_message);
  9.  
  10. header('Location: <a href="http://www.twojadomena.pl&#39%3b%29;" target="_blank">http://www.twojadomena.pl');</a>
Go to the top of the page
+Quote Post
Turson
post
Post #23





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Jeżeli died oznacza po prostu przerwanie wykonywanie skryptu, to przekierowanie nie nastąpi nigdy. Ale koncepcja dobra
Go to the top of the page
+Quote Post
-kk-
post
Post #24





Goście







Dziękuję, zabezpieczenie działa. Jak rozwiązać problem czyszczenia formularza po wysłaniu ?

  1. if(!$mail->Send()) {
  2. $_POST = array();
  3.  
  4.  
  5.  
  6. echo "There has been a mail error <br>";
  7. echo $mail->ErrorInfo."<br>";
  8. }
  9. header('Location: <a href="http://www.twojadomena.pl&#39%3b%29;" target="_blank">http://www.twojadomena.pl');</a>
  10. died($error_message);
Go to the top of the page
+Quote Post
-kk-
post
Post #25





Goście







Chyba wszystko działa, dziękuję za pomoc.
Go to the top of the page
+Quote Post
-kk-
post
Post #26





Goście







Jeszcze jednak jest mały mankament, a mianowicie teraz nie wyświetla komunikatu o poprawnym wysłaniu maila. Jak po przekierowaniu wyświetlić komunikat o wysłaniu wiadomości ?

  1. if(!$mail->Send()) {
  2. $_POST = array();
  3. echo "There has been a mail error <br>";
  4. echo $mail->ErrorInfo."<br>";
  5. }
  6. header('Location: <a href="http://www.akt.c0.pl/b/kontakt.php&#39%3b%29;" target="_blank">http://www.akt.c0.pl/b/kontakt.php');</a>
  7. // Clear all addresses and attachments
  8. $mail->ClearAddresses();
  9. $mail->ClearAttachments();
  10.  
  11. echo "<br />";
  12. echo '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
Go to the top of the page
+Quote Post
nospor
post
Post #27





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Zapisz komunikat do sesji, na stronie na ktorą przekierowales sprawdzaj czy jest w sesji. Jesli jest, do go wyswietl i skasuj z sesji
Go to the top of the page
+Quote Post
-kk-
post
Post #28





Goście







coś w tym stylu ?, wszystko jest na jednej stronie i przekierowanie jest na tą samą stronę

zapisuję do sesji
  1.  
  2. if(!isset($_SESSION['komunikat']))
  3. {
  4. $_SESSION['komunikat'] = '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  5. }


odczytuję i kasuję
  1. echo $_SESSION['komunikat'];
Go to the top of the page
+Quote Post
Turson
post
Post #29





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


  1. if(isset($_SESSION['komunikat'])){
  2. echo $_SESSION['komunikat'];
  3. }
  4. else{
  5. // pokaż formularz
  6. }
Go to the top of the page
+Quote Post
nospor
post
Post #30





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Tak, cos w tym stylu
Go to the top of the page
+Quote Post
-kk-
post
Post #31





Goście







Nie wiem za bardzo czy źle wstawiłem kod sesji, czy złym miejscu. mogę prosić jeszcze o jakąś podpowiedź?

cały formularz
  1. <?php
  2.  
  3. function died($error) {
  4.  
  5.  
  6. echo $error."<br /><br />";
  7.  
  8. }
  9.  
  10. // validation expected data exists
  11.  
  12. if(!isset($_POST['first_name']) ||
  13.  
  14. !isset($_POST['last_name']) ||
  15.  
  16. !isset($_POST['email']) ||
  17.  
  18. !isset($_POST['telephone']) ||
  19.  
  20. !isset($_POST['comments'])){
  21.  
  22. died('');
  23.  
  24. }
  25. else
  26. {
  27.  
  28.  
  29.  
  30. $first_name = $_POST['first_name']; // required
  31.  
  32. $last_name = $_POST['last_name']; // required
  33.  
  34. $email_from = $_POST['email']; // required
  35.  
  36. $telephone = $_POST['telephone']; // not required
  37.  
  38. $comments = $_POST['comments']; // required
  39.  
  40.  
  41.  
  42.  
  43.  
  44. $error_message = "";
  45.  
  46. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  47.  
  48. if(!preg_match($email_exp,$email_from)) {
  49.  
  50. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Email Address you entered does not appear to be valid.<br /></span> ';
  51.  
  52. }
  53.  
  54.  
  55. $string_exp = "/^[a-ząćęłńóśżźĄĆĘŁŃÓŚŻŹ \']+$/";
  56.  
  57. if(!preg_match($string_exp,$first_name)) {
  58.  
  59. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The First Name you entered does not appear to be valid.<br /></span> ';
  60.  
  61. }
  62.  
  63. if(!preg_match($string_exp,$last_name)) {
  64.  
  65. $error_message .='<span style="font-size: 10px; color:#FF0000;">The Last Name you entered does not appear to be valid.<br /></span> ';
  66.  
  67. }
  68.  
  69. if ($telephone == "" || !preg_match("/^[0-9]{9,11}$/i", $telephone)) {
  70.  
  71. $error_message .='<span style="font-size: 10px; color: #FF0000;">Musisz podać swój telefon<br /></span> ';
  72. }
  73.  
  74.  
  75. if(strlen($comments) < 2) {
  76.  
  77. $error_message .= '<span style="font-size: 10px; color:#FF0000;">The Comments you entered do not appear to be valid.<br /></span> ';
  78.  
  79. }
  80.  
  81. if(strlen($error_message) > 0) {
  82.  
  83. died($error_message);
  84. }
  85. else
  86. {
  87.  
  88.  
  89. $email_message = "Form details below.\n\n";
  90.  
  91.  
  92.  
  93.  
  94. function clean_string($string) {
  95.  
  96. $bad = array("content-type","bcc:","to:","cc:","href");
  97.  
  98. return str_replace($bad,"",$string);
  99.  
  100. }
  101.  
  102.  
  103.  
  104. $email_message .= "First Name: ".clean_string($first_name)."\n";
  105.  
  106. $email_message .= "Last Name: ".clean_string($last_name)."\n";
  107.  
  108. $email_message .= "Email: ".clean_string($email_from)."\n";
  109.  
  110. $email_message .= "Telephone: ".clean_string($telephone)."\n";
  111.  
  112. $email_message .= "File: ".clean_string($_FILES['myFile']['name'].", ".$_FILES['myFile1']['name'])."\n";
  113.  
  114. $email_message .= "Comments: ".clean_string($comments)."\n";
  115.  
  116.  
  117.  
  118. if(!isset($_SESSION['komunikat']))
  119. {
  120. $_SESSION['komunikat'] = '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  121. }
  122.  
  123.  
  124. require_once($_SERVER['DOCUMENT_ROOT'].'/b/lib/phpmailer/class.phpmailer.php');
  125.  
  126. $mail = new PHPMailer();
  127.  
  128. $mail->PluginDir = "/b/";
  129. $mail->IsMail();
  130. $mail->From = stripslashes($_POST['email']); //adres naszego konta
  131. $mail->FromName = $_POST['first_name']." ".$_POST['last_name']." ".$_POST['email'] ; //nagłówek From
  132. $mail->CharSet = "UTF-8";
  133. $mail->SetLanguage("pl", "./b/lib/phpmailer/language/");
  134.  
  135.  
  136.  
  137. $mail->Subject = "Mail testowy";//temat maila
  138. $mail->Body = $email_message;
  139. // adresatów dodajemy poprzez metode 'AddAddress'
  140. $mail->AddAddress("aa@wp.pl","Kolega");
  141.  
  142. $mail->AddAttachment(@$_FILES['myFile']['tmp_name'],@$_FILES['myFile']['name']);
  143. $mail->AddAttachment(@$_FILES['myFile1']['tmp_name'],@$_FILES['myFile1']['name']);
  144.  
  145. if(!$mail->Send()) {
  146. $_POST = array();
  147.  
  148.  
  149.  
  150. echo "There has been a mail error <br>";
  151. echo $mail->ErrorInfo."<br>";
  152. }
  153. header('Location: <a href="http://www.kontakt.php&#39%3b%29;" target="_blank">http://www.kontakt.php');</a>
  154.  
  155. if(isset($_SESSION['komunikat'])){
  156. echo $_SESSION['komunikat'];
  157. }
  158. else{
  159. // pokaż formularz
  160. }
  161.  
  162. died($error_message);
  163.  
  164. // Clear all addresses and attachments
  165. $mail->ClearAddresses();
  166. $mail->ClearAttachments();
  167.  
  168. }
  169.  
  170. ?>
  171. <!-- include your own success html here -->
  172. <?php
  173. }
  174. ?>
Go to the top of the page
+Quote Post
-kk-
post
Post #32





Goście







Co zrobić aby po odświeżeniu pozostawał komunikat o wysłaniu maila ?

  1. if(!$mail->Send()) {
  2. $_POST = array();
  3.  
  4. echo $mail->ErrorInfo."<br>";
  5. }
  6.  
  7.  
  8. if(!isset($_SESSION['komunikat']))
  9. {
  10. $_SESSION['komunikat'] = '<span style="color:#009900; font-size: 10px;">Thank you for contacting us. We will be in touch with you very soon.</span> ';
  11. }
  12. header('refresh: 1;');
  13.  
  14. // Clear all addresses and attachments
  15. $mail->ClearAddresses();
  16. $mail->ClearAttachments();
  17.  
  18. }
  19.  
  20. if(isset($_SESSION['komunikat'])){
  21. echo $_SESSION['komunikat'];
  22. }
  23. else{
  24. // pokaż formularz
  25. }
Go to the top of the page
+Quote Post
-byervie has-
post
Post #33





Goście







Nie trzymac go w sesji.
Go to the top of the page
+Quote Post
-kk-
post
Post #34





Goście







sesja miała właśnie służyć do wyświetlenia komunikatu, jak ją podtrzymać po odświeżeniu ?
Go to the top of the page
+Quote Post
-gosc-
post
Post #35





Goście







to nie usuwaj tej sesji session_destroyem() moze pomoze
Go to the top of the page
+Quote Post
-kk-
post
Post #36





Goście







nie pomogło
Go to the top of the page
+Quote Post
-kk-
post
Post #37





Goście







W jaki inny sposób wyświetlić komunikat po odświeżeniu strony ?
Go to the top of the page
+Quote Post
-kk-
post
Post #38





Goście







Próbuje cały czas rozwiązać problem wyświetlania komunikatu.
Na początku skryptu php dodaję
  1. $_SESSION['message'] = 'Wiadomość została wysłana pomyślnie';

po drodze mam walidację formularza, wysyłam za pomocą phpmailer. Mam na końcu warunek do wysyłki
  1. if(!$mail->Send())
  2. { echo $mail->ErrorInfo."<br>"; }
  3. else { header('refresh: 0;');
  4. exit();
  5. echo $_SESSION['message'];
  6. }


Jeśli błąd to komunikat Errorinfo, jeśli powodzenie to czyszczę nagłówki i wczytuję sesję.
  1. if(isset($_SESSION['message'])){
  2. echo "<br />";
  3. echo "<span style=\"color:green\">{$_SESSION['message']}</span>";
  4. unset($_SESSION['message']);
  5. }

Na końcu sposób wyświetlania.
Coś jednak sknociłem bo czyści wszystko i nie pokazuje komunikatu. Mogę prosić o podpowiedź co zrobiłem źle?
Go to the top of the page
+Quote Post
Turson
post
Post #39





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


  1. exit();
  2. echo $_SESSION['message'];

zła kolejność

zamiast
unset($_SESSION['message']);
daj
$_SESSION['message'] = null;

i zamiast
if(isset($_SESSION['message'])){
daj
if(isset($_SESSION['message']) && (!empty($_SESSION['message'])){

Ten post edytował Turson 14.06.2014, 16:16:28
Go to the top of the page
+Quote Post
-kk-
post
Post #40





Goście







teraz komunikat się pojawia ale znika po odświeżeniu.
  1. if(!$mail->Send())
  2. {
  3. echo $mail->ErrorInfo."<br>";
  4. }
  5. else
  6. {
  7. header('refresh: 0;');
  8. echo $_SESSION['message'];
  9. exit();
  10. }
  11. if (isset($_SESSION['message'])&&(!empty($_SESSION['message']))){
  12. echo "<br />";
  13. echo "<span style=\"color:green\">{$_SESSION['message']}</span>";
  14. $_SESSION['message'] = null;
  15. }
Go to the top of the page
+Quote Post
Turson
post
Post #41





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Chyba ma zniknąć przecież po wyświetleniu się, co nie?

Nie rozumiem tej części
  1. header('refresh: 0;');
  2. echo $_SESSION['message'];
  3. exit();

co to ma niby robić tak naprawdę?

Jeżeli chcesz zabezpieczyć stronę przed odświeżeniem przez przekierowanie, a po nim wyświetlić komunikat to powinno być tak:
- zapisujesz komunikat w sesji
- odświeżasz stronę/przekierowujesz na tę samą
- sprawdzasz czy jest komunikat w sesji i jak jest to wyświetlasz go i usuwasz z sesji
Go to the top of the page
+Quote Post
-kk-
post
Post #42





Goście







Tak, chcę wyświetlić komunikat po odświeżeniu. Zapisuję do sesji. Jeśli mail jest wysyłany odświeżam, następne sprawdzam czy jest komunikat w sesji i wyświetlam sesję. Tylko sesja się nie wyświetla.
  1. if(!$mail->Send())
  2. {
  3. echo $mail->ErrorInfo."<br>";
  4. }
  5. else
  6. {
  7. header('refresh: 0;');
  8. exit();
  9. }
  10.  
  11. if (isset($_SESSION['message'])){
  12. echo "<br />";
  13. echo "<span style=\"color:green\">{$_SESSION['message']}</span>";
  14. $_SESSION['message'] = null;
  15. }
  16. }
Go to the top of the page
+Quote Post
Turson
post
Post #43





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


  1. if(!$mail->Send())
  2. {
  3. echo $mail->ErrorInfo."<br>";
  4. }
  5. else
  6. {
  7. $_SESSION['message'] = 'Message';
  8. header('refresh: 0;');
  9. exit();
  10. }
  11.  
  12. if (isset($_SESSION['message']))
  13. {
  14. echo "<br />";
  15. echo "<span style=\"color:green\">{$_SESSION['message']}</span>";
  16. }
  17. }
Go to the top of the page
+Quote Post
-kk-
post
Post #44





Goście







naprawdę wielkie dzięki za pomoc ale nadal nie wyświetla komunikatu
Go to the top of the page
+Quote Post
Turson
post
Post #45





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Daj na samym początku kodu
  1. var_dump($_SESSION);

sprawdź rezultat tego wtedy gdy komunikat powinien istnieć
Go to the top of the page
+Quote Post
-kk-
post
Post #46





Goście







jak wstawię na początku skryptu to pokazuje null cały czas,
jak po session_start(); daję var_dump($_SESSION); to pokazuje array(1) { ["message"]=> string(7) "Message" }
Go to the top of the page
+Quote Post
Turson
post
Post #47





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Aaaa session_start(); powinno być na samym początku nie objęte w żadne warunki (IMG:style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post
-kk-
post
Post #48





Goście







niestety efekt jest jak poprzednio
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 27.09.2025 - 21:36