Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
php formularz - wszystkie pola muszą być wypełnione
#1
Witam wszystkich!

Mam problem z formularzem - mianowicie chodzi o to że chciałbym żeby wysyłka formularza była możliwa tylko po wypełnieniu wszystkich pól - żeby przy pozostawieniu któregoś z pól wolnego i kliknięciu wyślij pojawiał się komunikat że nie wypełniono wszystkich pól.


plik processContactForm.php
Kod PHP:
<?php
if (count($_POST))
{
        
////////// USTAWIENIA //////////
        
$email '[email protected]';      // Adres e-mail adresata
        
$subject 'temat';     // Temat listu
        
$message 'Dziękujemy za wysłanie formularza';       // Komunikat
        
$error 'Wystąpił błąd podczas wysyłania formularza';     // Komunikat błędu
        
$charset 'iso-8859-2';        // Strona kodowa
        //////////////////////////////
        
        
$head =
                
"MIME-Version: 1.0\r\n" .
                
"Content-Type: text/plain; charset=$charset\r\n" .
                
"Content-Transfer-Encoding: 8bit";
        
$body '';
        foreach (
$_POST as $name => $value)
        {
                if (
is_array($value))
                {
                        for (
$i 0$i count($value); $i++)
                        {
                                
$body .= "$name=" . (get_magic_quotes_gpc() ? stripslashes($value[$i]) : $value[$i]) . "\r\n";
                        }
                }
                else 
$body .= "$name=" . (get_magic_quotes_gpc() ? stripslashes($value) : $value) . "\r\n";
        }
        echo 
mail($email"=?$charset?B?" base64_encode($subject) . "?="$body$head) ? $message $error;

  


}


?>



