简体   繁体   中英

php form 2 step confirmation

i try to challenge my self but i stuck( I try to create a php form with 2 steps confirmation:

  1. When the user fill up the form and hit Submit, it checks all the conditions(name, pass etc.). If everything ok automatically redirecting the user.

  2. After redirecting (to the same page) the user can check all the details again. If they ok, hit again the submit button which redirects to the final page.

I stuck on the 2nd phase...how to redirect to the final page?

I'm very beginner so i'm curios what could be done better or any advise.

<?php
 // the php code 
session_start();


if ($_SERVER['REQUEST_METHOD'] == "POST") {

// setting up the variables

$title = $_POST['title'];
$fName = trim(filter_input(INPUT_POST,'fName', FILTER_SANITIZE_STRING));
$lName = trim(filter_input(INPUT_POST,'lName',FILTER_SANITIZE_STRING));
$age = intval($_POST['age']);


$_SESSION['title'] = $title;
$_SESSION['fName'] = $fName;
$_SESSION['lName'] = $lName;
$_SESSION['age'] = $age;



//checking for possible errors
if ( $fName == "" || strlen($fName) <= 2 ) {
    $errorMsg1 = "<span>Provide your First name!(minimum 3 characters)</span>";
    $status = false;

}
else if ( $lName == "" || strlen($lName) <= 2 ) {
    $errorMsg2 = "<span>Provide your Last name!(minimum 3 characters)</span>";
    $status = false;

}
else if ( $age < 18 ) {
    $errorMsg3 = "<span>You must be 18 or above!</span>";
    $status = false;

}

else {  $status = true; }

// redirecting to done page

if ($status) {
    header("Location:TEST ZONE.php?status=awaiting");
    }
  }

 ?>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<?php

if ( isset($_GET['status']) && $_GET['status'] == "awaiting" ) {
    echo "<form>"
    . "Check your Details!<br>"
    . $_SESSION['title'] . "<br>"
    . $_SESSION['fName'] . "<br>"
    . $_SESSION['lName'] . "<br>"
    . $_SESSION['age'] . "<br>"
   // **NOW WHEN I'M in the awaiting phase, i don't know what to do(**

    . "<input type='submit' name='submit'/>";

    echo "</form>";
   }

   else {  ?>
    <form action="TEST ZONE.php" method="post">
    <h3>Register Form </h3>

    <label for="title">Title </label>
    <select name="title">
        <option name="mr">Mr</option>
        <option name="ms">Ms</option>
    </select><br><br><br>
    <label for="fName">First Name</label><br>
    <input type="text" name="fName" id="fName" value="<?php if    (isset($fName)) { echo $fName; } ?>"><br><?php
    if (isset( $errorMsg1 )) {
        echo $errorMsg1;
    }
    ?><br><br>

    <label for="lName">Last Name</label><br>
    <input type="text" name="lName" id="lName" value="<?php if (isset($lName)) { echo $lName; } ?>"><br><?php
    if (isset( $errorMsg2 )) {
        echo $errorMsg2;
    }
    ?><br><br>

    <label for="age">Age</label><br>
    <input type="text" name="age" id="age" value="<?php if (isset($age)) { echo $age; }?>"><br><?php
    if (isset($errorMsg3)){
        echo $errorMsg3;
    } ?><br><br>
    <input type="submit" value="Submit"><input type="reset">

</form> <?php } ?>
  </div>



 </body>
</html>

Add action in your form to redirect final page.

You already have all values in session so you can access it in final page also

<?php

 // the php code 
session_start();


if ($_SERVER['REQUEST_METHOD'] == "POST") {

// setting up the variables

$title = $_POST['title'];
$fName = trim(filter_input(INPUT_POST,'fName', FILTER_SANITIZE_STRING));
$lName = trim(filter_input(INPUT_POST,'lName',FILTER_SANITIZE_STRING));
$age = intval($_POST['age']);


$_SESSION['title'] = $title;
$_SESSION['fName'] = $fName;
$_SESSION['lName'] = $lName;
$_SESSION['age'] = $age;



//checking for possible errors
if ( $fName == "" || strlen($fName) <= 2 ) {
    $errorMsg1 = "<span>Provide your First name!(minimum 3 characters)</span>";
    $status = false;

}
else if ( $lName == "" || strlen($lName) <= 2 ) {
    $errorMsg2 = "<span>Provide your Last name!(minimum 3 characters)</span>";
    $status = false;

}
else if ( $age < 18 ) {
    $errorMsg3 = "<span>You must be 18 or above!</span>";
    $status = false;

}

else {  $status = true; }

// redirecting to done page

if ($status) {
    header("Location:TEST ZONE.php?status=awaiting");
    }
  }

 ?>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<?php

if ( isset($_GET['status']) && $_GET['status'] == "awaiting" ) {
    echo "<form action='final_page.php'>"
    . "Check your Details!<br>"
    . $_SESSION['title'] . "<br>"
    . $_SESSION['fName'] . "<br>"
    . $_SESSION['lName'] . "<br>"
    . $_SESSION['age'] . "<br>"
   // **NOW WHEN I'M in the awaiting phase, i don't know what to do(**

    . "<input type='submit' name='submit'/>";

    echo "</form>";
   }

   else {  ?>
    <form action="TEST ZONE.php" method="post">
    <h3>Register Form </h3>

    <label for="title">Title </label>
    <select name="title">
        <option name="mr">Mr</option>
        <option name="ms">Ms</option>
    </select><br><br><br>
    <label for="fName">First Name</label><br>
    <input type="text" name="fName" id="fName" value="<?php if    (isset($fName)) { echo $fName; } ?>"><br><?php
    if (isset( $errorMsg1 )) {
        echo $errorMsg1;
    }
    ?><br><br>

    <label for="lName">Last Name</label><br>
    <input type="text" name="lName" id="lName" value="<?php if (isset($lName)) { echo $lName; } ?>"><br><?php
    if (isset( $errorMsg2 )) {
        echo $errorMsg2;
    }
    ?><br><br>

    <label for="age">Age</label><br>
    <input type="text" name="age" id="age" value="<?php if (isset($age)) { echo $age; }?>"><br><?php
    if (isset($errorMsg3)){
        echo $errorMsg3;
    } ?><br><br>
    <input type="submit" value="Submit"><input type="reset">

</form> <?php } ?>
  </div>

final_page.php

<?php
session_start();
$title = $_SESSION['title'];
$fName = $_SESSION['fName'];
$lName = $_SESSION['lName'];
$age = $_SESSION['age'];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM