简体   繁体   English

PHP验证后重定向到新页面

[英]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. 我对php很陌生,一直在一个网站上工作,该网站包含一份餐厅预订表格。 Currently, I have one file, which contains both html and php code. 目前,我有一个文件,其中包含html和php代码。 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()重定向用户。

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. 无论如何,提交后,您都可以在1秒后回显此javascript函数中的任何一个,以将用户重定向到新页面。

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 . 确实,您在不使用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. 您可以在存在错误的情况下设置一个名为$errors的数组,其键应为正在检查的表单的元素。 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: 最后,您只需要检查$errors是否设置,如下所示:

if (isset($errors)){ // do the necessary code for invalid input } else{ // Save the data and redirect the user using any mean of redirection. if(isset($ errors)){//对无效输入执行必要的代码} else {//保存数据并使用任何重定向方式重定向用户。 } }

Means of redirection 重定向方式

1- using php header function as other answers stated. 1-使用php header功能作为其他答案。 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. 但是,应在文件中的任何输出之前(即在脚本文件中的任何echoprint或什至是任何新行或html标记之前)使用header函数。

2- using cient-side javascript like the follwoing: 2-使用像下面这样的cient javascript:

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

3- Using client-side meta tag: 3-使用客户端元标记:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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