samedi 25 juin 2016

how can I get the action attribute in form tag to work correctly?

I know the tag form has an attribute called action and I know how action works, this is how I used it

<form method="post" action = "registerd.php">
</form>

The registerd.php page also exists as well. Now I am doing some validation in the tag form using php validation, but why does it jump to the registerd.php page without validating the values at first? is there any other solutions to this rather than using the header at the end of php code like :

header("location : registerd.php")

here is the whole code I'm writing Thank you very much in advance

<div id = "content">
            <?php
                $fnameErr = "";
                $lnameErr = "";
                $idErr = "";
                $placeErr = "";
                $dateErr = "";
                $emailErr = "";
                $pswErr = "";
                $file;
                $content = "";
                function test_input($data) {
                    $data = trim($data);
                    $data = stripslashes($data);
                    $data = htmlspecialchars($data);
                    return $data;
                }
                if(isset($_POST['register'])){
                    if(isset($_POST['firstname'])){
                        if(!empty($_POST['firstname'])){
                            if(strlen($_POST['firstname'])>1){
                                if(preg_match("/[a-zA-Z]+/", $_POST['firstname'])==1){
                                    $firstname = test_input($_POST['firstname']);
                                    $content .= "firstname:";
                                    $content .= $firstname; 
                                    $content .= " ";
                                }
                                else{   
                                    $fnameErr = "your first name should only include letters";
                                }
                            }
                            else{
                                $fnameErr = "your first name must be at least 2 characters";
                            }
                        }
                        else{
                            $fnameErr = "enter your first name";
                        }
                    }
                    else{
                        $fnameErr = "enter your first name";
                    }
                    if(isset($_POST['lastname'])){
                        if(!empty($_POST['lastname'])){
                            if(strlen($_POST['lastname'])>3){
                                if(preg_match("/[a-zA-Z]+/", $_POST['lastname'])==1){
                                    $lastname = test_input($_POST['lastname']);
                                    $content .= "lastname:";
                                    $content .= $lastname;
                                    $content .= " ";
                                }
                                else{
                                    $lnameErr = "your last name should only include letters";
                                }
                            }
                            else{
                                $lnameErr = "your last name must be at least 2 characters";
                            }
                        }
                        else{
                            $lnameErr = "enter your last name";
                        }
                    }
                    else{
                        $lnameErr = "enter your last name";
                    }

                    if(isset($_POST['idnum'])){
                        if(!empty($_POST['idnum'])){
                            if(strlen($_POST['idnum'])==10){
                                if(preg_match("/[0-9]{10}/", $_POST['idnum'])==1){
                                    $idnum = test_input($_POST['idnum']);
                                    $content .= "ID_No.:";
                                    $content .= $idnum;
                                    $content .= " ";
                                }
                                else{
                                    $idErr = "your ID number should only include digits";
                                }
                            }
                            else{
                                $idErr = "your ID number must be 10 digits";
                            }
                        }
                        else{
                            $idErr = "enter your ID number";
                        }
                    }
                    else{
                        $idErr = "enter your ID number";
                    }
                    if(isset($_POST['placeOfBirth'])){
                        if(!empty($_POST['placeOfBirth'])){
                            if(strlen($_POST['placeOfBirth'])>2){
                                if(preg_match("/[a-zA-Z]+/", $_POST['placeOfBirth'])==1){
                                    $placeOfBirth = test_input($_POST['placeOfBirth']);
                                    $content .= "placeOfBirth:";
                                    $content .= $placeOfBirth;
                                    $content .= " ";
                                }
                                else{
                                    $placeErr = "your place of birth should only include digits";
                                }
                            }
                            else{
                                $placeErr = "your place of birth must be at least 3 letters";
                            }
                        }
                    }   
                    if(isset($_POST['date'])){
                        if(!empty($_POST['date'])){
                            $date = test_input($_POST['date']);
                            $content .= "date:";
                            $content .= $date;
                            $content .= " ";
                        }
                        else{
                            $dateErr = "enter your date of birth";
                        }
                    }
                    else{
                        $dateErr = "enter your date of birth";
                    }


                    if(isset($_POST['email'])){
                        if(!empty($_POST['email'])){
                                $email = test_input($_POST['email']);
                                $content .= "email:";
                                $content .= $email;
                                $content .= " ";
                        }
                        else{
                            $emailErr = "enter your email";
                        }
                    }
                    else{
                        $emailErr = "enter your email";
                    }

                    if(isset($_POST['password'])){
                        if(!empty($_POST['password'])){
                                if(preg_match("/(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?!.*s).*$/", $_POST['password'])==1){
                                    $password = test_input($_POST['password']);
                                    $content .= "password:";
                                    $content .= $password;
                                    $content .= " ";
                                }
                                else{
                                    $pswErr = "your password should have numbers, uppercase<br/> & lowercase letters";
                                }
                        }
                        else{
                            $pswErr = "enter your password";
                        }
                    }
                    else{
                        $pswErr = "enter your password";
                    }
                    if(file_exists($email.".txt")){
                        echo(
                            "<script>
                                alert('this email already exists!');
                            </script>"
                            );
                        $firstname = $lastname = $idnum = $date = $placeOfBirth = $password = "";
                    }
                    else{
                        $file = fopen($email.".txt","a+") or die("Unable to open file!");
                        fwrite($file, $content);
                        fclose($file);
                        header("location: registered.php");
                    }
                }
            ?>
            <fieldset id = "fieldset">
                <legend><h1 class = "reg" >Register Form</h1></legend>
                <form method="post" action="#" enctype = "multipart/form-data">
                <table class = "table">
                    <tr>
                        <td  class = "labels">
                            <p class = "star">*</p> First Name
                        </td>
                        <td><input class = "inclass" type="text" required="required" placeholder="Please enter your first name" id = "firstname" name="firstname" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $fnameErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels"><p class = "star">*</p> Last Name</td>
                        <td><input class = "inclass" type="text" required="required" placeholder="Please enter your last name" name="lastname" id = "lastname" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $lnameErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels">Gender</td>
                        <td>
                            <input class = "inclass" type="radio" name="gender" value="0" /><p class = "radio" >Male</p>
                            <input class = "inclass" type="radio" name="gender" value="1" /><p class = "radio" >Female</p>
                        </td>
                    </tr>
                    <tr>
                        <td class = "labels"><p class = "star">*</p> ID Number</td>
                        <td><input class = "inclass" type="text" id="idnum" required="required" placeholder="Please enter your ID number" name="idnum" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $idErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels">Place of Birth</td>
                        <td><input class = "inclass" type="text" placeholder="Please enter your place of birth" name="placeOfBirth" id = "place" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $placeErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels"><p class = "star">*</p> Date Of Birth</td>
                        <td><input class = "inclass" type="date" id = "date" name="date"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $dateErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels"><p class = "star">*</p> Email</td>
                        <td><input class = "inclass" type="email" required="required" placeholder="Please enter your email" name="email" id = "email" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $emailErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td class = "labels"><p class = "star">*</p> Passwords</td>
                        <td><input class = "inclass" type="password" required="required" placeholder="Please enter your password" name="password" id = "password" onclick = "colorBorder(this.id)"/></td>
                    </tr>
                    <td  class = "err">
                        <span>
                                <?php
                                    echo $pswErr;
                                ?>
                        </span>
                    </td>
                    <tr>
                        <td><input type="submit" name="register" value="register" class="save"/></td>
                        <td><input type="reset" name = "reset" id = "reset"/></td>
                    </tr>
                </table>
                </form>
            </fieldset>
        </div>

Aucun commentaire:

Enregistrer un commentaire