Несколько ошибок в веб-форме PHP

У меня проблема с формой PHP, которую я разрабатываю. Я использую этот базовый код для проверки моей веб-формы и быстрого выполнения задачи:

http://www.html-form-guide.com/php-form/php-form-validation.html

Пока что все настроено так, как я хочу, кроме сообщений об ошибках. Пожалуйста, позвольте мне объяснить неприемлемое поведение, и, надеюсь, кто-то здесь может помочь мне исправить это.

Итак … в моей форме есть 4 обязательных поля (Имя, Фамилия, Электронная почта и Выпадающий список). Когда я проверяю форму и ввожу, например, только свое «Имя», форма возвращает каждое отдельное сообщение об ошибке, включая сообщение об ошибке, предназначенное для «Имени», даже если я действительно заполнил поле «Имя».

И то же самое относится к тому, какую комбинацию или сколько полей я заполняю, если это не заполненная форма (в этом случае она отправляется без проблем). Независимо от того, сколько или комбинация, все 4 сообщения об ошибках всегда возвращаются. Может кто-нибудь помочь? Вот код:

<?PHP
/*
Contact Form from HTML Form Guide
This program is free software published under the
terms of the GNU Lesser General Public License.
See this page for more info:
http://www.html-form-guide.com/php-form/php-form-validation.html
*/
require_once("C:\\wamp\\www\\sign-up-form\\php-form-validator\\samples\\3-with-client-side-validations\\include\\fgcontactform.php");
require_once("C:\\wamp\\www\\sign-up-form\\php-form-validator\\samples\\3-with-client-side-validations\\include\\formvalidator.php");

$formproc = new FGContactForm();

//Initialize the contact form
$formproc->AddRecipient("[email protected]"); //<<---Put your email address here
$formproc->SetFormRandomKey("CnRrspl1FyEylUj");

