简体   繁体   中英

PHP form validation on same page shows to early

What I'm trying to do is put the valadiation of a form on the same page. The problem is now that it already shows the errors if you visit the page. It has to be that the errors show after you fill in the form, not before.

This is my code:

<html>
<head>
  <title>Westpop</title>
  <link rel="stylesheet" href="opmaak.css">
</head>
<body>
    <fblack>
    <div id="header"><a href="Index.php"></a></div>

    <div id="registreer">
        <table cellpadding="5" align="center">
            <th colspan="2" align="left">Registeren</th>
            <form name="registreren"  method="post" action="registreer.php">
                <tr>
                    <td><fblackbold>Inloggegevens:<fblackbold></td>
                </tr>

                <tr>
                    <td>Email</td>
                    <td><input type="text" size="50" placeholder="email@provider.com" name="email"></td>
                </tr>
                <tr>
                    <td>Wachtwoord</td>
                    <td><input type="password" size="50" placeholder="min. 8 tekens, één hoofdletter, één cijfer" name="wachtwoord"></td>
                </tr>
                <tr>
                    <td><fblackbold>Persoonlijke gegevens:<fblackbold></td>
                </tr>
                <tr>
                    <td>Voornaam</td>
                    <td><input type="text" size="50" placeholder="" name="voornaam" ></td>
                </tr>
                <tr>
                    <td>Achternaam</td>
                    <td><input type="text" size="50" placeholder="" name="achternaam"></td>
                </tr>
                <tr>
                    <td>Geboorte Datum</td>
                    <td><input type="text" size="50" placeholder="jjjj-mm-dd" name="geboortedatum"></td>
                </tr>
                <tr>
                    <td>Geslacht</td>
                    <td>
                        M <input type="radio" size="50" value="m" name="geslacht">
                        V <input type="radio" size="50" value="v" name="geslacht">
                    </td>

                </tr>
                <tr>
                    <td>Adres</td>
                    <td><input type="text" size="50" placeholder="Straat 00" name="adres"></td>
                </tr>
                <tr>
                    <td>Woonplaats</td>
                    <td><input type="text" size="50" placeholder="" name="woonplaats"></td>
                </tr>

                <tr>
                    <td>Telefoonnummer</td>
                    <td><input type="text" size="50" placeholder="min. 9 tekens" name="telefoonnummer"></td>
                </tr>
                <tr>
                    <td>Functie</td>
                    <td>
                        <select name="functie">
                            <option value="catering">Catering</option>
                            <option value="muziekpodia">Muziek en podia</option>
                            <option value="vervoerovernachten">Vervoer en overnachten</option>
                            <option value="logistiekbeveiliging">Logistiek en beveiliging</option>
                            <option value="diversen">Diversen</option>
                        </select>
                    </td>
                </tr>
                <tr align="right">
                    <td></td>
                    <td><input type="reset" value="Wissen"><input type="submit" name="verzenden" value="Verzenden"></td>
                </tr>
            </form>
        </table>

        <?php 

            $host = "localhost";
            $gebruikersnaam = "root";
            $wachtwoord = "";
            mysql_connect($host, $gebruikersnaam, $wachtwoord);

            $demooistedatabase = "c5g4westpopintranet";
            mysql_select_db($demooistedatabase);

            $achternaam = $_POST["achternaam"];
            $voornaam = $_POST["voornaam"];
            $gbdatum = $_POST["geboortedatum"];
            $email = $_POST["email"];
            $geslacht = $_POST["geslacht"];
            $wachtwoord = $_POST["wachtwoord"];
            $woonplaats = $_POST["woonplaats"];
            $adres = $_POST["adres"];
            $telefoonnummer = $_POST["telefoonnummer"];
            $functie = $_POST["functie"];


            $achternaam = stripslashes($achternaam);
            $voornaam = stripslashes($voornaam);
            $gbdatum = stripslashes($gbdatum);
            $email = stripslashes($email);
            $geslacht = stripslashes($geslacht);
            $wachtwoord = stripslashes($wachtwoord);
            $woonplaats = stripslashes($woonplaats);
            $adres = stripslashes($adres);
            $telefoonnummer = stripslashes($telefoonnummer);
            $functie = stripslashes($functie);

            $query = "INSERT INTO 
                        vrijwilliger ( voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email )
                      VALUES
                        ( '$voornaam', '$achternaam', '$gbdatum', '$geslacht', '$wachtwoord', '$woonplaats', '$adres', '$telefoonnummer', '$functie', null, null, '$email' )";

            $foutloos=true;

            if( preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $wachtwoord) === 0 ) {
                echo '<fblack>Wachtwoord moet minstens 8 tekens lang zijn, een kleine letter, grote letter én cijfer bevatten.<br><fblack>';
                $foutloos = false; 
            }

            if( preg_match("/^[0-9]{10}+$/", $telefoonnummer) === 0 ) {
                echo '<fblack>Het telefoonnummer moet 10 cijfers bevatten.<br><fblack>';
                $foutloos = false; 
            }  

            if( preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/", $gbdatum) === 0 ) {
                echo '<fblack>Geboorte datum moet op dit formaat ingevoerd worden: JJJJ-MM-DD<br><fblack>';
                $foutloos = false; 
            }

            if( preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $email) === 0 )
            {
                echo '<fblack>Email moet hier op lijken: email@provider.com<br><fblack>';
                $foutloos = false; 
            }   

            if( preg_match("/^[A-Z][a-zA-Z -]+$/", $voornaam) === 0 ) {
                echo '<fblack>Voornaam is niet geldig ingevoerd, heeft u een hoofdletter gebruikt?<br><fblack>';
                $foutloos = false; 
            } 

            if( preg_match( "/^[A-Z][a-zA-Z -]+$/", $achternaam ) === 0 )
            {
                echo '<fblack>Achternaam is niet geldig ingevoerd, heeft u een hoofdletter gebruikt?<br><fblack>';
                $foutloos = false; 
            } 

            if( $geslacht == '' ) {
                echo '<fblack>U heeft uw geslacht niet aangegeven.<br><fblack>';
                $foutloos = false; 
            }

            if( preg_match( "/^[a-zA-Z]+\ +[0-9]+$/", $adres ) === 0 ) {
                echo '<fblack>Het adres is verkeerd ingevoerd.<br><fblack>';
                $foutloos = false; 
            }

            if( preg_match("/^[a-zA-Z\s]+$/", $woonplaats ) === 0) {
                echo '<fblack>De woonplaats is verkeerd ingevoerd.<br><fblack>';
                $foutloos = false; 
            }

            if( $foutloos == true ) {
                mysql_query($query) or die( '<fblack>U staat al in ons systeem<A HREF="javascript:javascript:history.go(-1)"><br><br>Klik hier om terug te gaan</A><fblack>' );
                echo "<fblack>Uw registratie is succesvol verwerkt!<br>Log <a href='login.php' MEDIA=screen>hier</a> in<fblack>";
            }
        ?>
    </div>
<?php
    include ("html_end.php");
?>

Simplest method is just to add a hidden field in the HTML, and then test to see if that field exists, and has the expected value. I tend to use something like this...

<input type="hidden" size="8" name="action" value="submitform" />

Then in the PHP you can do ...

if (isset($_POST['action']) && $_POST['action']=='submitform') {
   ... do form validation and submission ...
}

Using a hidden field is good because you don't need to worry about fields not appearing because they are empty/not set. For example, if your form only had a radio button and none of the options were set, that form parameter would simply not be passed by the browser. This way you don't have to worry about any issues like that.

Also if you ever have a page with more than one form, you can use the action parameter to tell them apart and process them appropriately.

you can wrap your PHP code in:

if( $_SERVER['REQUEST_METHOD'] == 'POST'){
  //handle form
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM