我正在处理一项需要我使用下拉菜单和文本输入处理乐队商品订单的作业。我正在同一页面上处理 php。我需要填写一个要求,当一个或多个字段未填写时,要求显示错误。我在逻辑上遇到了很多麻烦,正在找人查看我的代码,看看他们是否可以发现任何东西。我一整天都在搜索 Stack Overflow,但找不到任何解决方案。 它应该是这样的:页面加载,表单已启动,没有消息。您做出选择并输入您的信息并点击提交。如果一个或多个字段为空,则会显示一条消息,提示填写所有字段。如果填写了所有字段,它会显示您输入的信息并告诉您订购的商品类型。 现在它正在这样做:页面加载时出现“填写所有字段”错误。我做出选择并填写所有字段,它起作用了。但是,如果我在没有输入任何内容的情况下点击提交,它会给我成功消息,但没有数据,只有空格。没有任何错误迹象。
这是我的代码:
<?php
$message = "";
$chosenBand = $chosenColor = $chosenSize = $chosenStyle = $firstName = $lastName = $emailAddress =
$phoneNumber = $streetAddress = $cityName = $stateName = $zipCode = "";
$filled = 0;
if(isset($_POST['Submit']))
{
$filled = 0;
if(isset($_POST['bands']) && preg_match("/\w+/",$_POST['bands']) && $_POST['bands'] != "Choose a Band")
$chosenBand = $_POST['bands'];
$filled++;
if(isset($_POST['color']) && preg_match("/\w+/",$_POST['color']) && $_POST['color'] != "Choose a Color")
$chosenColor = $_POST['color'];
$filled++;
if(isset($_POST['size']) && preg_match("/\w+/",$_POST['size']) && $_POST['size'] != "Choose a Size")
$chosenSize = $_POST['size'];
$filled++;
if(isset($_POST['style']) && preg_match("/\w+/",$_POST['style']) && $_POST['style'] != "Choose a Style")
$chosenStyle = $_POST['style'];
$filled++;
if(isset($_POST['fname']) && preg_match("/\w+/",$_POST['fname']))
$firstName = $_POST['fname'];
$filled++;
if(isset($_POST['lname']) && preg_match("/\w+/",$_POST['lname']))
$lastName = $_POST['lname'];
$filled++;
if(isset($_POST['email']) && preg_match("/\w+/",$_POST['email']))
$emailAddress = $_POST['email'];
$filled++;
if(isset($_POST['phone']) && preg_match("/\w+/",$_POST['phone']))
$phoneNumber = $_POST['phone'];
$filled++;
if(isset($_POST['street']) && preg_match("/\w+/",$_POST['street']))
$streetAddress = $_POST['street'];
$filled++;
if(isset($_POST['city']) && preg_match("/\w+/",$_POST['city']))
$cityName = $_POST['city'];
$filled++;
if(isset($_POST['state']) && preg_match("/\w+/",$_POST['state']))
$stateName = $_POST['state'];
$filled++;
if(isset($_POST['zip']) && preg_match("/\w+/",$_POST['zip']))
$zipCode = $_POST['zip'];
$filled++;
if($filled == 12)
{
$message = $firstName." ".$lastName."<br>".$streetAddress."<br>".$cityName.", ".$stateName." ".$zipCode."<br>".$emailAddress."<br>".$phoneNumber."<br>"."Ordered: 1 ".$chosenSize." ".$chosenColor." ".$chosenBand." ".$chosenStyle;
}
else if($filled < 12)
{
$message = "Please fill out all fields";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Products</title>
</head>
<body>
<h1>Products page</h1>
<form method="post">
<select name="bands" id="bands">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenBand : 'Choose a Band';?></option>
<?php
$bandNames = array("Brand New", "Manchester Orchestra", "Twenty One Pilots", "Chequerboard",
"The Glitch Mob", "Olafur Arnalds", "Patrick O'Hearn", "Madeon", "Her Name is Calla", "pg.lost");
foreach($bandNames as $name)
{
echo "<option value='$name'>$name</option>";
}
?>
</select>
<select name="color" id="color">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenColor : 'Choose a Color';?></option>
<?php
$colors = array("Black", "Gray", "White", "Blue", "Red", "Pink");
foreach($colors as $color)
{
echo "<option value='$color'>$color</option>";
}
?>
</select>
<select name="size" id="size">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenSize : 'Choose a Size';?></option>
<?php
$sizes = array("XS", "S", "M", "L", "XL", "XXL");
foreach($sizes as $size)
{
echo "<option value='$size'>$size</option>";
}
?>
</select>
<select name="style" id="style">
<option selected="selected"><?php echo $result = isset($_POST['Submit']) ? $chosenStyle : 'Choose a Style';?></option>
<?php
$styles = array("Crew Neck Tee", "Crew Neck Sweatshirt", "Hoodie", "Crop Top", "Bomber Jacket",
"Windbreaker");
foreach($styles as $style)
{
echo "<option value='$style'>$style</option>";
}
?>
</select><br><br>
<input value="<?php echo $firstName;?>" placeholder="First Name" name="fname" id="fname"><br>
<input value="<?php echo $lastName;?>" placeholder="Last Name" name="lname" id="lname"><br>
<input value="<?php echo $emailAddress;?>" placeholder="Email" name="email" id="email"><br>
<input value="<?php echo $phoneNumber;?>" placeholder="Phone" name="phone" id="phone"><br><br>
<input value="<?php echo $streetAddress;?>" placeholder="Street" name="street" id="street"><br>
<input value="<?php echo $cityName;?>" placeholder="City" name="city" id="city"><br>
<input value="<?php echo $stateName;?>" placeholder="State" name="state" id="state"><br>
<input value="<?php echo $zipCode;?>" placeholder="Zip" name="zip" id="zip"><br><br>
<input type="submit" value="Submit" name="Submit"><br><br>
<?php echo $message; ?>
<?php echo $filled; ?>
</form>
</body>
</html>
我对 PHP 很陌生,所以请记住这一点。