FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Validation





sunpascal
Hi,

I need to do some validation on a form.

For this I have set the form action to post to itself by setting action="".

If the user clicks "submit" the form is posted to itself and if an error occurs (i.e. a field was left blank) it shows an error message so that the user can fill in the remaining information.

However after each click on "submit" the all the fields in the form are blank. How do I avoid that?

thanks for your help
DjMilez
...you've got a form and when submitted you don't want it to be blank?


Okay now, what?
sunpascal
I don't want the form to be blank if some fields have not been filled out.

In that case the user should be redirected to the original page with the form so that he can fill out the remaining information. Of course, the form shouldn't be blank when the user has already filled out some fields. Otherwise the user would have to fill out all the information again.
Peterssidan
You have the post data so it's only to put it out on the page again. I used to do like this:
Code:
<input name="name"  value="<? print $_POST['name'];  ?>" />

If no data has been sent nothing will be printed (value="") and it's empty like it should.
sunpascal
ok thanks, that works.

Although, it is a little more tedious with drop down menus.
AOP Web Development
you can try the javascript validation to avoid page refresh everytime it's invalidate... but for the most accurate if you are posting on the same page then that would be okay. but you will have a problem if it will pass on the other form then youneed to go back so alternative way is you can use a session for temp vals then clear it.

just an additional just post by @ peter

Code:

<input name="name"  value="<? print $_POST['name'];  ?>" />

// you can make it to make sure
// this is useful if you are using error_reporting( E_ALL );

<input name="name"  value="<?=(isset($_POST['name'])?$_POST['name']:''?>" />



hope it helps. Wink Wink Wink
Stubru Freak
Peterssidan wrote:
You have the post data so it's only to put it out on the page again. I used to do like this:
Code:
<input name="name"  value="<? print $_POST['name'];  ?>" />

If no data has been sent nothing will be printed (value="") and it's empty like it should.


You should do htmlspecialchars first, otherwise you will get in trouble when someone's input has quotes.
andyd34
Try this
Code:
<?
foreach($_POST as $val=>$str) { if(!isset($str)) { error++; }

if(!$sumbit | $error) { ?>

<form and all its contents

<?
} else {

do something with the form ?>
Related topics
PHP Validation Class
php mysql and site management tools
"Forms To Go" create scripts in PHP, ASP and Perl
Form validation
A very good PHP MySQL Tutorial
one line securing of php pages (login of users) (user auth)
How do i execute an sql file in a php script?
php..free account help
PHP Form Sending problem
simple form security!
I need a complex php/flash form... 250FRIH$
Two validations process
Where to Put Validation and Update Logic? PHP or MySQL
Looking for a javascript input validator (name your price)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.