Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Problem z polami wymaganymi w formularzu.
adiscan
post
Post #1





Grupa: Zarejestrowani
Postów: 64
Pomógł: 0
Dołączył: 12.04.2007

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


Mam problem z polami wymaganymi! Tzn nie wiem jak zrobić żeby komunikat o nie wypełnionym polu na tej samej stronie i bez potrzeby wypełniania ponownie formularza.


Poniższy skrypt to robi ale ... widnieje cały czas napis o nie wypełnionym polu i jesli dam wyslij i nie wypełnie pola wymaganego to czyści mi cały formularz ... i nie umie sobie z tym poradzic

I nastepna sprawa to formatowanie tresci samej wiadomosci tzn zmiennej $message tak by były zachowane wszystkie znaki w sensie zeby ENTER był Enterem a nie tak jak jest teraz JEDEN WIELKI CIĄG znakow!

Bede wdzieczny za wszystkie rady.

Wszystko znajduje sie w jednym pliku test2.php tzn formularz w htmlu i mail() w php ...

Do pola wymaganego zastosowałem instr warunkową if ... else

Oj przepraszam za tak rozwalonego posta (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)

  1. <?php
  2. if($name=='')
  3. {
  4. echo $error;
  5. }
  6. else
  7. {
  8. wykonaj skrypt
  9. }
  10. ?>


  1. <html>
  2.  
  3. <head>
  4. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
  5. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  6. <meta name="ProgId" content="FrontPage.Editor.Document">
  7. <title>Formularz Kontaktowy</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <p align="center">
  13. <div align="right">
  14. <form action="http://www.tienspolska.com/ftp/test2.php" name="form" method="post">
  15.  <center>
  16. <table border="0" cellpadding="0" cellspacing="1" width="322" height="533" bgcolor="#EDFCE0" style="border: 0 double #008000">
  17. <tr>
  18. <td width="627" height="30" colspan="4" bordercolor="#00FFFF" bgcolor="#BCE184">
  19. <p align="center"><font color="#FFFFFF"><b>Formularz Kontaktowy</b></font> </td>
  20. </tr>
  21. <tr>
  22. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Imię i 
  23. Nazwisko:     </font> </td>
  24. <td width="759" colspan="3" height="40"> <input type="text" name="name" size="55"></td>
  25. </tr>
  26. <tr>
  27. <td width="193" height="25" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Ulica:</font></td>
  28. <td width="418" colspan="3" height="25"> <input type="text" name="ulica" size="55"></td>
  29. </tr>
  30. <tr>
  31. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Nr domu:</font></td>
  32. <td width="137" height="40"> <input type="text" name="numer" size="10"></td>
  33. <td width="198" height="40">
  34. <p align="center"><font face="Arial" size="2">Kod pocztowy:</font></td>
  35. </center>
  36. <td width="424" height="40">
  37. <p align="left"><input type="text" name="kod" size="14"></td>
  38. </tr>
  39. <center>
  40. <tr>
  41. <td width="193" height="26" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Miejscowość: </font> </td>
  42. <td width="759" colspan="3" height="26"> <input type="text" name="miejscowosc" size="55"></td>
  43. </tr>
  44. <tr>
  45. <td width="193" height="25" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Twoj Email:</font></td>
  46. <td width="759" colspan="3" height="25"> <input type="text" name="email" size="55"></td>
  47. </tr>
  48. <tr>
  49. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Jak mamy Ci
  50. odpowiedzieć?</font></td>
  51. <td width="759" colspan="3" height="40"> <select size="1" name="select">
  52. <option selected>E-mail zwrotny</option>
  53. <option>Telefonicznie                  
  54. </option>
  55. </select>   <font size="2" face="Arial">  nr tel.  </font><font size="1"> 
  56. </font><input type="text" name="tel" size="12"></td>
  57. </tr>
  58. <tr>
  59. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Temat
  60. wiadomoćci:             </font>
  61. </td>
  62. <td width="759" colspan="3" height="40"> <select size="1" name="subject">
  63. <option selected>Pytanie odnośnie produktów firmy Tiens</option>
  64. <option>Karta Klienta Firmy Tiens - informacje</option>
  65. <option>Zamówienie</option>
  66. <option>Reklamacje</option>
  67. <option>Inny</option>
  68.  
  69. </select></td>
  70. </tr>
  71. <tr>
  72. <td width="193" height="271" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Treść
  73. wiadomoćci:</font></td>
  74. <td width="759" colspan="3" height="271" style="border-top-style: double" bordercolor="#BCE184"><textarea rows="13" name="tresc" cols="50"></textarea></td>
  75. </tr>
  76. <tr>
  77. <td width="902" height="27" colspan="4" align="center" bgcolor="#BCE184">
  78. <input type="submit" value=" Wyślij  " style="border-style: double; border-color: #008000"></td>
  79. <input type="reset" value="reset">
  80. </tr>
  81. </table>
  82. </center>
  83. </div>
  84. <p align="left"> 
  85. <p align="left"> 
  86. <p align="left"><br>
  87. </p>
  88. </form>
  89.  
  90. </body>
  91.  
  92. </html>
  93.  
  94. <?php
  95. header("Content-type: text/html; charset=utf-8");
  96.  
  97.  
  98.  
  99. $name = $_POST['name'];
  100. $ulica = $_POST['ulica'];
  101. $numer = $_POST['numer'];
  102. $kod = $_POST['kod'];
  103. $miejscowosc = $_POST['miejscowosc'];
  104. $email = $_POST['email'];  //email tego kto ma pytanie
  105. $select = $_POST['select'];
  106. $tel = $_POST['tel'];
  107. $tresc = $_POST['tresc'];
  108. $error = "nie wszystkie pola są wypełnione";
  109.  
  110. /////////////////////////////////////////////////////////////
  111.  
  112.  
  113.  
  114.  
  115. if($name=='')
  116. {
  117.  
  118. echo $error;
  119.  
  120. }
  121. else
  122.  
  123. {
  124. //adresaci
  125. $do = " wiadomo ".",";
  126. $do .= " wiadomo ";
  127.  
  128. // temat maila
  129. $subject = $_POST['subject'];
  130.  
  131. // wiadomosc
  132.  
  133. $wiadomosc .= '
  134. <html>
  135. <head>
  136. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  137. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  138. <meta name="ProgId" content="FrontPage.Editor.Document">
  139. <title>Tytuł tej wiadomosci:</title>
  140. </head>
  141.  
  142. <body>
  143.  
  144.  
  145. <div align="left">
  146. <table border="0" cellpadding="0" cellspacing="1" width="528" height="158">
  147. <tr>
  148. <td width="155" height="20" align="left"><b>Imie i Nazwisko:</b></td>
  149. <td width="363" height="20" align="left">'.$name.'</td>
  150. </tr>
  151. <tr>
  152. <td width="155" height="21" align="left"><b>Adres:</b></td>
  153. <td align="left" width="359">'.$ulica.' '.$numer.'</td>
  154. </tr>
  155. <tr>
  156. <td width="155" height="21" align="left"></td>
  157. <td align="left" width="359">'.$kod.' '.$miejscowosc.'</td>
  158. </tr>
  159. <tr>
  160. <td width="155" height="19" align="left"><b>Kotnakt:</b></td>
  161. <td width="359" height="19" align="left">e-mail:  '.$email.'</td>
  162. </tr>
  163. <tr>
  164. <td width="155" height="21" align="left"></td>
  165. <td width="363" height="21" align="left">tel: '.$tel.'</td>
  166. </tr>
  167. <tr>
  168. <td width="155" height="21" align="left"><b>Forma odpowiedzi:</b></td>
  169. <td width="363" height="21" align="left">'.$select.'</td>
  170. </tr>
  171. <tr>
  172. <td width="155" height="21" align="left"><b><font color="#000080">Temat:</font></b></td>
  173. <td width="363" height="21" align="left"><font color="#000080"><b>'.$subject.'</b></font></td>
  174. </tr>
  175. <tr>
  176. <td width="522" height="21" colspan="2" align="left">
  177. <p align="left">--------------------------------------------------------------</td>
  178. </tr>
  179. <tr>
  180. <td width="522" height="21" colspan="2" align="left"><i>'.$tresc.'</i></td>
  181. </tr>
  182. </table>
  183. </div>
  184.  
  185. </body>
  186. </html>
  187.  
  188. ';
  189.  
  190.  
  191. //naglowki:
  192.  
  193. $naglowki = "MIME-Version: 1.0rn";
  194. $naglowki .= "Content-type: text/html; charset=utf-8rn";
  195.  
  196.  
  197. //dodatkowy naglowek
  198.  
  199. $naglowki .= "From: tienspolska.com <adiscann@wp.pl>rn";
  200. //$naglowki .= "Cc: wiadomo rn";
  201. //$naglowki .= "Bcc: wiadomo rn";
  202.  
  203.  
  204. mail ($do,$subject,$wiadomosc,$naglowki);
  205.  
  206. //print_r($_POST);
  207.  
  208. };
  209.  
  210. ?>


