没有错误+显示空白页PHP

时间:2015-06-26 22:12:57

标签: php

我正在开发一个系统,可以在出现错误时回显diff表单。 我制作了一个包含所有字段的表单,然后我为每个字段再制作了8个表单。(如果他没有填充第一个字段echo $ form_1;如果他没有填充第二个字段echo $ form_2和等等,但是当我尝试测试我的代码时,它会显示一个空白页面。

register.php:

<?php

require 'regforms.php';

$btsubmit = isset($_POST["btsubmit"]) ;

if ($btsubmit) { 

    $ptuser = $_POST["ptuser"] ;
    $ptemail = $_POST["ptemail"] ;
    $ptveremail = $_POST["ptveremail"] ;
    $ptpass = $_POST["ptpass"] ;
    $ptverpass = $_POST["ptverpass"] ;
    $ptsecquest = $_POST["ptsecquest"] ;
    $ptsecans = $_POST["ptsecans"] ;
    $cpantibot = isset($_POST['cpantibot']) ;


    if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && empty($ptsecans) && empty($cpantibot)) {

                        echo $form_1;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && empty($ptsecans) && $cpantibot) {

                        echo $form_2;   

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && $ptsecans && $cpantibot) {

                        echo $form_3;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && $ptsecquest && $ptsecans && $cpantibot) {

                        echo $form_4;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

                        echo $form_5;   

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

                        echo $form_6;           

    } else if (empty($ptuser) && empty($ptemail) && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

                        echo $form_7;       

    } else if (empty($ptuser) && $ptemail && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {


                        echo $form_8;       

    } else if ($ptuser && $ptemail && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo 'ok';
        echo $form;

    }

} else { 

    echo $form;

    } 
    ?>

regforms.php:

    <?php

$form = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';

            $form_1 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                                    <td><font color = "red" >Checking this box is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';

        $form_2 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';   


        $form_3 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans" value = "$ptsecans" ></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';       

        $form_4 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';       


        $form_5 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';       
$form_6 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';       

        $form_7 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail" value = "$ptveremail" ></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';       

            $form_8 = '<form action = "register.php" method = "POST">
                                <table>
                                        <h3>Basic information</h3>                  
                                            <tr>
                                                    <td>Username : </td>
                                                    <td><input type = "text" name = "ptuser"></td>
                                                    <td><font color = "red" >This field is required.</font></td>
                                            </tr>
                                            <tr>
                                                    <td>Email : </td>
                                                    <td><input type = "text" name = "ptemail" value = "$ptemail" ></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify email: </td>
                                                    <td><input type = "text" name = "ptveremail" value = "$ptveremail" ></td>
                                            </tr>
                                        </table>
                                        <table>
                                        <h3>Security information</h3>   
                                            <tr>
                                                    <td>Password : </td>
                                                    <td><input type = "password" name = "ptpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Verify password : </td>
                                                    <td><input type = "password" name = "ptverpass"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security question : </td>
                                                    <td><input type = "text" name = "ptsecquest"></td>
                                            </tr>
                                            <tr>
                                                    <td>Security answer: </td>
                                                    <td><input type = "text" name = "ptsecans"></td>
                                            </tr>
                                        </table>
                                    <table>
                                    <h3>Verification</h3>   
                                        <table>
                                            <tr>
                                                    <td></td>
                                                    <td><label><input type="checkbox" name="cpantibot" value="I_am_a_humin">Verify you are a humin</label></td>
                                            </tr>
                                            <tr>
                                                    <td></td>
                                                    <td><input type = "submit" name = "btsubmit" value = "Submit" ></td>
                                            </tr>
                                    </table>
                        </form>';   

                        ?>

3 个答案:

答案 0 :(得分:0)

检查您的格式。查看脚本的结尾以及第一个IF语句如何关闭/未关闭。你没有关闭最后一个[elseif]语句!试试这个:

    <?php

require 'regforms.php';

$btsubmit = isset($_POST["btsubmit"]) ;

if ($btsubmit) { 

    $ptuser = $_POST["ptuser"] ;
    $ptemail = $_POST["ptemail"] ;
    $ptveremail = $_POST["ptveremail"] ;
    $ptpass = $_POST["ptpass"] ;
    $ptverpass = $_POST["ptverpass"] ;
    $ptsecquest = $_POST["ptsecquest"] ;
    $ptsecans = $_POST["ptsecans"] ;
    $cpantibot = isset($_POST['cpantibot']) ;


    if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && empty($ptsecans) && empty($cpantibot)) {

        echo $form_1;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && empty($ptsecans) && $cpantibot) {

        echo $form_2;   

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && empty($ptsecquest) && $ptsecans && $cpantibot) {

        echo $form_3;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && empty($ptverpass) && $ptsecquest && $ptsecans && $cpantibot) {

        echo $form_4;

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && empty($ptpass) && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo $form_5;   

    } else if (empty($ptuser) && empty($ptemail) && empty($ptveremail) && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo $form_6;           

    } else if (empty($ptuser) && empty($ptemail) && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo $form_7;       

    } else if (empty($ptuser) && $ptemail && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo $form_8;       

    } else if ($ptuser && $ptemail && $ptveremail && $ptpass && $ptverpass && $ptsecquest && $ptsecans && $cpantibot) {

        echo 'ok';
        echo $form;

    } else {

        echo $form;
    }

} else { 

    echo $form;
} 

