Skip to content
Advertisement

Moving Page after Validation in PHP

I want to change page after validation in PHP but, it appears on the same page with the validation.

Here is the logical process i want

if validation didnt complete/invalid input

   display error messages, and user must input again in the same page.

if form is validated complete with no invalid input.

    User will be moved to a new page for reviewing the inputed data.

And this is my PHP

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
    }

    if (empty($_POST["website"])) {
        $website = "";
    } else {
        $website = test_input($_POST["website"]);
    }

    if (empty($_POST["comment"])) {
        $comment = "";
    } else {
        $comment = test_input($_POST["comment"]);
    }

    if (empty($_POST["gender"])) {
        $genderErr = "Gender is required";
    } else {
        $gender = test_input($_POST["gender"]);
    }
}

if($nameErr == "" && $emailErr == "" && $genderErr == "" && $websiteErr == "") {
    header('Location: http://subc.chaidar-525.com');
    exit();
}
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

I use some referance from W3School, and it makes the review of data is in the same page as the form and validation, and i want the user will be transfered to another new page for reviewing their inputed data.

Advertisement

Answer

Use a session, roughly like this:

session_start();
if($nameErr == "" && $emailErr == "" && $genderErr == "" && $websiteErr == "") {
    $_SESSION['inputdata'] = $_POST;

    //A neater version would be to assign all vars, like:
    //$_SESSION['gender'] = $gender;

    header('Location: http://subc.chaidar-525.com');
    exit();
}


on the next page, use this:

session_start();

$input_data = $_SESSION['inputdata'];
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement