Chris_Mc_1985 Posted September 24, 2009 Share Posted September 24, 2009 I am trying to make a form validated by php with no luck so far. here is what I am using. <?php require_once("validation.php"); ?> <html> <body> <font color="black"> <b><u>Contact</b></u> <b><u>Me</b></u> <br> <br> <?if( isset($_POST['submit']) || !validateName($_POST['name']) || !validateSurname($_POST['surname']) || !validatePassword($_POST['pass'], $_POST['pass2']) || !validateContactnumber($_POST['contactnumber']) || !validateUsername($_POST['username']) ):?> <div id="error"> <ul> <?if(!validateName($_POST['name'])):?> <li><strong>Invalid Name:</strong> 3 letters or more needed!</li> <?endif?> <?if(!validateSurname($_POST['surname'])):?> <li><strong>Invalid Surname:</strong> 3 letters or more needed!</li> <?endif?> <?if(!validateEmail($_POST['email'])):?> <li><strong>Invalid E-mail:</strong> Please type a valid e-mail </li> <?endif?> <?if(!validateUsername($_POST['username'])):?> <li><strong>Invalid Username:</strong> 3 letters or more needed!</li> <?endif?> <?if(!validatePassword($_POST['pass'], $_POST['pass2'])):?> <li><strong>Passwords are invalid:</strong> Passwords don't match!</li> <?endif?> <?if(!validateContactnumber($_POST['contactnumber'])):?> <li><strong>Ivalid contact number</strong></li> <?endif?> </ul> </div> <? elseif ( isset($_POST['submit']) ):?> <div id="error" class="valid"> <ul> <li><strong>Congratulations!</strong> All fields are OK </li> </ul> </div> <?endif?> <form name="register" method="post" action="index.php?page=addmember" /> <label>Firstname:<input name="firstname" type="text" size="10"/></label> <label>Surname:<input name="surname" type="text" size="10"/></label> <label>Contact Number:<input name="contactnumber" type="text" size="10"/></label> <label>Email Address:<input name="email" type="text" size="20"/></label> <label>Username:<input name="username" type="text" size="10"/></label> <label>Password:<input name="pass" type="text" size="10"/></label> <label>Retype Password:<input name="pass2" type="text" size="10"/></label> <input type="submit" name="submit" value="submit"/> <input type="reset" name="reset" value="reset"/> </form> Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/ Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Hi Chris_Mc_1985, You have an if statement which is checking for Submit being set, you also have an elseif statement doing the same thing. Also, you are checking for $_POST['name'] which doesn't exist, I assume you mean $_POST['firstname']? It would be better to change the form to something like: <?php require_once("validation.php"); ?> <html> <body> <font color="black"> <b><u>Contact</b></u> <b><u>Me</b></u> <br> <br> <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!validateName($_POST['firstname'])) { $error .= '<li><strong>Invalid Name:</strong> 3 letters or more needed!</li>'; } if(!validateSurname($_POST['surname'])) { $error .= '<li><strong>Invalid Surname:</strong> 3 letters or more needed!</li>'; } if(!validateEmail($_POST['email'])) { $error .= '<li><strong>Invalid E-mail:</strong> Please type a valid e-mail </li>'; } if(!validateUsername($_POST['username'])) { $error .= '<li><strong>Invalid Username:</strong> 3 letters or more needed!</li>'; } if(!validatePassword($_POST['pass'], $_POST['pass2'])) { $error .= '<li><strong>Passwords are invalid:</strong> Passwords don\'t match!</li>'; } if(!validateContactnumber($_POST['contactnumber'])) { $error .= '<li><strong>Invalid contact number</strong></li>'; } if(!$error) { $content .= '<div id="error" class="valid">'; $content .= '<ul>'; $content .= '<li><strong>Congratulations!</strong> All fields are OK </li>'; $content .= '</ul>'; $content .= '</div>'; echo $content; } else { $content .= '<div id="error">'; $content .= '<ul>'; $content .= ''.$error.''; $content .= '</ul>'; $content .= '</div>'; echo $content; } } ?> <form name="register" method="post" action="index.php?page=addmember" /> <label>Firstname:<input name="firstname" type="text" size="10"/></label> <label>Surname:<input name="surname" type="text" size="10"/></label> <label>Contact Number:<input name="contactnumber" type="text" size="10"/></label> <label>Email Address:<input name="email" type="text" size="20"/></label> <label>Username:<input name="username" type="text" size="10"/></label> <label>Password:<input name="pass" type="text" size="10"/></label> <label>Retype Password:<input name="pass2" type="text" size="10"/></label> <input type="submit" name="submit" value="submit"/> <input type="reset" name="reset" value="reset"/> </form> This script stores the errors in a variable, if errors occur output them, if not show a success message. You haven't posted the contents of "validation.php" which you're including at the top of this script so can't comment on whether that code is correct or not. I hope this helps. Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924071 Share on other sites More sharing options...
Chris_Mc_1985 Posted September 24, 2009 Author Share Posted September 24, 2009 <?php function validateName($name){ if(strlen($name) < 4) return false; else return true; } function validateSurname($surname){ if(strlen($surname) < 4) return false; else return true; } function validateEmail($email){ return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email); } function validatePassword($pass, $pass2) { if(strpos($pass, ' ') !== false) return false; return $pass == $pass2 && strlen($pass) > 5; } function validateUsername($username){ if(strlen($username) < 5) return false; else return true; } fuction validateContactnumber($contactnumber){ if(strlen($contactnumber) < 5) return false; else return true; } ?> Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924097 Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Did you try the code I posted? Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924111 Share on other sites More sharing options...
Chris_Mc_1985 Posted September 24, 2009 Author Share Posted September 24, 2009 Did you try the code I posted? Yes sorry i did, this is what i got back from the code you posted Parse error: syntax error, unexpected T_STRING in validation.php on line 33 Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924115 Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Ok, thanks, that helps. The problem is that there is a typo in your validation.php file, the last function is spelled "fuction", change this to "function" and try again. Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924118 Share on other sites More sharing options...
Chris_Mc_1985 Posted September 24, 2009 Author Share Posted September 24, 2009 thanks for the help bricktop. that gets rid of the errors but the validation still does not work. the page just posts instead of showing the error messages Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924123 Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 OK, I've combined the validation into the main form, give the below a try: <html> <body> <font color="black"> <b><u>Contact</b></u> <b><u>Me</b></u> <br> <br> <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!$_POST['firstname'] || strlen($_POST['firstname']) < 4) { $error .= '<li><strong>Invalid Name:</strong> 3 letters or more needed!</li>'; } if(!$_POST['surname'] || strlen($_POST['surname']) < 4) { $error .= '<li><strong>Invalid Surname:</strong> 3 letters or more needed!</li>'; } if(!$_POST['email'] || !ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $_POST['email'])) { $error .= '<li><strong>Invalid E-mail:</strong> Please type a valid e-mail </li>'; } if(!$_POST['username'] || strlen($_POST['username']) < 5) { $error .= '<li><strong>Invalid Username:</strong> 5 letters or more needed!</li>'; } if(!$_POST['pass'] || !$_POST['pass2']) { $error .= '<li><strong>Please enter a password!</li>'; } elseif(!$pass == $pass2 && strlen($pass) > 5) { $error .= '<li><strong>Passwords are invalid:</strong> Passwords don\'t match!</li>'; } if(!$_POST['contactnumber'] || strlen($_POST['contactnumber']) < 5) { $error .= '<li><strong>Invalid contact number</strong></li>'; } if(!$error) { $content .= '<div id="error" class="valid">'; $content .= '<ul>'; $content .= '<li><strong>Congratulations!</strong> All fields are OK </li>'; $content .= '</ul>'; $content .= '</div>'; echo $content; } else { $content .= '<div id="error">'; $content .= '<ul>'; $content .= ''.$error.''; $content .= '</ul>'; $content .= '</div>'; echo $content; } } ?> <form name="register" method="post" action="index.php?page=addmember" /> <label>Firstname:<input name="firstname" type="text" size="10"/></label> <label>Surname:<input name="surname" type="text" size="10"/></label> <label>Contact Number:<input name="contactnumber" type="text" size="10"/></label> <label>Email Address:<input name="email" type="text" size="20"/></label> <label>Username:<input name="username" type="text" size="10"/></label> <label>Password:<input name="pass" type="text" size="10"/></label> <label>Retype Password:<input name="pass2" type="text" size="10"/></label> <input type="submit" name="submit" value="submit"/> <input type="reset" name="reset" value="reset"/> </form> Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924136 Share on other sites More sharing options...
Chris_Mc_1985 Posted September 24, 2009 Author Share Posted September 24, 2009 no. still nothing. Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924144 Share on other sites More sharing options...
Bricktop Posted September 24, 2009 Share Posted September 24, 2009 Hi Chris_Mc_1985, Are you sure? I've just tried it on my machine and it works fine. I've just added some extra password checking to the below but try again, it's definitely working on my machine: <html> <body> <font color="black"> <b><u>Contact</b></u> <b><u>Me</b></u> <br> <br> <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!$_POST['firstname'] || strlen($_POST['firstname']) < 4) { $error .= '<li><strong>Invalid Name:</strong> 3 letters or more needed!</li>'; } if(!$_POST['surname'] || strlen($_POST['surname']) < 4) { $error .= '<li><strong>Invalid Surname:</strong> 3 letters or more needed!</li>'; } if(!$_POST['email'] || !ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $_POST['email'])) { $error .= '<li><strong>Invalid E-mail:</strong> Please type a valid e-mail </li>'; } if(!$_POST['username'] || strlen($_POST['username']) < 5) { $error .= '<li><strong>Invalid Username:</strong> 5 letters or more needed!</li>'; } if(!$_POST['pass'] || !$_POST['pass2']) { $error .= '<li><strong>Please enter a password!</li>'; } elseif(strlen($_POST['pass']) < 5) { $error .= '<li><strong>Password must contain 5 characters or more!</li>'; } elseif($_POST['pass'] !== $_POST['pass2']) { $error .= '<li><strong>Passwords are invalid:</strong> Passwords don\'t match!</li>'; } if(!$_POST['contactnumber'] || strlen($_POST['contactnumber']) < 5) { $error .= '<li><strong>Invalid contact number</strong></li>'; } if(!$error) { $content .= '<div id="error" class="valid">'; $content .= '<ul>'; $content .= '<li><strong>Congratulations!</strong> All fields are OK </li>'; $content .= '</ul>'; $content .= '</div>'; echo $content; } else { $content .= '<div id="error">'; $content .= '<ul>'; $content .= ''.$error.''; $content .= '</ul>'; $content .= '</div>'; echo $content; } } ?> <form name="register" method="post" action="index.php?page=addmember" /> <label>Firstname:<input name="firstname" type="text" size="10"/></label> <label>Surname:<input name="surname" type="text" size="10"/></label> <label>Contact Number:<input name="contactnumber" type="text" size="10"/></label> <label>Email Address:<input name="email" type="text" size="20"/></label> <label>Username:<input name="username" type="text" size="10"/></label> <label>Password:<input name="pass" type="text" size="10"/></label> <label>Retype Password:<input name="pass2" type="text" size="10"/></label> <input type="submit" name="submit" value="submit"/> <input type="reset" name="reset" value="reset"/> </form> The only chnage I'm making is I'm changing: <form name="register" method="post" action="index.php?page=addmember" /> to: <form name="register" method="post" action="test.php" /> As this is the name of the file I saved your code into, that's the only difference so it might be worth you checking that is correct for your script. Link to comment https://forums.phpfreaks.com/topic/175350-form-validation-help-needed/#findComment-924146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.