plik index.php
Kod PHP:
<?php
include('processContactForm.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl"> 
<head> 

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script>
$(document).ready(function() {
        $('*').click(function() {
                if($("[name='regulamin']").is(':checked')) {
                        $("#wyslij").removeAttr('disabled');
                        
                } else {
                        $("#wyslij").attr('disabled', 'disabled');
                }
        });
});
</script>
<script type="text/javascript">

  String.prototype.trim = function() {
          return this.replace(/^\s+|\s+$/g, "");
  };
  
  function verifyContactData()
  {
        var form = document.getElementById('contactForm');
        var msg = "";
        var msgCount = 0;
  
        var value = form.imie_i_nazwisko.value;
        value = value.trim(); 
        if (value.length == 0) 
        {
          msg += "\nImię";
          msgCount++;
        }   
        value = form.telefon.value;
        value = value.trim(); 
        if (value.length == 0)
        {
          msg += "\nTelefon";
          msgCount++;
        }   
        value = form.email.value;
        value = value.trim();
        if (value.length == 0)
        {
          msg += "\nE-mail";
          msgCount++
        }   
        value = form.data_slubu.value;
        value = value.trim();
        if (value.length == 0)
        {
          msg += "\nData ślubu";
          msgCount++
        }   
        value = form.miejscowosc.value;
        value = value.trim();
        if (value.length == 0)
        {
          msg += "\nMiejscowość";
          msgCount++;
        }   
        value = form.miejsce_przyjecia.value;
        value = value.trim();
        if (value.length == 0)
        {
          msg += "\nMiejsce przyjęcia";
          msgCount++
        }   
  
        if (msgCount == 1)
        {
          alert("Wypełnij pole: " + msg);
          return false;
        }  
        if (msgCount > 1)
        {
          alert("Wypełnij pola: " + msg);
          return false;
        }  
  
        // ---------------------
        
        var value = form.imie_i_nazwisko.value;
        var result = value.match(/[0-9]+/);
        if (result != null)
           {
           alert("Wprowadzono cyfry w polu Imię i nazwisko!");
           return false;
           }
  
        value = form.telefon.value;
        result = value.match(/[a-zA-Z]+/);
        if (result != null)
           {
           alert("Wprowadzono litery w polu Telefon!");
           return false;
           }
        
        // sprawdzenie czy ktos nie wypelnil telefonu jedna i ta sama cyfra   
        result = value.match(/[0-9]{1}/);
        if (result != null)
        {
          var regex = new RegExp("[^-"+result+"]+");
          if (!regex.test(value))
           {
           alert("Błędny telefon.");
           return false;
           }
        }    
  
        value = form.email.value;
        result = value.match(/.+@.+\..+/);
        if (result == null)
           {
           alert("Błędy format adresu e-mail!");
           return false;
           }
  
        return true;
  }
    
</script>
<title>willbe</title> 
<meta name="description" content="" /> 
<meta name="keywords" content="" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="revisit-after" content="30 days" /> 
<meta http-equiv="content-language" content="pl" /> 
<meta name="Author" content="willbe" /> 
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head> 
<body>
        <!-- HEADER START -->
        <div id="whole">
        <div id="left" float="right"></div>
        <div id="right" float="right">
                <div id="gora"><img src="gora.jpg" /></div>
                <div id="dol">
                <div id="formularz">
                        <form action="processContactForm.php" method="post">
                        <table id="contact" cellpading="15" cellspacing="15" width="500px">
                        <tr>
                                <td>Imie</td>
                            <td><input type="text" name="imie"/></td>
                        </tr>
                        <tr>
                                <td>Nazwisko</td>
                            <td><input type="text" name="nazwisko"/></td>
                         </tr>
                         <tr>
                                <td>E-mail</td>
                            <td><input type="text" name="e-mail"/></td>
                         </tr>
                         <tr>
                                <td>Pesel</td>
                            <td><input type="text" name="pesel"/></td>
                         </tr>
                         <tr>
                                <td>Telefon</td>
                            <td><input type="text" name="telefon"/></td>
                         </tr>
                         <tr>
                                <td>Kod pocztowy</td>
                            <td><input type="text" name="kod_pocztowy"/></td>
                         </tr>
                         <tr>
                                <td>Kwota </td>
                            <td><input type="text" name="kwota"/></td>
                         </tr>
                         <tr>
                                <td align="left"><input type="checkbox" name="regulamin"/>
                           <span id="akc">Akceptuje warunki regulaminu</span></td>
                           <td></td>
                         </tr>
                        </table>
                        
                        <div id="buttons">
                        <span style="margin-right: 100px;">
                                <button type="submit" id="wyslij"></button>                             
                        </span>
                        </div>
                    </form>
                </div>
                </div>
        </div>
                <xdiv id="mxainx" float="right"><br/>
    </div>
 </body>
</html> 

ma ktoś jakiś pomysł?
Odpowiedz
#2
Najpierw "czymś porządnym" sprawdź, czy pola formularza nie przenoszą jakiegoś syfu i przypisz je do zmiennych, następnie sprawdź czy zmienne nie są puste. Jeżeli są to info i powrót, jeżeli są wypełnione to wysyłka.
Odpowiedz
#3
Może to ci pomorze? http://doman.art.pl/webmaster/walidacja-formularzy-html5-za-pomoca-js/
Bo ten skrypt który masz to właściwie trzeba przepisać od początku, a tutaj na forum są same lenie.
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  Jak zrobic Formularz Kontaktowy na stronie ? Kodarth 191 255,833 06-04-2021, 16:25
Ostatni post: zerin
  Walidacja zdjęć + formularz harvester2001 6 5,971 24-03-2014, 19:01
Ostatni post: harvester2001
  [PHP] Formularz mailowy - wysyłanie wiadomości spolprog 7 6,486 05-09-2013, 17:13
Ostatni post: Engine
  Pewny formularz and licznik JOzEk 1 2,345 04-05-2013, 14:35
Ostatni post: Engine
  Formularz dodający post suchar 4 3,484 04-08-2012, 14:09
Ostatni post: GeoAce5

Skocz do:


Użytkownicy przeglądający ten wątek: 1 gości
Sponsorzy i przyjaciele
SeoHost.pl