Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Problem z wyświetlaniem błędów formularza
Fsh
post
Post #1





Grupa: Zarejestrowani
Postów: 31
Pomógł: 0
Dołączył: 6.05.2007

Ostrzeżenie: (40%)
XX---


Witam wszystkich serdecznie,

Chciałbym żeby w moim formularzu były wyświetlane błędy w sytuacji jeśli użytkownik nie wpisze np. e-maila. Próbowałem już wielu sposób, ale żaden nie działa i jestem w kropce. Proszę o pomoc.

  1. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
  2. <div class="field"><label for="email">Email:</label><input id="email" name="email" type="text" class="area"/></div>
  3. <div class="field"><label for="name">Imię i Nazwisko:</label><input id="name" name="name" type="text" class="area"/></div>
  4. <div class="field"><label for="name">Przedmiot:</label><input id="name" name="przedmiot" type="text" class="area"/></div>
  5. <div class="field">
  6. <label for="school">Wybierz szkołę:</label>
  7. <select id="school" name="school" class="area2"/>
  8. <option value="podstawowa">Podstawowa</option>
  9. <option value="gimnazjum">Gimnazjum</option>
  10. <option value="srednia">Średnia</option>
  11. <option value="wyzsza">Wyższa</option>
  12. </select>
  13. </div>
  14. <div class="field"><label for="message">Treść:</label><textarea cols="41" rows="5" name="message"></textarea></div>
  15. <div class="field"><label for="tele">Załącznik:</label><input id="tele" name="filename" type="file" /></div>
  16. <div class="field">
  17. <label for="verif_box">Przepisz kod:</label>
  18. <div class="captcha_img"><img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" />
  19. </div>
  20. <?php if(isset($_GET['wrong_code'])){?>
  21. <div style="border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;">Wrong verification code</div><br />
  22. <?php ;}?><input name="verif_box" type="text" id="verif_box" class="captcha_field"/></div>
  23. <input id="sendme" type="submit" value="" />
  24. </form>


  1. <?php
  2. if ($_SERVER['REQUEST_METHOD']=="POST"){
  3. $verif_box = $_REQUEST["verif_box"];
  4. // Set the "To" email address
  5. $to="#";
  6.  
  7. //Subject of the mail
  8. $subject="Zadanie domowe";
  9.  
  10. // Get the sender's name and email address plug them a variable to be used later
  11. $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email'])."<".stripslashes($_POST['przedmiot'])."<".stripslashes($_POST['school']).">";
  12.  
  13. // Check for empty fields
  14. if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']))
  15. {
  16. $errors .= "\n Error: Wszystkie pola są wymagane";
  17. }
  18.  
  19. // Get all the values from input
  20. $name = $_POST['name'];
  21. $przedmiot = $_POST['przedmiot'];
  22. $school = $_POST['school'];
  23. $email_address = $_POST['email'];
  24. $message = $_POST['message'];
  25.  
  26. // Check the email address
  27. if (!eregi( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
  28. {
  29. $errors .= "\n Error: Zły adres e-mail!";
  30. }
  31.  
  32. // Now Generate a random string to be used as the boundary marker
  33. $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  34.  
  35. // Now Store the file information to a variables for easier access
  36. $tmp_name = $_FILES['filename']['tmp_name'];
  37. $type = $_FILES['filename']['type'];
  38. $file_name = $_FILES['filename']['name'];
  39. $size = $_FILES['filename']['size'];
  40.  
  41. // Now here we setting up the message of the mail
  42. $message = "\n\n Imię i Nazwisko: $name \n\n Przedmiot: $przedmiot \n\n Szkoła: $school \n\n E-Mail: $email_address \n\nWiadomość: \n\n $message \n\nPlik: $file_name";
  43.  
  44. // Check if the upload succeded, the file will exist
  45. if (file_exists($tmp_name)){
  46.  
  47. // Check to make sure that it is an uploaded file and not a system file
  48. if(is_uploaded_file($tmp_name)){
  49.  
  50. // Now Open the file for a binary read
  51. $file = fopen($tmp_name,'rb');
  52.  
  53. // Now read the file content into a variable
  54. $data = fread($file,filesize($tmp_name));
  55.  
  56. // close the file
  57. fclose($file);
  58.  
  59. // Now we need to encode it and split it into acceptable length lines
  60. $data = chunk_split(base64_encode($data));
  61. }
  62.  
  63. // Now we'll build the message headers
  64. $headers = "From: $from\r\n" .
  65. "MIME-Version: 1.0\r\n" .
  66. "Content-Type: multipart/mixed;\r\n" .
  67. " boundary=\"{$mime_boundary}\"";
  68.  
  69. // Next, we'll build the message body note that we insert two dashes in front of the MIME boundary when we use it
  70. $message = "This is a multi-part message in MIME format.\n\n" .
  71. "--{$mime_boundary}\n" .
  72. "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  73. "Content-Transfer-Encoding: 7bit\n\n" .
  74. $message . "\n\n";
  75.  
  76. // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
  77. $message .= "--{$mime_boundary}\n" .
  78. "Content-Type: {$type};\n" .
  79. " name=\"{$file_name}\"\n" .
  80. //"Content-Disposition: attachment;\n" .
  81. //" filename=\"{$fileatt_name}\"\n" .
  82. "Content-Transfer-Encoding: base64\n\n" .
  83. $data . "\n\n" .
  84. "--{$mime_boundary}--\n";
  85.  
  86. // Thats all.. Now we need to send this mail... :)
  87.  
  88.  
  89. if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
  90. (@mail($to, $subject, $message, $headers))
  91. ?>
  92. <div><center>Zgłoszenie zostało wysłane!</center></div>
  93. <?php
  94. }else
  95. {
  96. ?>
  97. <div><center>
  98. Wykryto błędy, spróbuj jeszcze raz.</center></div>
  99. <?php
  100. }
  101. }
  102. }
  103. ?>
Go to the top of the page
+Quote Post
in5ane
post
Post #2





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


Udziel dodatkowych informacji. Masz jakieś błędy? Czy chcesz to wyświetlić przy formularzu (nad?, pod?, obok konkretnego pola) czy na osobnej podstronie?
Go to the top of the page
+Quote Post
Fsh
post
Post #3





Grupa: Zarejestrowani
Postów: 31
Pomógł: 0
Dołączył: 6.05.2007

Ostrzeżenie: (40%)
XX---


Generalnie najlepiej by było jeśli błąd występowałby w danym impucie, ale nie musi tak być, może również pojawiać się na dole pod formularzem na zasadzie, że jeśli ktoś nie wpisze swojego e-maila to zostanie wyświetlona informacja pod formularzem np. "Wprowadź swój e-mail!", może to być również hurtowa wiadomość tak jak jest to w kodzie:

Kod
/ Check for empty fields
   if(empty($_POST['name'])  || empty($_POST['email']) || empty($_POST['message']))
    {
        $errors .= "\n Error: Wszystkie pola są wymagane";
    }


...z tym, że nie działa...

Dodam, że jeśli wypełni się wszystkie pola łącznie z załącznikiem to wyświetla się informacja o wysłaniu, jednak jeśli nie wypełni się nic to również żaden tekst się nie wyświetli.

Ten post edytował Fsh 16.04.2014, 21:25:44
Go to the top of the page
+Quote Post
Turson
post
Post #4





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

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


Zainteresuj się obsługą wyjątków try catch, to znacznie ułatwi sprawę.
Go to the top of the page
+Quote Post
Geston
post
Post #5





Grupa: Zarejestrowani
Postów: 60
Pomógł: 9
Dołączył: 29.11.2010
Skąd: T

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


Ewentualnie żeby odciążyć serwer niepotrzebnymi odwołaniami do sprawdzania, nie zastąpić, ale dodatkowo dać sprawdzanie bądź w js bądź skorzystać z nowych atrybutów dla inputów w html jak <input type="email" (...) required />. Wtedy ograniczysz wysyłanie błędnych danych na serwer do ich niepotrzebnego przerabiania, aczkolwiek - po stronie serwera ponowne sprawdzenie też jest obowiązkowe, tylko że wtedy - kiedy ktoś ominie walidację po stronie klienta - nie musisz poczuwać się do obowiązku jakiegoś ładnego informowania go o błędzie pod formularzem.
Go to the top of the page
+Quote Post
nospor
post
Post #6





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




Cytat
Dodam, że jeśli wypełni się wszystkie pola łącznie z załącznikiem to wyświetla się informacja o wysłaniu, jednak jeśli nie wypełni się nic to również żaden tekst się nie wyświetli.

Bo nigdzie zmiennej $errors nie wyswietlasz wiec niby czemu oczekujesz ze sie wyswietli? Samo z siebie sie nie wyswietli
Go to the top of the page
+Quote Post
Turson
post
Post #7





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

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


Skąd wziąłeś $errors['email']...

Już wspomniałem wcześniej o try catch, to najlepsze wyjscie

Ten post edytował Turson 17.04.2014, 16:36:28
Go to the top of the page
+Quote Post

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: 23.12.2025 - 23:34