Практика на простой форме PHP и не может понять, почему сообщения об ошибках проверки не появляются, когда поля не заполнены или не выбраны. Может ли кто-нибудь определить, что может вызвать ошибки $, чтобы не показывать? Я не могу сказать, правильно ли работает массив $ errors. Я хотел бы, чтобы каждое сообщение об ошибке появилось под полями, которые не были заполнены, и для полей, которые не очищаются, если, скажем, существует только одна неполная форма.Ошибки проверки формы PHP, не отображающие
Спасибо за любую помощь.
<?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['name']) && isset($_POST['origin']) && isset($_POST['dob']) && isset($_POST['sex']) && isset($_POST['destination']) && isset($_POST['trip']) && isset($_POST['purpose']) && isset($_POST['arrest']) && isset($_POST['arrest_reason']) && isset($_POST['terrorist']) && isset($_POST['fears']) && isset($_POST['insurance']) && isset($_POST['terms'])) {
//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']) && $arrest = $_POST['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;
}
}
?>
<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" tabindex="25">APPLY</button>
</div>
</fieldset>
</form>
</div>
Спасибо, это было полезно - не полностью решена моя проблема (настройка еще одного кода сменила любой выигрыш), но поставил меня на правильный путь. Похоже, что большим фактором были флажки и радиоприемники, которые не устанавливаются, если они не отмечены. Не полностью решить проблему, но попасть туда. –