?>

答案 1 :(得分:0)

尝试更好地构建代码。以下是我建议你处理的事情:

请注意,这是假设您使用的是PHP 5.4+并且您有短标签。如果没有,请将<?=替换为<?php echo,它应该可以正常工作。

<?php

$btsubmit = isset($_POST["btsubmit"]) ;

$fieldIsRequired    = '<font color="red">This field is required.</font>';
$passwordMismatch   = '<font color="red">Passwords mismatch.</font>';
$emailMismatch      = '<font color="red">Emails don\'t match.</font>';

if ($btsubmit) { 

    $ptuser = $_POST["ptuser"] ;
    $ptemail = $_POST["ptemail"] ;
    $ptveremail = $_POST["ptveremail"] ;
    $ptpass = $_POST["ptpass"] ;
    $ptverpass = $_POST["ptverpass"] ;
    $ptsecquest = $_POST["ptsecquest"] ;
    $ptsecans = $_POST["ptsecans"] ;
    $cpantibot = isset($_POST['cpantibot']) ;

    $errors[] = array();

    if (empty($ptuser))         $errors['ptuser'] = $fieldIsRequired;
    if (empty($ptemail))        $errors['ptemail'] = $fieldIsRequired;
    if (empty($ptveremail))     $errors['ptveremail'] = $fieldIsRequired;
    if (empty($ptpass))         $errors['ptpass'] = $fieldIsRequired;
    if (empty($ptverpass))      $errors['ptverpass'] = $fieldIsRequired;
    if (empty($ptsecquest))     $errors['ptsecquest'] = $fieldIsRequired;
    if (empty($ptsecans))       $errors['ptsecans'] = $fieldIsRequired;
    if (!$cpantibot)            $errors['cpantibot'] = $fieldIsRequired;

    // handling special case
    if (!empty($ptpass) && !empty($ptverpass) && $ptpass != $ptverpass) {
        $errors['ptpass'] = $passwordMismatch;
    }

    if (!empty($ptemail) && !empty($ptveremail) && $ptemail != $ptveremail) {
        $errors['ptemail'] = $emailMismatch;
    }

    if (count($errors) == 0) {
        // No errors!
        echo '<h3>Form is all ok!</h3>';
    }
}
?>

