Jump to content

PHP form clearing after validation error


cozzy1984

Recommended Posts

Hi, I'm quite a newbie to PHP and am doing a website. Have got a login working and the registration half way there, but am now in process of putting validation in Registration form. Have got the errors appearing if domething isn't filled in or if the username is taken however, it clears all the fields and i would like it to keep the values in them if possible. Is there an easy way round this?

 

I can give my code if needed

 

Cozzy

Link to comment
Share on other sites

There are a few ways to do this.

 

1.) NOT RECOMMENDED: Using javascript for your validation wouldn't clear your values, however it's very insecure and it's easy to get around.  Would not recommend this option!

 

2.) OK: After submitting and validation with php, if there are errors you'll need to repopulate those fields with the data submitted.  You can do this a couple of different ways.  The easiest way would probably be in each input set the value to a variable, and have those variables be blank by default.  Then if there are errors set those defaults to the data they provided and the fields will re-populate.

 

3.) MOST RECOMMENDED: Ajax, using AJAX for your form validation wouldn't clear your values, but you could still use php to validate the input.  To secure this, make sure you do not use a "submit" type button, use a regular button that calls an AJAX function to submit the data.  This way if they disable javascript they won't be able to submit the form.

Link to comment
Share on other sites

AJAX should never be depended on and should only be used as a supplement to PHP, as it wont work when a person has javascript turned off. So option 3 isn't the best option, but rather should be added after the fact.

 

As to the question at hand - depending on the element, they can have a value set to them. For example

 

<input type="text" name="somename" value="somevalue" />

 

This code will result in 'somevalue' being in the textbox when the page is loaded. So you want to use php to enter the value that was in the form when you submitted it in the first place:

 

<input type="text" name="somename" value="<?php echo $_POST['somename']; ?>" />

 

Since the field was submitted the first time you submitted the form, it will be dropped right back into the field when the page loads.

 

With <textarea> you drop the php echo function between <textarea> and </textarea>.

 

 

Link to comment
Share on other sites

The strict php method is fine, it just isn't as versatile as the ajax method.  To say "ajax should never be depended on" is a bit of a stretch.  You can make them have javascript enabled by not allowing the form to be submitted without it.

Link to comment
Share on other sites

as he said, javascrpt can be bypassed. reason it shud not be relied on for validation.

look at the popular firefox extension, Greasemonkey, which can remove / replace javascript from web pages. and still report that javascript is enabled.

 

Javascript can be used to make it easier for the end user, those that behave. for the rest dun forget php validation.

 

as to the post by haku, yep, but ya dun wanna use $_POST in yer html form either, another infraction of possible exploiting the code.

use the local variables. if u have validated / santized them.

if(isset($_POST['username'] && preg_match("@^\w+$@i",$username,$matches) && strlen($_POST['username']) < 30)
   $username=$_POST['username'];

 

than in yer html

<input type="text" name="somename" value="<?php echo $username;>"; ?>" />

Link to comment
Share on other sites

The strict php method is fine, it just isn't as versatile as the ajax method.  To say "ajax should never be depended on" is a bit of a stretch.  You can make them have javascript enabled by not allowing the form to be submitted without it.

 

Professionals always work to ALL users. Its bad practice to force your users to use anything. Of course in the real world, its not always like that - most people these days don't program for browsers under IE6 for example. So forcing people to use a certain technology (like javascript) is bad practice - you are likely to have some users who will just not use your site as a result. For someone who doesn't really care about that, its not a big deal. But for someone who is trying to create the most accessible site they can (i.e. professionals), sites should always be programmed to work without javascript, then have javascript functions (such as ajax) added overtop after the pure php base has been made.

Link to comment
Share on other sites

Good luck trying to pass that logic through a corporate environment just so 1 out of every 10,000 doesn't have a problem.  It's a tradeoff, spend the extra money for the employees to create a system that supports old technology, or use the latest technology and suggest people to update.  That's like telling Microsoft they need to change their update page to work with Firefox, it simply wouldn't be a smart decision on their part, but according to you that's "unprofessional"?  I used to think the same way before I worked in a corporate environment.

Link to comment
Share on other sites

The way I'd solve this problem is by first using Javascript validation to validate the form. Although a 100% of users won;'t have Javascript enabled, the majority will so they won't have the problem of the page reloading and having to reinput data.

 

When the javascript validation returns true or if javascript isn't enabled the form submits and the php also validates the data. The page is then redrawn with the variables being put into the value field of the input box.

 

I also wanna point out that I agree with haku, true IT professionals aim to make a site work with as many systems as possible. I also strongly believe that all websites should be made to work without any javascript. Once the site functions completely Javascript can be added as an extra for users who have it enabled.

 

Link to comment
Share on other sites

  • 3 years later...

I'm just learning about PHP and I'm diving in head first by attempting to modify a gift of a web site design and code from two wonderful friends/coders/designers who are as busy as a one armed paper hanger.  I'm wishing to stop the form from erasing values upon "failed" validation.  Later I'll take a deep breath and try to figure out how to add reCAPTCHA.

 

You can find the contact page (and the code under the "view source" feature) at our site of http://www.woofwoofwoof.org/contact/index.php.  I very much would appreciate assistance in modifying this code (if possible) to keep the original user's form entries intact despite failed validation.  Thank you so much!  I very much appreciate your taking the time to help out.  (P.S.  You can see that I tried to do what was suggested above in this thread under the "name" field -- but it unfortunately didn't appear do help out.  Oh, well.)

 

Please accept my apologies if I goofed either by 1) referring to the web page's view source rather than reproducing the code here, and 2) by replying to this post rather than starting a new thread.  It appeared to me that the above posts relate directly to my question; if I did done wrong, please let me know so I can do betterer in the future.  Thank you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.