PHP验证后重定向到新页面

时间:2014-11-19 12:37:44

标签: php forms validation

我是非常新的PHP,并且一直在一个网站上工作,其中包含一个餐厅预订表格。目前,我有一个文件,其中包含html和php代码。一旦用户点击提交,表单就会被验证,但我想知道如果用户输入表单的所有信息都是正确的,可以将用户重定向到新页面,确认他们的预订。

基本上这是我希望网站执行的过程:

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

以下是我的预订页面的必要代码:

....


<?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>
....

到目前为止,我已经为我的工作付出了很多努力,所以欢迎任何建议。请记住我也是网络语言的新手。谢谢。

5 个答案:

答案 0 :(得分:0)

您可以使用header()重定向您的用户。

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

答案 1 :(得分:0)

您将此表单数据发送到哪里?对自己或数据库。无论如何,提交后,你可以回应 任何这个javascript函数来 1秒后将用户重定向到新页面。

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>';

答案 2 :(得分:0)

试试这个

<?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;
}
?>

答案 3 :(得分:0)

这是您的代码

<?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;
 }

&GT;

答案 4 :(得分:0)

确实你在不使用Arrays的情况下做了很多复杂化。您可以在存在错误的情况下设置名为$errors的数组,并且其键应该是正在检查的表单的元素。例如,请查看以下蓝色打印代码:

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;
} 

为要验证的表单的其他元素重复类似的编码样式。最后,您只需要检查$errors是否设置如下:

if(isset($ errors)){ //为无效输入执行必要的代码 } 其他{ //使用任何重定向方式保存数据并重定向用户。 }

重定向方式

1-使用php header函数作为其他答案。但是,在文件中的任何输出之前,即在任何echoprint之前,甚至在脚本文件中的任何新行或html标记之前,都应使用标题函数。

2-使用像下面这样的cient-side javascript:

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

3-使用客户端元标记:

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