简体   繁体   English

同一页面上的PHP表单验证显示得很早

[英]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. 最简单的方法是在HTML中添加一个隐藏字段,然后测试该字段是否存在并具有预期值。 I tend to use something like this... 我倾向于使用这样的东西...

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

Then in the PHP you can do ... 然后在PHP中,您可以...

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. 同样,如果您的页面有多个表单,则可以使用action参数将它们分开并进行适当处理。

you can wrap your PHP code in: 您可以将PHP代码包装在:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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