在简单的PHP表单上练习,并且无法弄清楚为什么在未填写或选择字段时未显示验证错误消息。任何人都可以发现可能导致$错误不显示的内容吗?我无法判断$ errors数组是否正常运行。我希望每个错误消息都弹出未完成的字段下方,并且字段不清除,例如,是否只有一个不完整的表单。
感谢您的帮助。
views
答案 0 :(得分:0)
您需要将验证码保留在if条件之外。在if条件中,您已经在检查是否已设置所有字段,并且如果设置了所有字段,则不会显示错误。因此,在if条件开始之前移动验证代码。
答案 1 :(得分:0)
POST中的复选框只显示有值。
<?php
//DEFINE VARIABLES
$errors = array();
$success = false;
$name = $nameErr = $origin = $dob = $sex = $destination = null;
$destination_options = array("choose" => "--Please Choose Your Destination--", "mercury" => "Mercury", "venus" => "Venus", "moon" => "Moon", "jupiter" => "Jupiter", "saturn" => "Saturn", "uranus" => "Uranus", "neptune" => "Neptune", "pluto" => "Pluto", "ub313" => "2003 UB313");
$trip = $purpose = $arrest = $arrest_reason = $terrorist = $fears = $insurance = $terms = null;
// Check if form was submitted
if (isset($_POST['submitBtn'])) {
//VALIDATE INPUTS
if (empty($_POST['name']))
$errors['name'] = "Please enter your name.";
else
$name = $_POST['name'];
if (empty($_POST['origin']))
$errors['origin'] = "Please enter your country of origin.";
else
$origin = $_POST['origin'];
if (empty($_POST['dob']) && is_numeric($_POST['dob']))
$errors['dob'] = "Please enter a valid birth date.";
else
$dob = $_POST['dob'];
if (empty($_POST['sex']) && preg_match("/\b(M)\b/", $_POST['sex']) && preg_match("/\b(F)\b/", $_POST['sex']))
$errors['sex'] = "Please provide your sex.";
else
$sex = $_POST['sex'];
if (!array_key_exists($_POST['destination'], $destination_options))
$errors['destination'] = "Please select a destination.";
else
$destination = $_POST['destination'];
if (empty($_POST['trip']))
$errors['trip'] = "Please choose One Way or Round Trip travel.";
else
$trip = $_POST['trip'];
if (empty($_POST['purpose']))
$errors['purpose'] = "Please tell us why you\'re traveling.";
else
$purpose = $_POST['purpose'];
if (empty($_POST['arrest']))
$errors['arrest'] = "Please tell us if you were ever arrested.";
else
$arrest = $_POST['arrest'];
if (empty($_POST['arrest_reason']) && (isset($arrest) && $arrest=='yes'))
$errors['arrest_reason'] = "Please tell us why you were arrested.";
else
$arrest_reason = $_POST['arrest_reason'];
if (empty($_POST['terrorist']))
$errors['terrorist'] = "Come on now, tell us if you're a terrorist. We really need to know!";
else
$terrorist = $_POST['terrorist'];
if (empty($_POST['terms']))
$errors['terms'] = "You must agree to the terms & conditions before submitting.";
// IF NO ERRORS
if (!$errors) {
$success = true;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- font-family: 'Roboto', sans-serif; -->
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,300,500' rel='stylesheet' type='text/css'>
<title>Interplanetary Travel Registration</title>
<style type="text/css">
html {margin:0; padding:0; background-color:#eeeeee; font-family:'Roboto',Helvetica,Arial,sans-serif;}
#container {width:98%; min-width:480px; max-width:1000px; margin:0 auto;}
#intro {text-align:center; text-transform:uppercase;}
legend {font-weight:700; text-transform:uppercase;}
input {padding:0.4rem; border:1px solid #cccccc; border-radius:2px;}
textarea {padding:0.5rem; border:1px solid #cccccc; border-radius:2px;}
span.italic {font-style:italic;}
.finePrint {font-size:0.8rem;}
button {padding:0.7rem;cursor: pointer;}
.buttonDiv {margin:0 auto; width:60px; text-align:center;}
.error {color:#be1e2d; font-weight:700}
</style>
</head>
<body>
<div id="container">
<div id="intro">
<img src="img/p_logo.svg" alt="logo" />
<?php
// IF NO ERRORS AND FORM SUBMITS SUCCESSFULLY
if ($success) {
if ($_POST['arrest']=='no' && $_POST['terrorist']=='no' && $_POST['insurance']=='regular') {
echo "<p class='error'>THANK YOU! Your application is being processed.<br />Your estimated trip cost with REGULAR insurance is $40,000. <br />You will hear from a representative shortly to finalize the details. <br />Get ready to go to space!</p>";
} else if ($_POST['arrest']=='no' && $_POST['terrorist']=='no' && $_POST['insurance']=='deluxe') {
echo "<p class='error'>THANK YOU! Your application is being processed.<br />Your estimated trip cost with DELUXE insurance is $50,000. <br />You will hear from a representative shortly to finalize the details. <br />Get ready to go to space!</p>";
} else if ($_POST['arrest']=='no' && $_POST['terrorist']=='no' && $_POST['insurance']=='no') {
echo "<p class='error'>THANK YOU! Your application is being processed and you will hear from a representative soon.<br />Get ready to go to space!</p>";
}
if ($_POST['arrest']=='yes' || $_POST['terrorist']=='yes')
echo "<p class='error'>BASED ON YOUR INFORMATION, YOU HAVE BEEN DEEMED A SECURITY RISK AND YOUR APPLICATION IS DENIED.<br />AN AGENT WILL REACH YOU SHORTLY.</p>";
// CLEAR FIELDS
list($name, $origin, $dob, $sex, $destination, $trip, $purpose, $arrest, $arrest_reason, $terrorist, $fears, $insurance, $terms) = array(null, null, null, null, null, null, null, null, null, null, null, null, null);
}
?>
<h1>Interplanetary Shuttle Registration</h1>
<p>
Please complete all fields. <br />Costs will be calculated and applicants accepted or rejected for travel based on input.<br />
If approved, you will be contacted by a PlanetCo agent to complete the process.
</p>
</div>
<form action="space_trip_registration.php" method="POST">
<fieldset>
<legend>Basic Info</legend>
<label for="name">Full Name</label>
<input type="text" id="name" name="name" tabindex="1" value="<?php echo htmlspecialchars($name, ENT_QUOTES); ?>" />
<?php
if (isset($errors['name']))
echo "<p class='error'>".$errors['name']."</p>";
?>
<label for="origin">Country of Origin</label>
<input type="text" id="origin" name="origin" tabindex="2" value="<?php echo htmlspecialchars($origin, ENT_QUOTES); ?>" />
<?php
if (isset($errors['origin']))
echo "<p class='error'>".$errors['origin']."</p>";
?>
<br /><br />
<label for="dob">Date of Birth</label>
<input type="text" name="dob" maxlength="10" placeholder="MM/DD/YYYY" tabindex="3" value="<?php echo htmlspecialchars($dob, ENT_QUOTES);?>" />
<?php
if (isset($errors['dob']))
echo "<p class='error'>".$errors['dob']."</p>";
?>
<label for="sex">Sex</label>
<input type="text" name="sex" maxlength="1" placeholder="M/F" tabindex="4" value="<?php echo htmlspecialchars($sex, ENT_QUOTES); ?>" />
<?php
if (isset($errors['sex']))
echo "<p class='error'>".$errors['sex']."</p>";
?>
<br /><br />
<legend>Desination</legend>
<label for="destination">Destination</label>
<select name="destination" id="destination" tabindex="5">
<!-- <option selected="selected"> --Please Choose Your Destination-- </option> -->
<?php
foreach($destination_options as $key => $value)
echo "<option value='" .$key. "'".($key == $destination ? " selected='selected'" : " ").">".htmlspecialchars($value, ENT_QUOTES)."</option>"
?>
</select>
<?php
if (isset($errors['destination']))
echo "<p class='error'>".$errors['destination']."</p>";
?>
<br /><br />
<label for="trip">Is This A One-Way or Round- Trip?</label>
<br />
<input type="radio" id="trip" name="trip" value="one" tabindex="6">One Way <input type="radio" id="trip" name="trip" value="round" tabindex="7">Round Trip
<?php
if (isset($errors['trip']))
echo "<p class='error'>".$errors['trip']."</p>";
?>
<br /><br />
<label for="purpose">Primary Reason for Traveling</label>
<br />
<textarea rows="10" cols="50" id="purpose" name="purpose" tabindex="8" <?php echo htmlspecialchars($purpose, ENT_QUOTES); ?> ></textarea>
<?php
if (isset($errors['purpose']))
echo "<p class='error'>".$errors['purpose']."</p>";
?>
<br /><br />
<legend>Additional Info</legend>
<label for="arrest">Have You Ever Been Arrested?</label>
<input type="radio" name="arrest" value="yes">Yes <input type="radio" name="arrest" value="no" tabindex="9" >No
<?php
if (isset($errors['arrest']))
echo "<p class='error'>".$errors['arrest']."</p>";
?>
<br />
<label for="arrest_reason">If yes, why?</label>
<input type="text" name="arrest_reason" tabindex="10" value="<?php echo htmlspecialchars($arrest_reason, ENT_QUOTES); ?>" />
<?php
if (isset($errors['arrest_reason']))
echo "<p class='error'>".$errors['arrest_reason']."</p>";
?>
<br /><br />
<label for="terrorist">Are you a terrorist?</label>
<input type="radio" name="terrorist" value="yes" tabindex="11">Yes <input type="radio" name="terrorist" value="no" tabindex="12">No
<?php
if (isset($errors['terrorist']))
echo "<p class='error'>".$errors['terrorist']."</p>";
?>
<br /><br />
<!-- FEARS CHECKBOXES -->
<label for="fears">What Do You Fear? <span class="italic finePrint">(Check all that apply)</span></label><br />
<label for="aliens"><input type="checkbox" id="aliens" name="fears" value="aliens" tabindex="13">Aliens</label>
<label for="zero_gravity"><input type="checkbox" id="zero_graviy" name="fears" value="zero_gravity" tabindex="14">Zero Gravity</label>
<label for="solar_flares"><input type="checkbox" id="solar_flares" name="fears" value="solar_flares" tabindex="15">Solar Flares</label>
<br />
<label for="vast_space"><input type="checkbox" id="vast_space" name="fears" value="vast_space" tabindex="16">Vast Infinite Space</label>
<label for="black_holes"><input type="checkbox" id="black_holes" name="fears" value="black_holes" tabindex="17">Black Holes</label>
<label for="wormholes"><input type="checkbox" id="wormholes" name="fears" value="wormholes" tabindex="18">Wormholes</label>
<br />
<label for="airlock"><input type="checkbox" id="airlock" name="fears" value="airlock" tabindex="19">Getting accidentally blown out of an airlock by a malicious, self-aware computer</label> <br />
<label for="interstellar"><input type="checkbox" id="interstellar" name="fears" value="interstellar" tabindex="20">Getting lost in space-time like in <span class="italic">Interstellar</span></label>
<?php
if (isset($errors['fears']))
echo "<p class='error'>".$errors['fears']."</p>";
?>
<br /><br />
<!-- END FEARS CHECKBOXES -->
<legend>Insurance</legend>
<p>Trip insurance is offered in two packages: Deluxe and Regular.<br /><span class="italic finePrint">Insurance does not cover cases of catastrophic malfunction, alien abduction, or discharge from airlock by a malicious, self-aware computer.</span></p>
<label for="insurance">Purchase Trip Insurance</label>
<label for="no"><input type="radio" id="no" name="insurance" value="no" checked="checked" tabindex="21">No</label>
<label for="regular"><input type="radio" id="regular" name="insurance" value="regular" tabindex="22">Regular <span class="italic finePrint">(add $10,000)</span></label>
<label for="deluxe"><input type="radio" id="deluxe" name="insurance" value="deluxe" tabindex="23">Deluxe <span class="italic finePrint">(add $20,000)</span></label>
<br /><br />
<!-- FINAL SUBMIT SECTION -->
<legend>Terms and Conditions</legend>
<label for="terms"><input type="checkbox" id="terms" name="terms" value="terms" tabindex="24">I agree to the <a href="#" id='conditions' title="Terms and Conditions">terms and conditions</a> set by PlanetCo.</label>
<?php
if (isset($errors['terms']))
echo "<p class='error'>".$errors['terms']."</p>";
?>
<br /><br />
<div class="buttonDiv">
<button type="submit" name="submitBtn" tabindex="25">APPLY</button>
</div>
</fieldset>
</form>
</div>
<script type="text/javascript">
document.getElementById('conditions').onclick=function(){
alert("TERMS & CONDITIONS: It's space, dude. We're gonna do our best, but you travel at your own risk. It's like, how the hell can we do anything about aliens, right?");
}
</script>
</body>
</html>