<form action="register.php" method="POST">
    <table>
        <tr>
            <td colspan="3"><h3>Basic information</h3></td>
        </tr>
        <tr>
            <td>Username : </td>
            <td><input type="text" name="ptuser" value="<?= $ptuser ?>"></td>
            <td><?= $errors['ptuser'] ?></td>
        </tr>
        <tr>
            <td>Email : </td>
            <td><input type="text" name="ptemail" value="<?= $ptemail ?>"></td>
            <td><?= $errors['ptemail'] ?></td>
        </tr>
        <tr>
            <td>Verify email: </td>
            <td><input type="text" name="ptveremail" value="<?= $ptveremail ?>"></td>
            <td><?= $errors['ptveremail'] ?></td>
        </tr>
        <tr>
            <td colspan="3"><h3>Security information</h3></td>
        </tr>
        <tr>
            <td>Password : </td>
            <td><input type="password" name="ptpass"></td>
            <td><?= $errors['ptpass'] ?></td>
        </tr>
        <tr>
            <td>Verify password : </td>
            <td><input type="password" name="ptverpass"></td>
            <td><?= $errors['ptverpass'] ?></td>
        </tr>
        <tr>
            <td>Security question : </td>
            <td><input type="text" name="ptsecquest" value="<?= $ptsecquest ?>"></td>
            <td><?= $errors['ptsecquest'] ?></td>
        </tr>
        <tr>
            <td>Security answer: </td>
            <td><input type="text" name="ptsecans" value="<?= $ptsecans ?>"></td>
            <td><?= $errors['ptsecans'] ?></td>
        </tr>
        <tr>
            <td colspan="3"><h3>Verification</h3></td>
        </tr>
        <tr>
            <td></td>
            <td><label><input type="checkbox" checked="<?= $cpantibot ? 'checked' : '' ?>" name="cpantibot" value="I_am_a_human">Verify you are a human</label></td>
            <td><?= $errors['cpantibot'] ?></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" name="btsubmit" value="Submit" ></td>
            <td></td>
        </tr>
    </table>
</form>

答案 2 :(得分:0)

好的,如果您正在尝试验证您可能想要的表单字段 a)对输入进行消毒 b)简化代码

您不需要8种不同的表单,只需要一个表格并回显无法验证的字段旁边的“缺失字段”变量值。

你可能想要从这样的事情开始(或者更简化它并从中做出一个循环):

<?php

require 'regforms.php';

$btsubmit = $ptuser = $ptemail = $ptveremail = $ptpass = $ptverpass = $ptsecquest = $ptsecans = $cpantibot = '';

$btsubmit = isset($_POST['btsubmit']) ? filter_var($_POST['btsubmit'], FILTER_SANITIZE_STRING) : 'failed';

if (!empty($btsubmit) && $btsubmit != ' ' && $btsubmit != 'failed') { 

     $ptuser = isset($_POST['ptuser']) ? filter_var($_POST['ptuser'], FILTER_SANITIZE_STRING) : 'failed';
     $ptemail = isset($_POST['ptemail']) ? filter_var($_POST['ptemail'], FILTER_SANITIZE_EMAIL) : 'failed';
     $ptveremail = isset($_POST['ptveremail']) ? filter_var($_POST['ptveremail'], FILTER_SANITIZE_EMAIL) : 'failed';
     $ptpass = isset($_POST['ptpass']) ? filter_var($_POST['ptpass'], FILTER_SANITIZE_STRING) : 'failed';
     $ptverpass = isset($_POST['ptverpass']) ? filter_var($_POST['ptverpass'], FILTER_SANITIZE_STRING) : 'failed';
     $ptsecquest = isset($_POST['ptsecquest']) ? filter_var($_POST['ptsecquest'], FILTER_SANITIZE_STRING) : 'failed';
     $ptsecans = isset($_POST['ptsecans']) ? filter_var($_POST['ptsecans'], FILTER_SANITIZE_STRING) : 'failed';
     $cpantibot = isset($_POST['cpantibot']) ? filter_var($_POST['cpantibot'], FILTER_SANITIZE_STRING) : 'failed';


    if($ptuser == 'failed'){ $ptuserErr = 'Missing field!'; }
    if($ptemail == 'failed'){ $ptemailErr = 'Missing field!'; }  
    if($ptveremail == 'failed'){ $ptveremailErr = 'Missing field!'; }
    if($ptpass == 'failed'){ $ptpassErr = 'Missing field!'; } 
    if($ptverpass == 'failed'){ $ptverpassErr = 'Missing field!'; } 
    if($ptsecquest == 'failed'){ $ptsecquestErr = 'Missing field!'; } 
    if($ptsecans == 'failed'){  $ptsecansErr = 'Missing field!'; } 
    if($cpantibot == 'failed'){ $cpantibotErr = 'Missing field!'; } 

然后在HTMLform中使用以下内容:

 <td>Username : </td>
 <td><input type = "text" name = "ptuser"><font color = "red" ><?php echo $ptuserErr; ?></font></td>