chocopi Posted May 11, 2007 Share Posted May 11, 2007 Well i am just trying to create a basic registration form with some sort of validation. I'm not a complete n00b in coding, but I am new to php, so please bare with me. My form that I use has four fields: email, username, password, confirm password. I would like some validation on all of these fields. My first main problem is that the validation occurs when the form is loaded, so instantly an error comes up telling the user they have not filled in the field. My second problem is that i could not get an validation to work on the password or email. The third problem is that when the submit button is pressed all of the fields are wiped clean. So if anyone could help i would need validation for all of the fields, to only allow the validation to occur when the button is pressed and so that the fields arent wiped clean when the field is submitted. Here is the code: <? $title = 'Registration'; require_once ('page_header.php'); $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); ?> // HTML code here (I couldnt be bothered to put this here as it is irrelevant. <form name="login" method="post" action="<?php echo $PHP_SELF;?>"> <input type="submit" name="submit" value="Submit"> <? $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); $checkemail = mysql_query("SELECT email FROM users WHERE email='$email'"); $email_exist = mysql_num_rows($checkemail); print("<font color=\"#ff0000\">\n"); if (strlen($username) < 4){ die ("Your username must be longer than four characters."); } else if ($username_exist > 0){ die ("I'm sorry but the username " . $_POST['username'] . " has already been taken. Please pick another one."); } else if ($email_exist > 0){ echo ("Your are only allowed one account per email."); exit(); } else if (!$_POST['email']){ echo ("Please fill out your email, so that your account can be activated."); exit(); } else { print("</font>\n"); ?> Please delete the validation as it is a jumble of what i have written and taken. The database is MySQL and the is table is 'users' which contains the field username, password and email. Thanks for your help ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/50964-solved-form-various-problems/ Share on other sites More sharing options...
micah1701 Posted May 11, 2007 Share Posted May 11, 2007 My first main problem is that the validation occurs when the form is loaded, so instantly an error comes up telling the user they have not filled in the field. only run the validation part of the script when the form has been submitted: <?php if($_POST){ $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); $checkemail = mysql_query("SELECT email FROM users WHERE email='$email'"); $email_exist = mysql_num_rows($checkemail); print("<font color=\"#ff0000\">\n"); if (strlen($username) < 4){ die ("Your username must be longer than four characters."); } else if ($username_exist > 0){ die ("I'm sorry but the username " . $_POST['username'] . " has already been taken. Please pick another one."); } else if ($email_exist > 0){ echo ("Your are only allowed one account per email."); exit(); } else if (!$_POST['email']){ echo ("Please fill out your email, so that your account can be activated."); exit(); } else { print("</font>\n"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50964-solved-form-various-problems/#findComment-250735 Share on other sites More sharing options...
chocopi Posted May 11, 2007 Author Share Posted May 11, 2007 Cheers, thanks mate One down two to go Quote Link to comment https://forums.phpfreaks.com/topic/50964-solved-form-various-problems/#findComment-250743 Share on other sites More sharing options...
chocopi Posted May 11, 2007 Author Share Posted May 11, 2007 ok i was able to sort out and stop the fields from being erased. such a n00bish error. me => all i had to do was add $username and $email into the value of the input boxes. DOY. but that did mean that i had to put all of my html into the "print" tag, but oh welly. So if anyone could help me with the validation it would be greatly appreciated. Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/50964-solved-form-various-problems/#findComment-250778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.