[RESOLVED] Strange Problem Regarding Form Validation
Posted: Wed Aug 09, 2006 6:23 am
OK, so this is my 3rd post, and my third question. In my previous question I had a working php file that would collect the data from a for, validate it and, if certain criteria where met, send the information to the database. I wanted to know how to display error messages on the same page as the form. Well, thanks to a couple of members here I was able to fix that, but I've now got another, rather strange problem.
The initial PHP file worked fine, and would display error messages if the user; Entered a password fewer than 6 characters, their passwords didn't match, they left a field blank or if their user/email was already in the database. Now, I have used the exact same code in the new file, and things have gotten rather strange.
At first, it would recreate the HTML form over and over again when 'Submit' was hit and an error occurred, it would display the error, then recreate the HTML form twice if the user/email already existed. I managed to get around that by adding some statements in. When I tried it with teh dies in, it worked...or so I thought...I found that now it allowed me to enter in blank entries in some of the fields. Remember, I am using the EXACT validation code from my initial PHP file, which worked exactly how I wanted it to.
Now, I don't know how this 'fixed' it - and by 'fixed' I mean, resolved the previous problem, but created a new one - but I switched the order of my if statements around a bit, so that the validation for blank fields came before the any other validation checks. Now, I tried submitting a blank field, and I got an error telling me that I'd entered one, so, I thought I'd fixed it AGAIN, however, even if I try entering text in the box, I still get the 'Enter Business Name' error which I coded if the Business Name field was left blank.
I just don't know what could be going wrong here. The code worked perfectly when it was in the original PHP file, and all that I've added is the function, which contains the ode for printing my form, a couple of die; statements, and moved the if statements around a bit, why are things suddenly going wrong?
Anyway, I'll post the validation part of my code here:
Sorry to be posting questions all the time, I'm not one of those people who joins forums just to ask question after question, I'd like to contribute to the community two, but it seems the only way to do se here is to answer questions, or ask them...and I think we've estaclished I wouldn't be the greatest 'answerer', but I've only been coding in PHP for a week or so, so don't beat me with a big stick if the above code is pretty naff 
The initial PHP file worked fine, and would display error messages if the user; Entered a password fewer than 6 characters, their passwords didn't match, they left a field blank or if their user/email was already in the database. Now, I have used the exact same code in the new file, and things have gotten rather strange.
At first, it would recreate the HTML form over and over again when 'Submit' was hit and an error occurred, it would display the error, then recreate the HTML form twice if the user/email already existed. I managed to get around that by adding some
Code: Select all
die;Now, I don't know how this 'fixed' it - and by 'fixed' I mean, resolved the previous problem, but created a new one - but I switched the order of my if statements around a bit, so that the validation for blank fields came before the any other validation checks. Now, I tried submitting a blank field, and I got an error telling me that I'd entered one, so, I thought I'd fixed it AGAIN, however, even if I try entering text in the box, I still get the 'Enter Business Name' error which I coded if the Business Name field was left blank.
I just don't know what could be going wrong here. The code worked perfectly when it was in the original PHP file, and all that I've added is the
Code: Select all
ShowForm();Anyway, I'll post the validation part of my code here:
Code: Select all
//check for errors
//check if user/email exists
$userquery = mysql_query("SELECT username FROM southport_businesses WHERE username ='$busname'");
$useravailable = mysql_num_rows($userquery);
$emailquery= mysql_query("SELECT email FROM southport_businesses WHERE email ='$email'");
$emailavailable = mysql_num_rows($emailquery);
if($useravailable > 0)
{
print('Username is taken, please select another');
ShowForm();
die;
}
if($emailavailable > 0)
{
print('E-mail has already been registered');
ShowForm();
die;
}
if($_POST['password1']== "")
{
print("Please fill in password");
ShowForm();
die;
}
if($_POST['busname'] == "");
{
print('Please fill in business name');
ShowForm();
die;
}
if($_POST['password2'] == "")
{
print('Please fill in password');
ShowForm();
die;
}
if($_POST['email'] == "")
{
print('Please fill in email');
ShowForm();
die;
}
if($_POST['PostCode']=="")
{
print('Please fill in postcode');
ShowForm();
die;
}
if($_POST['cont_name']=="")
{
print('Please fill in contact name');
ShowForm();
die;
}
if($_POST['contactnumber']=="")
{
print('Please fill in contact number');
ShowForm();
die;
}
if($_POST['HouseName']=="")
{
print('Please fill in House Number');
ShowForm();
die;
}
if($_POST['HouseName']=="")
{
print('Please fill in road');
ShowForm();
die;
}
if($_POST['HouseName']=="")
{
print('Please fill in town');
ShowForm();
die;
}
//now check passwords match
if($password1 != $password2)
{
print("Passwords don't match!");
ShowForm();
die;
}
//check if passwords meet a minimum length
if(strlen($password1) < 6)
{
print('Password must be 6 characters or longer');
ShowForm();
die;
}
else
{
//ADD DATA TO TABLE