简体   繁体   中英

Redirect to new page after PHP validation

I am very new to php and have been working on a website which contains a form for a restaurant reservation. Currently, I have one file, which contains both html and php code. The form is validated once the user clicks submit, however I was wondering how it might be possible to redirect the user to a new page, confirming their reservation, if all of the information they have entered into the form is correct.

Basically this is the process I wish the website to perform:

user fills out form

if validation not complete

   display error messages, loop back to form so user can correct fields

if form is validated fully

    Send user to confirmation page

Here is the necessary code for my reservations page:

....


<?php
$nameErr = $teleErr = $emailErr = $partyErr = $vipErr = $reservationErr = $timeErr = "";
$name = $tele = $email = $party = $vip = $reservation = $time = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Please enter a full name";
    } else {
        $name = test_input($_POST["name"]);
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Invalid name entered";
        }
    }

    if (empty($_POST["tele"])) {
        $teleErr = "Please enter a telephone number";
    } else {
        $tele = test_input($_POST["tele"]);
        if (!preg_match("/^[0-9 ]{7,}$/",$tele)) {
            $teleErr = "Invalid telephone number entered";
        }
    }

    if (empty($_POST["email"])) {
        $emailErr = "Please enter an email address";
    } else {
        $email = test_input($_POST["email"]);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email entered";
        }
    }

    if($_POST['party']=="") {
        $partyErr = "Please select the party size";
    } else {
        $party = test_input($_POST["party"]);
    }

    if (empty($_POST["vip"])) {
        $vipErr = "Please make a VIP area selection";
    } else {
        $vip = test_input($_POST["vip"]);
    }

    if (empty($_POST["reservation"])) {
        $reservationErr = "Please enter the reservation date";
    } else {
        $reservation = test_input($_POST["reservation"]);
        if (!preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/",$reservation)) {
            $reservationErr = "Invalid reservation date";
        }
    }

    if($_POST['time']=="") {
        $timeErr = "Please select the reservation time";
    } else {
        $time = test_input($_POST["time"]);
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<body>

<div id= "container">

<div id="header">
<div id="logo">
<img src="Steakhouselogo.png" width="440" height="152" alt="This is an image of the Steakhouse® logo">
</div>
<br>
<p class="slogan"> <strong> Welcome to Steakhouse®, the number 1 restaurant for flame grilled goodness. </strong> </p>
</div>
<div id="links">
<ul class="nav">

</ul>
</div>
<br>

<!-- Introduction of HTML form -->
<div id="body">
<h1> Book a Table </h1>
<br><br>

<br>
<div class="view">
<img src="view.png" width="451" height="227" alt="A view of our restaurant">
</div>
<br>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<!-- Personal information -->
<div class="form">
<div class="indicates">
<br>
* indicates a required field
</div>
<p  class="ex">
<br><br>
<strong> Full Name* : </strong> <br> <input type="text" placeholder="John Doe" name="name" value="<?php echo $name;?>">
<span class="error"> <?php echo $nameErr;?></span>
<br><br><br>

<strong> Contact Telephone* : </strong> <br> <input type="text" placeholder="Telephone Number" name="tele" value="<?php echo $tele;?>">
<span class="error"><?php echo $teleErr;?></span>
<br><br><br>

<strong> Contact Email* : </strong> <br> <input type="text" placeholder="Example@email.com" name="email" value="<?php echo $email;?>">
<span class="error"><?php echo $emailErr;?></span>
<br><br>

<!-- Party requirments -->

<br>
<strong>Select Party Size* :</strong>
<br>
<select name="party" id="party" value="<?php echo $party;?>">
<option value="">Please Select</option>
<option <?php if (isset($party) && $party=="5") echo "selected";?> value="5">1 Person (+£5)</option>
<option <?php if (isset($party) && $party=="10") echo "selected";?> value="10">2 People (+£10)</option>
<option <?php if (isset($party) && $party=="15") echo "selected";?> value="15">3 People (+£15)</option>
<option <?php if (isset($party) && $party=="20") echo "selected";?> value="20">4 People (+£20)</option>
<option <?php if (isset($party) && $party=="25") echo "selected";?> value="25">5 People (+£25)</option>
<option <?php if (isset($party) && $party=="30") echo "selected";?> value="30">6 People (+£30)</option>
<option <?php if (isset($party) && $party=="35") echo "selected";?> value="35">7 People (+£35)</option>
<option <?php if (isset($party) && $party=="40") echo "selected";?> value="40">8 People (+£40)</option>
<option <?php if (isset($party) && $party=="45") echo "selected";?> value="45">9 People (+£45)</option>
<option <?php if (isset($party) && $party=="50") echo "selected";?> value="50">10+ People (+£50)</option>
</select>
<span id="party" class="error"><?php echo $partyErr;?></span>
<br><br><br>
<strong>Dietary Requirements:</strong>
<br><br>
Vegetarian <input type="checkbox" name="diet[]" value="Vegetarian">
<br><br>
Vegan <input type="checkbox" name="diet[]" value="Vegan">
<br><br>
Peanut Allergy <input type="checkbox" name="diet[]" value="Peanut Allergy">
<br><br>
Gluten Allergy <input type="checkbox" name="diet[]" value="Gluten Allergy">
<br><br><br>

<strong> VIP area* : </strong> <br><br>
Yes (+£5) <input type="radio" name="vip" <?php if (isset($vip) && $vip=="Yes") echo "checked";?> value="Yes">
<br><span id="vip" class="error"><?php echo $vipErr;?></span><br>
No <input type="radio" name="vip" <?php if (isset($vip) && $vip=="No") echo "checked";?> value="No">
<br><br><br>

<strong> Reservation Date* : </strong> <br> <input type="text" placeholder="DD/MM/YYYY" name="reservation" value="<?php echo $reservation;?>">
<span class="error"><?php echo $reservationErr;?></span>
<br><br><br>

<strong> Reservation Time* : </strong>
<br>
<select name="time" value="<?php echo $time;?>">
<option value="">Please Select</option>
<option <?php if (isset($time) && $time=="17:00") echo "selected";?> value="17:00">17:00</option>
<option <?php if (isset($time) && $time=="17:30") echo "selected";?> value="17:30">17:30</option>
<option <?php if (isset($time) && $time=="18:00") echo "selected";?> value="18:00">18:00</option>
<option <?php if (isset($time) && $time=="18:30") echo "selected";?> value="18:30">18:30</option>
<option <?php if (isset($time) && $time=="19:00") echo "selected";?> value="19:00">19:00</option>
<option <?php if (isset($time) && $time=="19:30") echo "selected";?> value="19:30">19:30</option>
<option <?php if (isset($time) && $time=="20:00") echo "selected";?> value="20:00">20:00</option>
<option <?php if (isset($time) && $time=="20:30") echo "selected";?> value="20:30">20:30</option>
<option <?php if (isset($time) && $time=="21:00") echo "selected";?> value="21:00">21:00</option>
<option <?php if (isset($time) && $time=="21:30") echo "selected";?> value="21:30">21:30</option>
<option <?php if (isset($time) && $time=="22:00") echo "selected";?> value="22:00">22:00</option>
</select>
<span id="time" class="error"><?php echo $timeErr;?></span>
<br><br><br>
<strong> Any Additional Information: </strong>
<br>
<textarea name="comments" placeholder="Birthdays, Class Parties..." rows="7" cols="40"></textarea>
<br><br>
<div class="totalPrice">
The total reservation price will be calculated automatically once submitted.
<br><br><br>
</div>
<div class="submitEtc">
<input type="submit" id="submit" name="submit" value="Submit">
<input type="reset" value="Reset form">
<br><br><br><br>
....

I have put a lot of effort into my work thus far, so any suggestions are welcomed. Please remember I am new to web languages also. Thank you.

You can redirect your user using the header() .

header('Location: http://yoursite.com/dashboard');
exit();

Where are you sending this form data to?. To itself or database. Anyway upon submission, you can echo any of this javascript function to redirect the user to a new page after 1 seconds.

echo "<script>
window.setTimeout(function() {
    window.location.href = 'redirect.php';
}, 1000);
</script>";


or


echo '<script>
$(document).ready(function() {  
    window.setInterval(function() {
    var timeLeft    = $("#timeLeft").html();                                
        if(eval(timeLeft) == 0){
                window.location= ("welcome.php");                 
        }else{              
            $("#timeLeft").html(eval(timeLeft)- eval(1));
        }
    }, 1000); 
});  
</script>';

Try this

<?php
$nameErr = $teleErr = $emailErr = $partyErr = $vipErr = $reservationErr = $timeErr = "";
$name = $tele = $email = $party = $vip = $reservation = $time = "";

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

    $c = 0;

    if(empty($_POST["name"])) { {
        $nameErr = "Please enter a full name";
        $c++;
    }

    if(!empty($_POST["name"])) {
        $name = test_input($_POST["name"]);
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Invalid name entered";
            $c++;
        }
    }

    if (empty($_POST["tele"])) {
        $teleErr = "Please enter a telephone number";
        $c++;
    } 


    if (!empty($_POST["tele"])) {
        $tele = test_input($_POST["tele"]);
        if (!preg_match("/^[0-9 ]{7,}$/",$tele)) {
            $teleErr = "Invalid telephone number entered";
            $c++;
        }
    }

    if (empty($_POST["email"])) {
        $emailErr = "Please enter an email address";
        $c++;
    } 

    if (!empty($_POST["email"])) {
        $email = test_input($_POST["email"]);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email entered";
            $c++;
        }
    }

    if($_POST['party']=="") {
        $partyErr = "Please select the party size";
        $c++;
    } else {
        $party = test_input($_POST["party"]);
    }

    if (empty($_POST["vip"])) {
        $vipErr = "Please make a VIP area selection";
        $c++;
    } else {
        $vip = test_input($_POST["vip"]);
    }

    if (empty($_POST["reservation"])) {
        $reservationErr = "Please enter the reservation date";
        $c++;
    }

    if (!empty($_POST["reservation"])) {
        $reservation = test_input($_POST["reservation"]);
        if (!preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/",$reservation)) {
            $reservationErr = "Invalid reservation date";
            $c++;
        }
    }

    if($_POST['time']=="") {
        $timeErr = "Please select the reservation time";
        $c++;
    } else {
        $time = test_input($_POST["time"]);
    }

    if($c == 0) {
        // redirect here
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

Here is your code

<?php
  $nameErr = $teleErr = $emailErr = $partyErr = $vipErr = $reservationErr = $timeErr = "";
  $name = $tele = $email = $party = $vip = $reservation = $time = "";

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Please enter a full name";
  } else {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $nameErr = "Invalid name entered";
    }
  }

  if (empty($_POST["tele"])) {
    $teleErr = "Please enter a telephone number";
  } else {
    $tele = test_input($_POST["tele"]);
    if (!preg_match("/^[0-9 ]{7,}$/",$tele)) {
        $teleErr = "Invalid telephone number entered";
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Please enter an email address";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email entered";
    }
  }

  if($_POST['party']=="") {
    $partyErr = "Please select the party size";
  } else {
    $party = test_input($_POST["party"]);
  }

  if (empty($_POST["vip"])) {
    $vipErr = "Please make a VIP area selection";
  } else {
    $vip = test_input($_POST["vip"]);
  }

  if (empty($_POST["reservation"])) {
    $reservationErr = "Please enter the reservation date";
  } else {
    $reservation = test_input($_POST["reservation"]);
    if (!preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/",$reservation)) {
        $reservationErr = "Invalid reservation date";
  }
 }

 if($_POST['time']=="") {
    $timeErr = "Please select the reservation time";
 } else {
    $time = test_input($_POST["time"]);
 }

 if($nameErr == "" && $teleErr == "" && $emailErr == "" && $partyErr == "" && $vipErr == "" &&  $reservationErr == "" && $timeErr == ""){

    header('Location: http://yoursite.com/dashboard');
    exit();

}

 function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 }

?>

Indeed you have done a lot of complication without using Arrays . You may in the case of an error existence set an array named $errors and its key should be the element of the form which is being on check. For example look at the following blue print code:

if (!my_check_email($_POST['email'])){
/* my_check_email() is a custom function the performs email validation. It returns true for valid email and it returns false for invalid email 
*/
// Here invalid email
$errors['email'] = true;
} 

The repeat similar coding style for other elements of the form you want to validate. At the end you just have to check if the $errors is set or not as follows:

if (isset($errors)){ // do the necessary code for invalid input } else{ // Save the data and redirect the user using any mean of redirection. }

Means of redirection

1- using php header function as other answers stated. However, using header function should be used before any output in your file ie before any echo or print or even any new line or html tags in your script file.

2- using cient-side javascript like the follwoing:

echo "<script>\n
    window.location.href = 'redirect.php';\n
</script>";

3- Using client-side meta tag:

echo '<meta http-equiv="refresh" content="0;URL=http://www.indiana.edu/~account/new-directory" />';

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