Jax2 Posted April 4, 2010 Share Posted April 4, 2010 Hi all, I currently validate my form before it's submitted using JS. It works fine to make sure that nothing is left blank, but I need to go a step further. I know how to validate the form data AFTER it's been submitted using PHP, no problem there, but the form page itself already has post data, so if a user hit's the back key, the entire form is blank again, so I need to do this before it's actually submitted. Again, JS works fine for what it's worth, but I need to verify whether or not a username already exists in the database. my form currently uses: <input type=submit" name="register" onclick="return checkForm();"> So, briefly I need it to function like this: User fills out form: username - new user name password - new password email - new email repeat email - repeat new email clicks on submit before the form goes to nextpage.php, it validates the information and also checks the database to see if the username exists, and if it does, returns an error saying "Please choose a new username". I'd like to do this without using JS, which is why this is posted now in the php section. Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/197548-php-form-validation-before-submitting-form-no-js/ Share on other sites More sharing options...
Pikachu2000 Posted April 4, 2010 Share Posted April 4, 2010 You can't use solely PHP to perform any form validation without submitting the form. PHP, as you know, is a server side language. Make the form fields sticky, and validate it all at once, using header() to redirect to a success page if submitted with no errors, or redisplay the form with the errors highlighted (if there are errors) or if the username is unavailable. Quote Link to comment https://forums.phpfreaks.com/topic/197548-php-form-validation-before-submitting-form-no-js/#findComment-1036804 Share on other sites More sharing options...
the182guy Posted April 4, 2010 Share Posted April 4, 2010 I think you just need to structure your script so that if there is an error, show the error and show the form aswell, including the error message, and populate the form with the users submitted details. I suspect you are submitting the form to a seperate script then if there is an error you can't show the prepopulated form because you've left that page. Have a look at this simple script's structure <?php function checkUsername() { // add code to check username exists, return true or false } function validateForm() { // add code to validate the form data, return true if valid, or a message if not valid e.g 'Name is too short' } $showForm = true; $errorMsg = ''; $display['username'] = ''; $display['name'] = ''; if($_SERVER['REQUEST_METHOD'] == 'POST') // has the form been posted? { // yes, the form has been posted // do any validation and username checks $usernameExists = checkUsername(); $isValid = validateForm(); if($usernameExists == false) { // username does not exist - Good, now check if form is valid if($isValid) { // form is valid // add code here to save to database or whatever $showForm = false; // don't show the form because everything was OK and is completed } else { // form is not valid, need to show the msg $errorMsg = $isValid; // $isValid contains the error message from validateForm() } } else { $errorMsg = 'Sorry, that username already exists, please try another one.'; } $display['username'] = htmlentities(strip_tags(trim($_POST['username']))); $display['name'] = htmlentities(strip_tags(trim($_POST['name']); } ?> <html> <body> <?php if($showForm): // do we need to show the form ?> <?php if(strlen($errorMsg)>0): // if there is an error msg, show it ?> <p>Error: <?php echo $errorMsg; ?></p> <?php endif; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Username: <input type="text" name="username" value="<?php echo $display['username']; ?>" /><br /> Your Name: <input type="text" name="name" value="<?php echo $display['name']; ?>" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php else: // no we don't need to show the form ?> <p>Thankyou for your submission. Your details have been saved etc etc etc.</p> <?php endif; ?> </body> </html> What it does is show the form, when submitted if there is an error then the error is shown above the form with the form prepoluted below (not blank). If the validation and username check is successfull then the thankyou message is shown. Quote Link to comment https://forums.phpfreaks.com/topic/197548-php-form-validation-before-submitting-form-no-js/#findComment-1036834 Share on other sites More sharing options...
greatstar00 Posted April 4, 2010 Share Posted April 4, 2010 all u can try it use ajax to check Quote Link to comment https://forums.phpfreaks.com/topic/197548-php-form-validation-before-submitting-form-no-js/#findComment-1036865 Share on other sites More sharing options...
Jax2 Posted April 4, 2010 Author Share Posted April 4, 2010 Thank you. I had a major brain fart and couldn't think straight after 3 weeks of staring at this website lol ... you got me thinking and I solved it doing pretty much what you said. Here is how it now works in case anyone was wondering, or cares Form submits like usual -- user enters information, presses submit and it calls my checkForm() function which does nothing more than make sure the lines aren't blank and the names/passwords/emails are the correct length and structure. So far so good. On the next page, before anything is submitted to the database, it is first cleaned, and then I check with a simple query to see if the database contains the username or email address entered. If it does not, it submits the info to the db. If the user name already exists, I simply have a new form, as suggested, that tells the users to please choose a new name, gives them a spot to enter it, and then rechecks all the form information again (using hidden inputs for the post data (email, password) from page 1). If everything looks good on the 2nd form check, it reloads the 2nd page again and rechecks the data. If the username and email are okay now, it submits it. It works perfectly. Thank you so much for kicking my butt back into gear Quote Link to comment https://forums.phpfreaks.com/topic/197548-php-form-validation-before-submitting-form-no-js/#findComment-1036871 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.