1

I'm working on a system that would echo diff forms in case there was an error. i made a form that contain all the fields and then I made 8 more forms for each one of the fields.(if he didn't fill the first field echo $form_1; if he didn't fill the second field echo $form_2 and etc but when I try and test my code it would show a blank page.

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>';   

                        ?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • white screen of death: error checking\display are off, turn them on to see the error. at the top of your php page add: `ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1);` –  Jun 26 '15 at 22:15
  • Still a white screen of death :( –  Jun 26 '15 at 22:19
  • Then check your webserver logs – Mark Baker Jun 26 '15 at 22:24
  • Man i also tried that ... nothing .... –  Jun 26 '15 at 22:25
  • hey your code works for me. can you tell is there any difference in all form variables. because by looking them it looks all are identical. also your both files are at same working location or not? – Alive to die - Anant Jun 26 '15 at 22:30
  • Hmm weird yes the codes are diff ... i added lines near the fields ( the error lines and etc) and yes both are in the same file. –  Jun 26 '15 at 22:44
  • `and yes both are in the same file.` what is it mean? they must be two different files at the same working location. – Alive to die - Anant Jun 26 '15 at 22:48
  • That what i ment ... –  Jun 26 '15 at 22:55
  • What if you remove all the ifs and else ifs and only `echo $form`? Do you get a white screen still? – Simon Germain Jun 26 '15 at 23:11
  • if i echo each form alone ($form_1 and $form_2 and $form_3) it works but when i try to run them in this code it would not work . –  Jun 26 '15 at 23:21
  • I also noticed that you're adding `

    ` tags inside a `

    ` tag. I don't think that would cause PHP to freak out, but that's invalid HTML and can cause unexpected effects.
    – Simon Germain Jun 26 '15 at 23:42

3 Answers3

0

Check your formatting. See the end of your script and how is the 1st IF statement closed/not closed. You are not closing the last [elseif] statement! Try this:

    <?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;
} 

?>
Milan
  • 3,209
  • 1
  • 35
  • 46
0

Try structuring your code a little better. Here's what I would propose you handle this:

Note that this is assuming you're using PHP 5.4+ and you have short tags on. If not, replace the <?= with <?php echo and it should work just fine.

<?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>
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
  • You ... You are a genius but pls tell me now that every thing work as a charm how i get to echo the fields that are already filled with the values (i.g username and email and verification email) don't want use to retype them every time an error accrued. –  Jun 26 '15 at 23:45
  • Well, This should work just fine. If you look inside the `
    ` tag, you'll see that I output the value of `$errors['']` in the third cell of your table. If it's set to something, like `$fieldIsRequired`, it will display your error message. If not, it won't display anything in the cell.
    – Simon Germain Jun 26 '15 at 23:48
  • sorry can you explain more ? what to add and where ? i want to add because in this case a php value like $ptveremail for example. –  Jun 27 '15 at 00:01
  • Where do you want to add it? – Simon Germain Jun 27 '15 at 00:42
  • Ah, I see... You can just set the `value=""` in your `` tag to set its value to your php variable. I'll update my answer. – Simon Germain Jun 27 '15 at 00:44
  • You .... You ... take a cookie :3 –  Jun 27 '15 at 07:57
0

OK then if you are trying to validate the form fields you may want to a) sanitize the input a bit b) simplify the code

You do not need 8 different forms, you just need one and echo the "missing field" variable value next to the field which fails to validate.

You may want to start with something like this (or simplify it even more and make a loop out of this):

<?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!'; } 

and then in your HTMLform use something like this:

 <td>Username : </td>
 <td><input type = "text" name = "ptuser"><font color = "red" ><?php echo $ptuserErr; ?></font></td>
Milan
  • 3,209
  • 1
  • 35
  • 46