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
...you've got a form and when submitted you don't want it to be blank?
Okay now, what?
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.
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.
ok thanks, that works.
Although, it is a little more tedious with drop down menus.
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.

| 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.
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 ?> |