如果没有选择任何内容,如何让我的选择框显示错误?目前,它在提交时没有给我任何错误,因为" select"字段是字段框中的第一个项目,因此它暗示它已填写。
这仅适用于性别国家和州。
include 'connection.php';
if ($_POST['submit']=="Sign Up") {
if (!$_POST['firstname']) $error.="<br />Please enter your first name";
else {
if (!preg_match("/^[- '\p{L}]+$/u", $_POST['firstname'])) $error.="<br />First name may only contain letters";
}
if (!$_POST['lastname']) $error.="<br />Please enter your last name";
else {
if (!preg_match("/^[- '\p{L}]+$/u", $_POST['lastname'])) $error.="<br />Last name may only contain letters";
}
if (!$_POST['email']) $error.="<br />Please enter your email";
else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br />Please enter a valid email address";
if (!$_POST['password']) $error.="<br />Please enter your password";
else {
if (strlen($_POST['password'])<8) $error.="<br />Please enter a password with at least 8 characters";
if (!preg_match('`[A-Z]`', $_POST['password'])) $error.="<br />Please enter at least 1 capital letter";
}
if ($_POST['password'] !== $_POST['confirmpassword'])
$error.="<br />Your passwords do not match.";
if (!$_POST['gender']) $error.="<br />Please enter your gender";
if (!$_POST['country']) $error.="<br />Please enter your country";
if (!$_POST['s
tate&#39;])$ error。=&#34;
请输入您的州&#34;;
形式:
<form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label class="col-md-5 control-label">First Name*</label>
<div class="col-md-2">
<input type="text" name="firstname" id="firstname" class="form-control input-md" value="<?php echo addslashes ($_POST['firstname']); ?>" />
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Last Name*</label>
<div class="col-md-2">
<input type="text" name="lastname" class="form-control input-md" value="<?php echo addslashes ($_POST['lastname']); ?>" />
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Email*</label>
<div class="col-md-2">
<input type="email" name="email" class="form-control input-md" value="<?php echo addslashes ($_POST['email']); ?>" />
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Password*</label>
<div class="col-md-2">
<input type="password" name="password" class="form-control input-md" />
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Confirm Password*</label>
<div class="col-md-2">
<input type="password" name="confirmpassword" class="form-control input-md" />
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Gender*</label>
<div class="col-md-1">
<select id="gender" name="gender" class="form-control">
<option>Select</option>
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Country*</label>
<div class="col-md-2">
<select id="country" name ="country" class="form-control"></select>
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">State*</label>
<div class="col-md-2">
<select name ="state" id ="state" class="form-control"></select>
<script language="javascript">
populateCountries("country", "state");
</script>
</div>
</div>
<div class="text-center">
<input type="submit" name="submit" class="btn btn-kani btn-lg" value="Sign Up"/>
</div>
</form>
JS档案
// Countries
var country_arr = new Array("Afghanistan", "Albania", "Algeria", "American Samoa", "Angola", "Anguilla", "Antartica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Ashmore and Cartier Island", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", Georgia and South Sandwich Islands", "Spain", "Spratly Islands", "Sri Lanka", "Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Tobago", "Toga", "Tokelau", "Tonga", "Trinidad", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "Uruguay", "USA", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands", "Wales", "Wallis and Futuna", "West Bank", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe");
// States
var s_a = new Array();
s_a[0]="";
s_a[1]="Badakhshan|Badghis|Baghlan|Balkh|Bamian|Farah|Faryab|Ghazni|Ghowr|Helmand|Herat|Jowzjan|Kabol|Kandahar|Kapisa|Konar|Kondoz|Laghman|Lowgar|Nangarhar|Nimruz|Oruzgan|Paktia|Paktika|Parvan|Samangan|Sar-e Pol|Takhar|Vardak|Zabol";
s_a[2]="Berat|Bulqize|Delvine|Devoll (Bilisht)|Diber (Peshkopi)|Durres|Elbasan|Fier|Gjirokaster|Gramsh|Has (Krume)|Kavaje|Kolonje (Erseke)|Korce|Kruje|Kucove|Kukes|Kurbin|Lezhe|Librazhd|Lushnje|Malesi e Madhe (Koplik)|Mallakaster (Ballsh)|Mat (Burrel)|Mirdite (Rreshen)|Peqin|Permet|Pogradec|Puke|Sarande|Shkoder|Skrapar (Corovode)|Tepelene|Tirane (Tirana)|Tirane (Tirana)|Tropoje (Bajram Curri)|Vlore";
s_a[3]="Adrar|Ain Defla|Ain Temouchent|Alger|Annaba|Batna|Bechar|Bejaia|Biskra|Blida|Bordj Bou Arreridj|Bouira|Boumerdes|Chlef|Constantine|Djelfa|El Bayadh|El Oued|El Tarf|Ghardaia|Guelma|Illizi|Jijel|Khenchela|Laghouat|M'Sila|Mascara|Medea|Mila|Mostaganem|Naama|Oran|Ouargla|Oum el Bouaghi|Relizane|Saida|Setif|Sidi Bel Abbes|Skikda|Souk Ahras|Tamanghasset|Tebessa|Tiaret|Tindouf|Tipaza|Tissemsilt|Tizi Ouzou|Tlemcen";
s_a[4]="Eastern|Manu'a|Rose Island|Swains Island|Western";
s_a[5]="Andorra la Vella|Bengo|Benguela|Bie|Cabinda|Canillo|Cuando Cubango|Cuanza Norte|Cuanza Sul|Cunene|Encamp|Escaldes-Engordany|Huambo|Huila|La Massana|Luanda|Lunda Norte|Lunda Sul|Malanje|Moxico|Namibe|Ordino|Sant Julia de Loria|Uige|Zaire";
s_a[6]="Anguilla";
s_a[7]="Antartica";
s_a[8]="Barbuda|Redonda|Saint George|Saint John|Saint Mary|Saint Paul|Saint Peter|Saint Philip";
s_a[9]="Antartica e Islas del Atlantico Sur|Buenos Aires|Buenos Aires Capital Federal|Catamarca|Chaco|Chubut|Cordoba|Corrientes|Entre Rios|Formosa|Jujuy|La Pampa|La Rioja|Mendoza|Misiones|Neuquen|Rio Negro|Salta|San Juan|San Luis|Santa Cruz|Santa Fe|Santiago del Estero|Tierra del Fuego|Tucuman";
s_a[10]="Aragatsotn|Ararat|Armavir|Geghark'unik'|Kotayk'|Lorri|Shirak|Syunik'|Tavush|Vayots' Dzor|Yerevan";
s_a[11]="Aruba";
s_a[12]="Ashmore and Cartier Island";
s_a[13]="Australian Capital Territory|New South Wales|Northern Territory|Queensland|South Australia|Tasmania|Victoria|Western Australia";
s_a[247]="West Bank";
s_a[248]="Western Sahara";
s_a[249]="'Adan|'Ataq|Abyan|Al Bayda'|Al Hudaydah|Al Jawf|Al Mahrah|Al Mahwit|Dhamar|Hadhramawt|Hajjah|Ibb|Lahij|Ma'rib|Sa'dah|San'a'|Ta'izz";
s_a[250]="Kosovo|Montenegro|Serbia|Vojvodina";
s_a[251]="Central|Copperbelt|Eastern|Luapula|Lusaka|North-Western|Northern|Southern|Western";
s_a[252]="Bulawayo|Harare|ManicalandMashonaland Central|Mashonaland East|Mashonaland West|Masvingo|Matabeleland North|Matabeleland South|Midlands";
function populateStates( countryElementId, stateElementId ){
var selectedCountryIndex = document.getElementById( countryElementId ).selectedIndex;
var stateElement = document.getElementById( stateElementId );
stateElement.length=0; // Fixed by Julian Woods
stateElement.options[0] = new Option('Select State','');
stateElement.selectedIndex = 0;
var state_arr = s_a[selectedCountryIndex].split("|");
for (var i=0; i<state_arr.length; i++) {
stateElement.options[stateElement.length] = new Option(state_arr[i],state_arr[i]);
}
}
function populateCountries(countryElementId, stateElementId){
// given the id of the <select> tag as function argument, it inserts <option> tags
var countryElement = document.getElementById(countryElementId);
countryElement.length=0;
countryElement.options[0] = new Option('Select Country','-1');
countryElement.selectedIndex = 0;
for (var i=0; i<country_arr.length; i++) {
countryElement.options[countryElement.length] = new Option(country_arr[i],country_arr[i]);
}
// Assigned all countries. Now assign event listener for the states.
if( stateElementId ){
countryElement.onchange = function(){
populateStates( countryElementId, stateElementId );
};
}
}
答案 0 :(得分:0)
应该是这样的:
<select id="gender" name="gender" class="form-control">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
评估线将是:
if((isset($_POST['gender']) && empty($_POST['gender'])) || (!isset($_POST['gender']))) $error.="<br />Please enter your gender";
您的选择默认值将为空。你可以在那时检查空。
作为旁注,要跳过所有这些PHP验证,使用jQuery验证将节省95%的PHP。