Reputation: 239
I am creating a registration form, but I have got one problem only... why can they pass the validation with an invalid entry? The only thing (which I know) is that they can pass it without choosing a country!
Here's my HTML/PHP code:
<table border="0" width="100%">
<form action="" method="post">
<tr><td><label>Username:<span class="requiredfield">*</span></label></td><td width="200"><input type="text" name="myusername" id="myusername" maxlength="15" style="background-color:transparent;" /></td></tr>
<tr><td><label>Summoner Name (League of Legends):<span class="requiredfield">*</span></label></td><td><input type="text" name="mysummonername" id="mysummonername" maxlength="30" /></td></tr>
<tr><td><label>Password:<span class="requiredfield">*</span></label></td><td><input type="password" name="mypassword" id="mypassword" maxlength="15" /></td></tr>
<tr><td><label>Retype Password:<span class="requiredfield">*</span></label></td><td><input type="password" name="myrpassword" id="myrpassword" maxlength="15" /></td></tr>
<tr><td><label>E-mail:<span class="requiredfield">*</span></label></td><td><input type="text" name="myemail" id="myemail" maxlength="65" /></td></tr>
<tr><td><label>Retype E-mail:<span class="requiredfield">*</span></label></td><td><input type="text" name="myremail" id="myremail" maxlength="65" /></td></tr>
<tr><td width="300"><br />Please note that we only need this information because we'll send out prices to the winners of our tournaments.</td></tr>
<tr><td><label>First Name:<span class="requiredfield">*</span></label></td><td><input type="text" name="myfname" id="myfname" maxlength="20" /></td></tr>
<tr><td><label>Last Name:<span class="requiredfield">*</span></label></td><td><input type="text" name="mylname" id="mylname" maxlength="30" /></td></tr>
<tr><td><label>Select Country:<span class="requiredfield">*</span></label></td><td><select name="mycountry" id="mycountry">
<option value="-choose-">-Choose-</option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
...
</select></td></tr>
<tr><td><label>Postal Code:</label></td><td><input type="text" name="myzipcode" id="myzipcode" maxlength="9" /></td></tr>
<tr><td><label>City:</label></td><td><input type="text" name="mycity" id="mycity" maxlength="60" /></td></tr>
<tr><td><label>Street:</label></td><td><input type="text" name="mystreet" id="mystreet" maxlength="50" /></td></tr>
<tr><td><label>Telephone Number:</label></td><td><input type="text" name="myphonenumber" id="myphonenumber" maxlength="15" /></td></tr>
<!--<tr><td><label>Invite Code<span style="color:#F00;">*</span>:</label></td><td><input type="text" name="myinvcode" id="myinvcode" /></td></tr>-->
<!-- <tr><td colspan="2" style="height: 20px;"></td></tr> -->
<tr><td width="300"><br />This information will be used later on when you have lost your password and need to retrieve it again.</td></tr>
<tr><td><label>Secret Question:<span class="requiredfield">*</span></label></td><td><input type="text" name="myscrtquestion" id="myscrtquestion" maxlength="50" /></td></tr>
<tr><td><label>Secret Answer:<span class="requiredfield">*</span></label></td><td><input type="text" name="myscrtanswer" id="myscrtanswer" maxlength="50" /></td></tr>
<tr><td></td><td><span class="button"><input type="submit" name="submitregister" value="Register" /></span></td></tr>
<tr><td colspan="2">By pressing "Register" you agree to the <a href='termsofuse.php'>terms of use</a> and our <a href='privacypolicy.php'>privacy policy</a>.</td></tr>
</form>
<?php
if (isset($_POST['submitregister'])) {
ob_start();
include 'config.php'; // Connect to Database
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mysummonername=$_POST['mysummonername'];
$myfname=$_POST['myfname'];
$mylname=$_POST['mylname'];
$mypassword=$_POST['mypassword'];
$myrpassword=$_POST['myrpassword'];
$myemail=$_POST['myemail'];
$myremail=$_POST['myremail'];
// $myinvcode=$_POST['myinvcode'];
$myphonenumber=$_POST['myphonenumber'];
$mycountry=$_POST['mycountry'];
$myzipcode=$_POST['myzipcode'];
$mycity=$_POST['mycity'];
$mystreet=$_POST['mystreet'];
$myscrtquestion=$_POST['myscrtquestion'];
$myscrtanswer=$_POST['myscrtanswer'];
// check to make sure required fields are entered
if ($myusername == '' || $mysummonername == '' || $myfname == '' || $mylname == '' || $mypassword == '' || $myemail == '' || $mycountry == "-Choose-" || $myscrtquestion == '' || $myscrtanswer == '') {
echo "<span style='color:#F00;'>Please fill in all the required fields. All the fields which are marked with a <span style='color:#0076c9;'>*</span> are required fields!</span>";
Why can you still pass this when you havn't chosen a country? I can't figure it out!
Upvotes: 0
Views: 165
Reputation: 1669
Because the option --choose-- is all lowercase in your html code and you're testing for --Choose-- with a capital 'C' in your PHP.
Ross
Upvotes: 0
Reputation: 1533
Either change your <option value="-choose-">-Choose-</option>
to <option value="-Choose-">-Choose-</option>
, or leave the value of "-Choose-" option blank and check for empty "mycountry": $mycountry == ""
Upvotes: 1
Reputation:
As the previous answers stated, you are checking for -Choose
when you need -choose-
. A way to debug for this problem in the future is to do a:
print_r($_POST);
Which shows you all the POST data, so you can see what has been posted.
To see if they haven't chosen a country see if the relevant post data is null or not.
Upvotes: 1
Reputation: 2352
Check the the casing of latters. In Option you have -Choose- but you compare it to the -choose-
Upvotes: 1
Reputation: 54729
Because the value for country that's being passed when they leave it on the choose value is -choose-
, but you are checking for $mycountry == "-Choose-"
. Those two values are not equal.
You should probably develop a better way of checking if the country is correct though. Anyone could copy your form and point it back to your page, entering whatever value they like for that field and you're only checking to make sure it's not -choose-
.
Upvotes: 3