Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Przedszkole _ [JavaScript][PHP]Formularz chyba nie przechodzi

Napisany przez: sadistic_son 14.03.2024, 15:12:48

Cześć, problem wydaje się banalny ale coś nie widzę błędu. W skrócie - mam formularz z dwoma polami. Po kliknięciu w submit javascript sprawdza czy wpisano poprawny email oraz czy zadane pytanie ma minimum 10 znaków. Jeśli nie to wyświetla error a jeśli tak to powinno przejść do wysłania formularza. No właśnie, powinno a chyba nie przechodzi, bo nie wykonuje się niz wewnątrz warunku sprawdzającego czy są dane z POST. Czy w js jest jakiś błąd? Dzięki.

  1. if(http://www.php.net/isset($_POST['ask_submit']) && !http://www.php.net/empty($_POST['ask_email']) && !http://www.php.net/empty($_POST['ask_question'])){
  2. http://www.php.net/echo '<h1>JEST</h1>';
  3. //ciąg dalszy itd.
  4. }

  1. <http://december.com/html/4/element/form.html id="ask_us" method="POST" name="ask_us" action="index.php">
  2. <http://december.com/html/4/element/label.html for="email">Twój adres email:</http://december.com/html/4/element/label.html>
  3. <http://december.com/html/4/element/input.html type="email" id="email" name="ask_email" required><http://december.com/html/4/element/br.html>
  4. <http://december.com/html/4/element/label.html for="question">Pytanie do nas:</http://december.com/html/4/element/label.html>
  5. <http://december.com/html/4/element/textarea.html id="question" name="ask_question" required></http://december.com/html/4/element/textarea.html><http://december.com/html/4/element/br.html>
  6. <http://december.com/html/4/element/input.html type="submit" value="Wyślij zapytanie" name="ask_submit" >
  7. </http://december.com/html/4/element/form.html>
  8. <http://december.com/html/4/element/div.html id="error-message"></http://december.com/html/4/element/div.html>
  9. <http://december.com/html/4/element/script.html>
  10. document.getElementById("ask_us").addEventListener("submit", function(event) {
  11. event.preventDefault();
  12. var email = document.getElementById("email").value;
  13. var question = document.getElementById("question").value;
  14. var errorMessage = "";
  15.  
  16. if (!validateEmail(email)) {
  17. errorMessage += "Wpisz poprawny adres email.<http://december.com/html/4/element/br.html>";
  18. }
  19.  
  20. if (question.length < 10) {
  21. errorMessage += "Zadaj pytanie składające się z co najmniej 10 znaków.<br>";
  22. }
  23.  
  24. if (errorMessage === "") {
  25. this.submit();
  26. } else {
  27. document.getElementById("error-message").innerHTML = errorMessage;
  28. }
  29. });
  30.  
  31. function validateEmail(email) {
  32. var re = /\S+@\S+\.\S+/;
  33. return re.test(email);
  34. }
  35. </http://december.com/html/4/element/script.html>



EDIT: Jeśli usunę if(isset($_POST['ask_submit'])) a zostawię resztę warunku, tj !empty(...) to wtedy działa. Co ten js wyprawia z tym submitem? Zabiera mu default behavior ale po sprawdzeniu poprawności danych oddaje, więc formularz powinien przejść prawda?

Napisany przez: nospor 14.03.2024, 17:12:25

nie
if (errorMessage === "") {
this.submit();
} else {
document.getElementById("error-message").innerHTML = errorMessage;
}
a
if (errorMessage === "") {
return true;
} else {
document.getElementById("error-message").innerHTML = errorMessage;
return false;
}

Napisany przez: sadistic_son 15.03.2024, 09:15:18

Elegancko. Dzięki.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)