PHP 使用 preg_match 和正则表达式进行表单验证
PHP Form Validation with preg_match and regular expressions
大家好,我目前正在尝试验证表单中的用户输入。我正在使用正则表达式和 php preg_match 函数。我正在尝试使用电子邮件、phone 号码和邮政编码来执行此操作。但是,当用户输入不符合正则表达式的电子邮件、phone 号码或邮政编码时,什么也没有发生,用户应该会在这种情况下收到一条错误消息。所以我相信我的正则表达式没有被读取,我也不知道为什么。感谢您的帮助。
这是我的代码:
<?php
if(isset($_POST['order'])){
$fname = trim(htmlspecialchars($_POST['first_name']));
echo $fname;
$address = trim(htmlspecialchars($_POST['address']));
echo $address;
$city = trim(htmlspecialchars($_POST['city']));
echo $city;
$zipcode = trim(htmlspecialchars($_POST['zip_code']));
echo $zipcode;
$email = trim(htmlspecialchars($_POST['email']));
echo $email;
$validzip = "/^\d{5}$|^\d{5}-\d{4}$/";
$validemail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
$state = $_POST['states'];
echo $state;
$lname = trim(htmlspecialchars($_POST['last_name']));
echo $lname;
$phonenumber = trim(htmlspecialchars($_POST['phone_number']));
echo $phonenumber;
$vaildpn = "/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/";
if(isset($_POST['first_name']) && $fname!= "" && isset($_POST['address'])&& $address !="" && isset($_POST['city']) && $city != "" && isset($_POST['zip_code']) && $zipcode != ""&& isset($_POST['email']) && $email != "" && isset($_POST['last_name']) && $lname != "" && isset($_POST['phone_number']) && $phonenumber != "" && isset($_POST['card_Number']) && $cardnumber != "" && isset($_POST['exp_date']) && $expdate != "" && isset($_POST['cvc']) && $cvc != "" && preg_match($validzip, $zipcode) && preg_match($validemail, $email)){
header("Location: orderplaced.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Confrimation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/confirm.css">
</head>
<body>
<header>
<h1>Order Confrimation</h1>
</header>
<main>
<nav>
<ul>
<form action="cart.php">
<li><button type="submit" name="back" value="Back to Cart">Back to Cart</button></li>
</form>
<form action="index.php">
<li><button type="submit" action="index.php" name="shopping">Return to Shopping</button></li>
</form>
</ul>
</nav>
<form method="post" name="shipping" action="confirm.php">
<h2>Please enter your shipping information:</h2>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="first_name"
<?php
if(isset($_POST['first_name'])&& $fname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['fname'])){
echo "value=$fname>";
}
else{
echo"><span>*</span>";
}
?>
<input type="hidden" name="fname2" value="<?php echo "$fname2"; ?>" />
<br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="last_name"
<?php
if(isset($_POST['last_name'])&& $lname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['lname'])){
echo "value=$lname>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="email">Email:</label>
<input type="text" id="email" name="email"
<?php
if(isset($_POST['email'])&& $email==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['email'])){
echo "value=$email>";
}
else if(isset($_POST['email']) && !preg_match($validemail, $email)){
echo "value=$email>";
echo "<span>Please enter a vaild email</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address"
<?php
if(isset($_POST['address'])&& $address==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['address'])){
echo "value=$address>";
}
else{
echo"><span>*</span>";
}
?><br>
<label for ="state">State</label>
<select name="states">
<option value="AL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AL') ? 'selected="selected"' : ''; ?>>Alabama</option>
<option value="AK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AK') ? 'selected="selected"' : ''; ?>>Alaska</option>
<option value="AZ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AZ') ? 'selected="selected"' : ''; ?>>Arizona</option>
<option value="AR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AR') ? 'selected="selected"' : ''; ?>>Arkansas</option>
<option value="CA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CA') ? 'selected="selected"' : ''; ?>>California</option>
<option value="CO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CO') ? 'selected="selected"' : ''; ?>>Colorado</option>
<option value="CT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CT') ? 'selected="selected"' : ''; ?>>Connecticut</option>
<option value="DE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'DE') ? 'selected="selected"' : ''; ?>>Delaware</option>
<option value="FL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'FL') ? 'selected="selected"' : ''; ?>>Florida</option>
<option value="GA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'GA') ? 'selected="selected"' : ''; ?>>Georgia</option>
<option value="HI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'HI') ? 'selected="selected"' : ''; ?>>Hawaii</option>
<option value="ID"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ID') ? 'selected="selected"' : ''; ?>>Idaho</option>
<option value="IL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IL') ? 'selected="selected"' : ''; ?>>Illinois</option>
<option value="IN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IN') ? 'selected="selected"' : ''; ?>>Indiana</option>
<option value="IA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IA') ? 'selected="selected"' : ''; ?>>Iowa</option>
<option value="KS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KS') ? 'selected="selected"' : ''; ?>>Kansas</option>
<option value="KY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KY') ? 'selected="selected"' : ''; ?>>Kentucky</option>
<option value="LA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'LA') ? 'selected="selected"' : ''; ?>>Louisiana</option>
<option value="ME"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ME') ? 'selected="selected"' : ''; ?>>Maine</option>
<option value="MD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MD') ? 'selected="selected"' : ''; ?>>Maryland</option>
<option value="MA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MA') ? 'selected="selected"' : ''; ?>>Massachusetts</option>
<option value="MI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MI') ? 'selected="selected"' : ''; ?>>Michigan</option>
<option value="MN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MN') ? 'selected="selected"' : ''; ?>>Minnesota</option>
<option value="MS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MS') ? 'selected="selected"' : ''; ?>>Mississippi</option>
<option value="MO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MO') ? 'selected="selected"' : ''; ?>>Missouri</option>
<option value="MT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MT') ? 'selected="selected"' : ''; ?>>Montana</option>
<option value="NE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NE') ? 'selected="selected"' : ''; ?>>Nebraska</option>
<option value="NV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NV') ? 'selected="selected"' : ''; ?>>Nevada</option>
<option value="NH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NH') ? 'selected="selected"' : ''; ?>>New Hampshire</option>
<option value="NJ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NJ') ? 'selected="selected"' : ''; ?>>New Jersey</option>
<option value="NM"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NM') ? 'selected="selected"' : ''; ?>>New Mexico</option>
<option value="NY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NY') ? 'selected="selected"' : ''; ?>>New York</option>
<option value="NC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NC') ? 'selected="selected"' : ''; ?>>North Carolina</option>
<option value="ND"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ND') ? 'selected="selected"' : ''; ?>>North Dakota</option>
<option value="OH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OH') ? 'selected="selected"' : ''; ?>>Ohio</option>
<option value="OK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OK') ? 'selected="selected"' : ''; ?>>Oklahoma</option>
<option value="OR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OR') ? 'selected="selected"' : ''; ?>>Oregon</option>
<option value="PA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'PA') ? 'selected="selected"' : ''; ?>>Pennsylvania</option>
<option value="RI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'RI') ? 'selected="selected"' : ''; ?>>Rhode Island</option>
<option value="SC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SC') ? 'selected="selected"' : ''; ?>>South Carolina</option>
<option value="SD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SD') ? 'selected="selected"' : ''; ?>>South Dakota</option>
<option value="TN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TN') ? 'selected="selected"' : ''; ?>>Tennessee</option>
<option value="TX"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TX') ? 'selected="selected"' : ''; ?>>Texas</option>
<option value="UT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'UT') ? 'selected="selected"' : ''; ?>>Utah</option>
<option value="VT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VT') ? 'selected="selected"' : ''; ?>>Vermont</option>
<option value="VA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VA') ? 'selected="selected"' : ''; ?>>Virginia</option>
<option value="WA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WA') ? 'selected="selected"' : ''; ?>>Washington</option>
<option value="WV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WV') ? 'selected="selected"' : ''; ?>>West Virginia</option>
<option value="WI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WI') ? 'selected="selected"' : ''; ?>>Wisconsin</option>
<option value="WY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WY') ? 'selected="selected"' : ''; ?>>Wyoming</option>
</select>
<label for ="city">City:</label>
<input type="text" id="city" name="city"
<?php
if(isset($_POST['city'])&& $city==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['city'])){
echo "value=$city>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="zipcode">Zip Code:</label>
<input type="text" id="zipcode" name="zip_code"
<?php
if(isset($_POST['zip_code'])&& $zipcode==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['zip_code'])){
echo "value=$zipcode>";
}
else if(isset($_POST['zip_code'])&& !preg_match($validzip, $zipcode) ){
echo "value=$zipcode>";
echo "<span>Please enter a vaild zip-code</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="phonenumber">Phone Number:</label>
<input type="text" id="phonenumber" name="phone_number"
<?php
if(isset($_POST['phone_number'])&& $phonenumber==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['phone_number'])){
echo "value=$phonenumber>";
}
else if(isset($_POST['phone_number'])&& !preg_match($vaildpn, $phonenumber)){
echo "value=$phonenumber>";
echo "<span>Invaild Phone Number, please enter between 7-10 digts</span>";
}
else{
echo"><span>*</span>";
}
?>
<button type="submit" name="order" id="order" value="Checkout">Checkout</button>
</form>
</main>
<footer>
Sierra Sprungl – CS3800 Final Project - Spring 2020
</footer>
没有 $_POST['order']
正在提交。尝试将此添加到您的表单中:
<input type="hidden" value="1" name="order">
我还建议您使用更好的验证方法,例如 filter_var
大家好,我目前正在尝试验证表单中的用户输入。我正在使用正则表达式和 php preg_match 函数。我正在尝试使用电子邮件、phone 号码和邮政编码来执行此操作。但是,当用户输入不符合正则表达式的电子邮件、phone 号码或邮政编码时,什么也没有发生,用户应该会在这种情况下收到一条错误消息。所以我相信我的正则表达式没有被读取,我也不知道为什么。感谢您的帮助。
这是我的代码:
<?php
if(isset($_POST['order'])){
$fname = trim(htmlspecialchars($_POST['first_name']));
echo $fname;
$address = trim(htmlspecialchars($_POST['address']));
echo $address;
$city = trim(htmlspecialchars($_POST['city']));
echo $city;
$zipcode = trim(htmlspecialchars($_POST['zip_code']));
echo $zipcode;
$email = trim(htmlspecialchars($_POST['email']));
echo $email;
$validzip = "/^\d{5}$|^\d{5}-\d{4}$/";
$validemail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
$state = $_POST['states'];
echo $state;
$lname = trim(htmlspecialchars($_POST['last_name']));
echo $lname;
$phonenumber = trim(htmlspecialchars($_POST['phone_number']));
echo $phonenumber;
$vaildpn = "/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/";
if(isset($_POST['first_name']) && $fname!= "" && isset($_POST['address'])&& $address !="" && isset($_POST['city']) && $city != "" && isset($_POST['zip_code']) && $zipcode != ""&& isset($_POST['email']) && $email != "" && isset($_POST['last_name']) && $lname != "" && isset($_POST['phone_number']) && $phonenumber != "" && isset($_POST['card_Number']) && $cardnumber != "" && isset($_POST['exp_date']) && $expdate != "" && isset($_POST['cvc']) && $cvc != "" && preg_match($validzip, $zipcode) && preg_match($validemail, $email)){
header("Location: orderplaced.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Confrimation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/confirm.css">
</head>
<body>
<header>
<h1>Order Confrimation</h1>
</header>
<main>
<nav>
<ul>
<form action="cart.php">
<li><button type="submit" name="back" value="Back to Cart">Back to Cart</button></li>
</form>
<form action="index.php">
<li><button type="submit" action="index.php" name="shopping">Return to Shopping</button></li>
</form>
</ul>
</nav>
<form method="post" name="shipping" action="confirm.php">
<h2>Please enter your shipping information:</h2>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="first_name"
<?php
if(isset($_POST['first_name'])&& $fname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['fname'])){
echo "value=$fname>";
}
else{
echo"><span>*</span>";
}
?>
<input type="hidden" name="fname2" value="<?php echo "$fname2"; ?>" />
<br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="last_name"
<?php
if(isset($_POST['last_name'])&& $lname==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['lname'])){
echo "value=$lname>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="email">Email:</label>
<input type="text" id="email" name="email"
<?php
if(isset($_POST['email'])&& $email==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['email'])){
echo "value=$email>";
}
else if(isset($_POST['email']) && !preg_match($validemail, $email)){
echo "value=$email>";
echo "<span>Please enter a vaild email</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address"
<?php
if(isset($_POST['address'])&& $address==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['address'])){
echo "value=$address>";
}
else{
echo"><span>*</span>";
}
?><br>
<label for ="state">State</label>
<select name="states">
<option value="AL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AL') ? 'selected="selected"' : ''; ?>>Alabama</option>
<option value="AK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AK') ? 'selected="selected"' : ''; ?>>Alaska</option>
<option value="AZ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AZ') ? 'selected="selected"' : ''; ?>>Arizona</option>
<option value="AR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'AR') ? 'selected="selected"' : ''; ?>>Arkansas</option>
<option value="CA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CA') ? 'selected="selected"' : ''; ?>>California</option>
<option value="CO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CO') ? 'selected="selected"' : ''; ?>>Colorado</option>
<option value="CT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'CT') ? 'selected="selected"' : ''; ?>>Connecticut</option>
<option value="DE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'DE') ? 'selected="selected"' : ''; ?>>Delaware</option>
<option value="FL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'FL') ? 'selected="selected"' : ''; ?>>Florida</option>
<option value="GA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'GA') ? 'selected="selected"' : ''; ?>>Georgia</option>
<option value="HI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'HI') ? 'selected="selected"' : ''; ?>>Hawaii</option>
<option value="ID"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ID') ? 'selected="selected"' : ''; ?>>Idaho</option>
<option value="IL"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IL') ? 'selected="selected"' : ''; ?>>Illinois</option>
<option value="IN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IN') ? 'selected="selected"' : ''; ?>>Indiana</option>
<option value="IA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'IA') ? 'selected="selected"' : ''; ?>>Iowa</option>
<option value="KS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KS') ? 'selected="selected"' : ''; ?>>Kansas</option>
<option value="KY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'KY') ? 'selected="selected"' : ''; ?>>Kentucky</option>
<option value="LA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'LA') ? 'selected="selected"' : ''; ?>>Louisiana</option>
<option value="ME"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ME') ? 'selected="selected"' : ''; ?>>Maine</option>
<option value="MD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MD') ? 'selected="selected"' : ''; ?>>Maryland</option>
<option value="MA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MA') ? 'selected="selected"' : ''; ?>>Massachusetts</option>
<option value="MI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MI') ? 'selected="selected"' : ''; ?>>Michigan</option>
<option value="MN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MN') ? 'selected="selected"' : ''; ?>>Minnesota</option>
<option value="MS"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MS') ? 'selected="selected"' : ''; ?>>Mississippi</option>
<option value="MO"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MO') ? 'selected="selected"' : ''; ?>>Missouri</option>
<option value="MT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'MT') ? 'selected="selected"' : ''; ?>>Montana</option>
<option value="NE"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NE') ? 'selected="selected"' : ''; ?>>Nebraska</option>
<option value="NV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NV') ? 'selected="selected"' : ''; ?>>Nevada</option>
<option value="NH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NH') ? 'selected="selected"' : ''; ?>>New Hampshire</option>
<option value="NJ"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NJ') ? 'selected="selected"' : ''; ?>>New Jersey</option>
<option value="NM"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NM') ? 'selected="selected"' : ''; ?>>New Mexico</option>
<option value="NY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NY') ? 'selected="selected"' : ''; ?>>New York</option>
<option value="NC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'NC') ? 'selected="selected"' : ''; ?>>North Carolina</option>
<option value="ND"<?php echo (isset($_POST['states']) && $_POST['states'] == 'ND') ? 'selected="selected"' : ''; ?>>North Dakota</option>
<option value="OH"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OH') ? 'selected="selected"' : ''; ?>>Ohio</option>
<option value="OK"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OK') ? 'selected="selected"' : ''; ?>>Oklahoma</option>
<option value="OR"<?php echo (isset($_POST['states']) && $_POST['states'] == 'OR') ? 'selected="selected"' : ''; ?>>Oregon</option>
<option value="PA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'PA') ? 'selected="selected"' : ''; ?>>Pennsylvania</option>
<option value="RI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'RI') ? 'selected="selected"' : ''; ?>>Rhode Island</option>
<option value="SC"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SC') ? 'selected="selected"' : ''; ?>>South Carolina</option>
<option value="SD"<?php echo (isset($_POST['states']) && $_POST['states'] == 'SD') ? 'selected="selected"' : ''; ?>>South Dakota</option>
<option value="TN"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TN') ? 'selected="selected"' : ''; ?>>Tennessee</option>
<option value="TX"<?php echo (isset($_POST['states']) && $_POST['states'] == 'TX') ? 'selected="selected"' : ''; ?>>Texas</option>
<option value="UT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'UT') ? 'selected="selected"' : ''; ?>>Utah</option>
<option value="VT"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VT') ? 'selected="selected"' : ''; ?>>Vermont</option>
<option value="VA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'VA') ? 'selected="selected"' : ''; ?>>Virginia</option>
<option value="WA"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WA') ? 'selected="selected"' : ''; ?>>Washington</option>
<option value="WV"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WV') ? 'selected="selected"' : ''; ?>>West Virginia</option>
<option value="WI"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WI') ? 'selected="selected"' : ''; ?>>Wisconsin</option>
<option value="WY"<?php echo (isset($_POST['states']) && $_POST['states'] == 'WY') ? 'selected="selected"' : ''; ?>>Wyoming</option>
</select>
<label for ="city">City:</label>
<input type="text" id="city" name="city"
<?php
if(isset($_POST['city'])&& $city==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['city'])){
echo "value=$city>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="zipcode">Zip Code:</label>
<input type="text" id="zipcode" name="zip_code"
<?php
if(isset($_POST['zip_code'])&& $zipcode==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['zip_code'])){
echo "value=$zipcode>";
}
else if(isset($_POST['zip_code'])&& !preg_match($validzip, $zipcode) ){
echo "value=$zipcode>";
echo "<span>Please enter a vaild zip-code</span>";
}
else{
echo"><span>*</span>";
}
?>
<br>
<label for ="phonenumber">Phone Number:</label>
<input type="text" id="phonenumber" name="phone_number"
<?php
if(isset($_POST['phone_number'])&& $phonenumber==""){
echo"><span>This field is required</span>";
}
else if(isset($_POST['phone_number'])){
echo "value=$phonenumber>";
}
else if(isset($_POST['phone_number'])&& !preg_match($vaildpn, $phonenumber)){
echo "value=$phonenumber>";
echo "<span>Invaild Phone Number, please enter between 7-10 digts</span>";
}
else{
echo"><span>*</span>";
}
?>
<button type="submit" name="order" id="order" value="Checkout">Checkout</button>
</form>
</main>
<footer>
Sierra Sprungl – CS3800 Final Project - Spring 2020
</footer>
没有 $_POST['order']
正在提交。尝试将此添加到您的表单中:
<input type="hidden" value="1" name="order">
我还建议您使用更好的验证方法,例如 filter_var