views:

38

answers:

4

Here's my registerFormOne.php code:

<?php session_start();
require("validationLibrary.php");
$validForm = true;
?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 1 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>        
        <form action="registerFormTwo.php" method="post">
            <dt>First Name:</dt>
                <dd><input type="text" name="firstName" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['firstName'])){
                            if(!validateRequired($_POST['firstName'])){
                                $validForm = false;
                            }
                        }
                    ?>
                </dd><br />

            <dt>Last Name:</dt>
                <dd><input type="text" name="lastName" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['lastName'])){
                            if(!validateRequired($_POST['lastName'])){
                                $validForm = false;
                            }
                        }
                    ?>
                </dd><br />

            <dt>EMail:</dt>
                <dd><input type="text" name="email" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['email'])){
                            if(!validateEmail($_POST['email'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Age:</dt>
                <dd><input type="text" name="age" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['age'])){
                            if(!validateNumber($_POST['age'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Date of Birth:</dt>
                <dd><input type="text" name="dateOfBirth" /></dd><br />
                <dd>
                    <?php
                        if(isset($_POST['dateOfBirth'])){
                            if(!validateRequired($_POST['dateOfBirth'])){
                                $validForm = false;        
                            }
                        }                        
                    ?>
                </dd><br />

            <dt>Gender:</dt>
                <dd>Masculino <input type="radio" value="M" name="gender" checked/> &nbsp;&nbsp;
                Femenino <input type="radio" value="F" name="gender" />
                </dd>            

            <dt><input type="submit" /></dt>            
        </form>
    </body>
</html>

And here's my validationLibrary.php which I use as a class:

<?php
function validateRequired($field){
    if($field == ""){
        echo "Information for this field is required.";
        return false;
    }
    else{
        return true;
    }
}

function validateNumber($field){
    if(!is_numeric($field)){
        echo "Only numeric values are allowed in this field.";
        return false;
    }
    else{
        return true;
    }
}

function validateEmail($field){
    $regexp = "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z-][0-9a-zA-Z-]+\.)+[a-zA-Z](2,6)$/";

    if(!preg_match($regexp,$field)){
        echo "Please type in a valid email address.";
        return false;
    }
    else{
        return true;
    }
}

?>

I need to have the formOne reload again, and NOT go to the formTwo if the variable $validForm is false.

I have on idea how to do this. Thank you very much for your help. :)

+3  A: 

Change the action attribute of the form to submit to itself, not form #2. Only if your validation code passes do you redirect to form #2 with a location header().

Dan Grossman
A: 

Dunno what's the "best" way to do so, but what I do is make a form's action return to the same page, then check form input before I put any content on the page -- if there's something wrong, I do nothing, and I can prefill the values with $_POST values -- if nothing's wrong, I use header('Location: formTwo.php') to continue :) you can pass on the variables if after changing the header you reassign the variables.

henasraf
A: 

Something like this:

<?php

$errors = array();

if (isset($_POST['submit']))
{
    // validate and possibly populate the $errors array

    if (empty($errors))
    {
        // do something with the data
        header('Location: target.php');
        exit;
    }
}

?>
<html>
<?php if (!empty($errors)) : ?>        <?php echo implode(', ', $errors) ?>
<?php endif; ?>
    <form>
        <!-- fill form elements with values if they are set -->
        ...
    </form>
</html>
Franz
A: 

Are you opposed to using javascript for form validation? If not it will save a trip to the server and back, and the problem of reserving the form's page is avoided.

Aoriste