Ten post edytował adiscan 16.04.2007, 18:59:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
phpion
post
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Poniżej masz działającą walidację dla pól 'name' oraz 'ulica'. Ewentualne błędy wpisujesz do tablicy $errors po czym (w formularzu) wyświetlasz je (o ile są) za pomocą funkcji showError:
  1. <?php showError('name'); ?>

gdzie jako parametr funkcji podajesz index pola formularza będący równocześnie indexem w tablicy $errors.
  1. <?php
  2.  
  3. function showError($errorId) {
  4. global $errors;
  5.  
  6. if (isset($errors[$errorId])) {
  7. echo '<br /><span style="color: red;">'.$errors[$errorId].'</font>';
  8. }
  9. }
  10.  
  11. $errors = array();
  12.  
  13. if (isset($_POST['submit'])) { // jesli wyslano formularz
  14. if (strlen($_POST['name']) < 1) {
  15. $errors['name'] = 'Wpisz swoje imie';
  16. }
  17.  
  18. if (strlen($_POST['ulica']) < 1) {
  19. $errors['ulica'] = 'Wpisz ulice';
  20. }
  21.  
  22. // itd.
  23.  
  24. if (count($errors) == 0) { // wszystko OK - nie ma bledow
  25. //adresaci
  26. $do = " adiscann@wp.pl ".",";
  27. $do .= " adiscann@wp.pl ";
  28.  
  29. // temat maila
  30. $subject = $_POST['subject'];
  31.  
  32. // wiadomosc
  33.  
  34. $wiadomosc .= '
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  38. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  39. <meta name="ProgId" content="FrontPage.Editor.Document">
  40. <title>Tytuł tej wiadomosci:</title>
  41. </head>
  42.  
  43. <body>
  44.  
  45.  
  46. <div align="left">
  47. <table border="0" cellpadding="0" cellspacing="1" width="528" height="158">
  48. <tr>
  49. <td width="155" height="20" align="left"><b>Imie i Nazwisko:</b></td>
  50. <td width="363" height="20" align="left">'.$name.'</td>
  51. </tr>
  52. <tr>
  53. <td width="155" height="21" align="left"><b>Adres:</b></td>
  54. <td align="left" width="359">'.$ulica.' '.$numer.'</td>
  55. </tr>
  56. <tr>
  57. <td width="155" height="21" align="left"></td>
  58. <td align="left" width="359">'.$kod.' '.$miejscowosc.'</td>
  59. </tr>
  60. <tr>
  61. <td width="155" height="19" align="left"><b>Kotnakt:</b></td>
  62. <td width="359" height="19" align="left">e-mail: '.$email.'</td>
  63. </tr>
  64. <tr>
  65. <td width="155" height="21" align="left"></td>
  66. <td width="363" height="21" align="left">tel: '.$tel.'</td>
  67. </tr>
  68. <tr>
  69. <td width="155" height="21" align="left"><b>Forma odpowiedzi:</b></td>
  70. <td width="363" height="21" align="left">'.$select.'</td>
  71. </tr>
  72. <tr>
  73. <td width="155" height="21" align="left"><b><font color="#000080">Temat:</font></b></td>
  74. <td width="363" height="21" align="left"><font color="#000080"><b>'.$subject.'</b></font></td>
  75. </tr>
  76. <tr>
  77. <td width="522" height="21" colspan="2" align="left">
  78. <p align="left">--------------------------------------------------------------</td>
  79. </tr>
  80. <tr>
  81. <td width="522" height="21" colspan="2" align="left"><i>'.$tresc.'</i></td>
  82. </tr>
  83. </table>
  84. </div>
  85.  
  86. </body>
  87. </html>
  88.  
  89. ';
  90.  
  91.  
  92. //naglowki:
  93.  
  94. $naglowki = "MIME-Version: 1.0rn";
  95. $naglowki .= "Content-type: text/html; charset=utf-8rn";
  96.  
  97.  
  98. //dodatkowy naglowek
  99.  
  100. $naglowki .= "From: tienspolska.com <adiscann@wp.pl>rn";
  101. //$naglowki .= "Cc: wiadomo rn";
  102. //$naglowki .= "Bcc: wiadomo rn";
  103.  
  104.  
  105. mail ($do,$subject,$wiadomosc,$naglowki); // po tym mozesz przekierować na jakas stronke z info 'Dziekujemy za wyslanie maila'
  106. }
  107. }
  108. ?>
  109. <html>
  110. <head>
  111. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-2">
  112. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  113. <meta name="ProgId" content="FrontPage.Editor.Document">
  114. <title>Formularz Kontaktowy</title>
  115. </head>
  116.  
  117. <body>
  118.  
  119. <p align="center">
  120. <div align="right">
  121. <form action="" name="form" method="post">
  122.  <center>
  123. <table border="0" cellpadding="0" cellspacing="1" width="322" height="533" bgcolor="#EDFCE0" style="border: 0 double #008000">
  124. <tr>
  125. <td width="627" height="30" colspan="4" bordercolor="#00FFFF" bgcolor="#BCE184">
  126. <p align="center"><font color="#FFFFFF"><b>Formularz Kontaktowy</b></font> </td>
  127. </tr>
  128. <tr>
  129. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Imię i 
  130. Nazwisko:  </font> </td>
  131. <td width="759" colspan="3" height="40"> <input type="text" name="name" size="55" value="<?php echo $_POST['name']; ?>"><?php showError('name'); ?></td>
  132. </tr>
  133. <tr>
  134. <td width="193" height="25" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Ulica:</font></td>
  135. <td width="418" colspan="3" height="25"> <input type="text" name="ulica" value="<?php echo $_POST['ulica']; ?>" size="55"><?php showError('ulica'); ?></td>
  136. </tr>
  137. <tr>
  138. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Nr domu:</font></td>
  139. <td width="137" height="40"> <input type="text" name="numer" size="10" value="<?php echo $_POST['numer']; ?>"></td>
  140. <td width="198" height="40">
  141. <p align="center"><font face="Arial" size="2">Kod pocztowy:</font></td>
  142. </center>
  143. <td width="424" height="40">
  144. <p align="left"><input type="text" name="kod" size="14" value="<?php echo $_POST['kod']; ?>"></td>
  145. </tr>
  146. <center>
  147. <tr>
  148. <td width="193" height="26" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Miejscowość: </font> </td>
  149. <td width="759" colspan="3" height="26"> <input type="text" name="miejscowosc" value="<?php echo $_POST['miejscowosc']; ?>" size="55"></td>
  150. </tr>
  151. <tr>
  152. <td width="193" height="25" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Twoj Email:</font></td>
  153. <td width="759" colspan="3" height="25"> <input type="text" name="email" size="55" value="<?php echo $_POST['email']; ?>"></td>
  154. </tr>
  155. <tr>
  156. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Jak mamy Ci
  157. odpowiedzieć?</font></td>
  158. <td width="759" colspan="3" height="40"> <select size="1" name="select">
  159. <option selected>E-mail zwrotny</option>
  160. <option>Telefonicznie
  161. </option>
  162. </select>  <font size="2" face="Arial"> nr tel. </font><font size="1"> 
  163. </font><input type="text" name="tel" size="12"></td>
  164. </tr>
  165. <tr>
  166. <td width="193" height="40" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Temat
  167. wiadomoćci:  </font>
  168. </td>
  169. <td width="759" colspan="3" height="40"> <select size="1" name="subject">
  170. <option selected>Pytanie odnośnie produktów firmy Tiens</option>
  171. <option>Karta Klienta Firmy Tiens - informacje</option>
  172. <option>Zamówienie</option>
  173. <option>Reklamacje</option>
  174. <option>Inny</option>
  175.  
  176. </select></td>
  177. </tr>
  178. <tr>
  179. <td width="193" height="271" style="border-right-style: double" bordercolor="#BCE184" bordercolorlight="#EDFCE0" bordercolordark="#BCE184"><font size="2" face="Arial">Treść
  180. wiadomoćci:</font></td>
  181. <td width="759" colspan="3" height="271" style="border-top-style: double" bordercolor="#BCE184"><textarea rows="13" name="tresc" cols="50"><?php echo $_POST['tresc']; ?></textarea></td>
  182. </tr>
  183. <tr>
  184. <td width="902" height="27" colspan="4" align="center" bgcolor="#BCE184">
  185. <input type="submit" value=" Wyślij  " style="border-style: double; border-color: #008000" name="submit"></td>
  186. <input type="reset" value="reset">
  187. </tr>
  188. </table>
  189. </center>
  190. </div>
  191. <p align="left"> 
  192. <p align="left"> 
  193. <p align="left"><br>
  194. </p>
  195. </form>
  196.  
  197. </body>
  198.  
  199. </html>
Go to the top of the page
+Quote Post

Posty w temacie


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: 28.12.2025 - 02:21