$validation_errors="";
if(isset($_POST["submitted"]))
{// We need to validate only after the form is submitted

//Setup Server side Validations
//Please note that the element name is case sensitive
$validator = new FormValidator();
$validator->addValidation("name","req","Please fill in your name");
$validator->addValidation("email","email","Please enter a valid email address");
$validator->addValidation("email","req","Please fill in your email");
$validator->addValidation("Nearest_City","req","Please select your nearest city");

//Then validate the form
if($validator->ValidateForm())
{
//If the validations succeeded, proceed with form processing
if($formproc->ProcessForm())
{
$formproc->RedirectToURL("http://www.inscohvac.com/");
}
}
else
{
//Validations failed. Display Errors.
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
$validation_errors .= "$inp_err</p>\n";
}
}
}//if
$disp_name  = isset($_POST["name"])?$_POST["name"]:"";
$disp_email = isset($_POST["email"])?$_POST["email"]:"";
$disp_message = isset($_POST["message"])?$_POST["message"]:"";
$disp_Nearest_City = isset($_POST["Nearest City"])?$_POST["Nearest City"]:"";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type=”text/css”>
#flytrap { display: none !important; }
</style>
<link rel="stylesheet" type="text/css" href="css/normalize.css">
<link rel="stylesheet" type="text/css" href="css/style.css">

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="php-form-validator/samples/3-with-client-side-validations/scripts/gen_validatorv31.js"></script>
<script type=”text/javacript”>
// jQuery is already loaded
$(‘#ccsfg’).submit(function(){
if ( $(‘#flytrap’).val() != ” ) return false; }
});
</script>

</head>

<body>
<div class="row">
<div class="small-3 medium-4 large-4 small-centered columns large-centered columns">
<form id="ccsfg" name="ccsfg" method="post" action="<?php echo $formproc->GetSelfScript(); ?>" accept-charset="UTF-8">

<h4>Registration Form</h4><p>To join our mailing list, please complete the information below and click the "Join Insco Mailing List" button.</p>

<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
<input type='text'  class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />

<div>
<span class="help"><?php echo $formproc->GetErrorMessage(); ?></span>
<span class="help"><?php echo $validation_errors; ?></span>
</div>

<!-- ########## First Name ########## -->
<label for="FirstName">First Name*</label>
<input type="text" name="FirstName" value="<?php echo htmlentities($disp_name) ?>" maxlength="50" id="FirstName" />

<!-- ########## Last Name ########## -->
<label for="LastName">Last Name*</label>
<input type="text" name="LastName" value="<?php echo htmlentities($disp_name) ?>" maxlength="50" id="LastName" />

<!-- ########## Email Address ########## -->
<label for="EmailAddress">Email Address*</label>
<input type="text" name="EmailAddress" value="<?php echo htmlentities($disp_email) ?>" maxlength="100" id="EmailAddress" />

<!-- ########## Street Address ########## -->
<label for="Addr1">Street Address</label>
<input type="text" name="Addr1" value="" id="Addr1" />

<!-- ########## City ########## -->
<label for="City">City</label>
<input type="text" name="City" value="" id="City" />

<!-- ########## State/Province (US/Canada) ########## -->
<label for="StateCode">State/Province (US/Canada)</label>
<select name="StateCode">
<option value="">- Please Select</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AB">Alberta</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="AA">Armed Forces Americas</option>
<option value="AE">Armed Forces Europe</option>
<option value="AP">Armed Forces Pacific</option>
<option value="BC">British Columbia</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NB">New Brunswick</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NL">Newfoundland and Labrador</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="NT">Northwest Territories</option>
<option value="NS">Nova Scotia</option>
<option value="NU">Nunavut</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="ON">Ontario</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="PE">Prince Edward Island</option>
<option value="QC">Quebec</option>
<option value="RI">Rhode Island</option>
<option value="SK">Saskatchewan</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
<option value="YT">Yukon Territory</option>
</select><!-- ########## Zip/Postal Code ########## -->
<label for="PostalCode">Zip Code</label>
<input type="text" name="PostalCode" value="" id="PostalCode" />

<!-- ########## Company Name ########## -->
<label for="CompanyName">Company Name</label>
<input type="text" name="CompanyName" value="" id="CompanyName" />

<!-- ########## Work Phone ########## -->
<label for="WorkPhone">Work Phone</label>
<input type="text" name="WorkPhone" value="" id="WorkPhone" />

<!-- ########## Contact Lists ########## -->

<label for="Nearest_City">Nearest City*</label>

<div class="descriptor"><h5><small><em>Please select the closest city to you or the location of your nearest Insco store.</em></small></h5></div>

<select name="Nearest_City" value="<?php echo htmlentities($disp_name) ?>" >

<option value="">- Please Select</option>
<option value="City-Abilene" name="Lists[]" id="list_City-Abilene"><label for="list_City-Abilene">Abilene</label></option>
<option value="City-Arlington" name="Lists[]" id="list_City-Arlington"><label for="list_City-Arlington">Arlington</label></option>
<option value="City-Austin" name="Lists[]" id="list_City-Austin"><label for="list_City-Austin">Austin</label></option>
<option value="City-Brownsville" name="Lists[]" id="list_City-Brownsville"><label for="list_City-Brownsville">Brownsville</label></option>
<option value="City-Corpus Christi" name="Lists[]" id="list_City-Corpus Christi"><label for="list_City-Corpus Christi">Corpus Christi</label></option>
<option value="City-Dallas" name="Lists[]" id="list_City-Dallas"><label for="list_City-Dallas">Dallas</label></option>
<option value="City-Del Rio" name="Lists[]" id="list_City-Del Rio"><label for="list_City-Del Rio">Del Rio</label></option>
<option value="City-Denton" name="Lists[]" id="list_City-Denton"><label for="list_City-Denton">Denton</label></option>
<option value="City-DeSoto" name="Lists[]" id="list_City-DeSoto"><label for="list_City-DeSoto">DeSoto</label></option>
<option value="City-El Paso" name="Lists[]" id="list_City-El Paso"><label for="list_City-El Paso">El Paso</label></option>
<option value="City-Fort Worth" name="Lists[]" id="list_City-Fort Worth"><label for="list_City-Fort Worth">Fort Worth</label></option>
<option value="City-Garland" name="Lists[]" id="list_City-Garland"><label for="list_City-Garland">Garland</label></option>
<option value="City-Georgetown" name="Lists[]" id="list_City-Georgetown"><label for="list_City-Georgetown">Georgetown</label></option>
<option value="City-Grapevine" name="Lists[]" id="list_City-Grapevine"><label for="list_City-Grapevine">Grapevine</label></option>
<option value="City-Harlingen" name="Lists[]" id="list_City-Harlingen"><label for="list_City-Harlingen">Harlingen</label></option>
<option value="City-Houston" name="Lists[]" id="list_City-Houston"><label for="list_City-Houston">Houston</label></option>
<option value="City-Kerrville" name="Lists[]" id="list_City-Kerrville"><label for="list_City-Kerrville">Kerrville</label></option>
<option value="City-Laredo" name="Lists[]" id="list_City-Laredo"><label for="list_City-Laredo">Laredo</label></option>
<option value="City-Lubbock" name="Lists[]" id="list_City-Lubbock"><label for="list_City-Lubbock">Lubbock</label></option>
<option value="City-McAllen" name="Lists[]" id="list_City-McAllen"><label for="list_City-McAllen">McAllen</label></option>
<option value="City-New Braunfels" name="Lists[]" id="list_City-New Braunfels"><label for="list_City-New Braunfels">New Braunfels</label></option>
<option value="City-Oklahoma City" name="Lists[]" id="list_City-Oklahoma City"><label for="list_City-Oklahoma City">Oklahoma City</label></option>
<option value="City-San Angelo" name="Lists[]" id="list_City-San Angelo"><label for="list_City-San Angelo">San Angelo</label></option>
<option value="City-San Antonio" name="Lists[]" id="list_City-San Antonio"><label for="list_City-San Antonio">San Antonio</label></option>
<option value="City-Tyler" name="Lists[]" id="list_City-Tyler"><label for="list_City-Tyler">Tyler</label></option>
<option value="City-Victoria" name="Lists[]" id="list_City-Victoria"><label for="list_City-Victoria">Victoria</label></option>
<option value="City-Wichita Falls" name="Lists[]" id="list_City-Wichita Falls"><label for="list_City-Wichita Falls">Wichita Falls</label></option>

</select>

<label id="flytrap" for="flytrap">If you are human, leave this blank</label><input type="text" name="flytrap" id="flytrap" />

<h4><small><em>* Indicates a requird field.</em></small></h4>

<input type="submit" name="signup" id="signup" value="Join Insco Mailing List" class="button radius" />

</form>
</div>
</div>

<script type='text/javascript'>
// <![CDATA[

var frmvalidator  = new Validator("ccsfg");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");

frmvalidator.addValidation("email","req","Please provide your email address");

frmvalidator.addValidation("email","email","Please provide a valid email address");

frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");
// ]]>
</script>

</body>
</html>

0

Решение

Возможно, если бы вы были более осторожны в своих соглашениях об именах:

$disp_Nearest_City = isset($_POST["Nearest City"])?$_POST["Nearest City"]:"";
^^^^^^^^^^^^---spot the difference
<select name="Nearest_City" value="<?php echo htmlentities($disp_name) ?>" >
^^^^^^^^^^^^---spot the difference
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]