/    Sign up×
Community /Pin to ProfileBookmark

Advance form validation

Hi,

Is there a way that when a form go to its process page(i.e. when u click submit) that is the fields have not been filled out it will take them back to a page with a msg of the fields they have not actually filled out and keep the settings of all the other fields.

I am currently using a basic one of this

[code=php]
function filled_out($form_vars) {
foreach ($form_vars as $key => $value) {
if((substr($key, 0, 4) != ‘OPT_’) && (substr($key, 0, 6) != ‘GROUP_’)) {
if ($value == ”) {
return false;
}
}
}
return true;
}
[/code]

Obviously OPT_ and GROUP_ are optional fields.

Any ideas or tuts on how to do that

Thanks
Adam

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 07.2005 — Best way that comes to mind for me would be to use a [url=http://us3.php.net/manual/en/ref.session.php]PHP session[/url] and store the values of interest in the $_SESSIONS[] array. When displaying the form, check to see if the appropriate $_SESSION['param'] is set, and if so make it the default value for that field.
Copy linkTweet thisAlerts:
@k0r54authorJan 07.2005 — I was thinking something along the lines of that but do you have any code for that???
Copy linkTweet thisAlerts:
@NogDogJan 07.2005 — Quick way to write all of your $_POST keys=>values to the $_SESSION array:
[code=php]
$_SESSION = array_merge($_SESSION, $_POST);
[/code]

When outputting your form:
[code=php]
$value = (isset($_SESSION['abc'])) ? " value='{$_SESSION['abc']}'" : "";
echo "<input name='abc' type='text'$value>";
[/code]
Copy linkTweet thisAlerts:
@k0r54authorJan 07.2005 — Ok, im a little lost by that.

I think my head is slowing down today.

How would i firstly check the fields in i.e no2.php and then go back to form.php and enter the fields that have been field in?

Thanks

Adam

Perhaps you could attach something simple for me to c, if its not to much trouble

Thanks Very much

Adam
Copy linkTweet thisAlerts:
@BrewsterJan 07.2005 — How about posting the data to form.php and if the user has entered all of the data correctly redirecting them to no2.php ?

This way, if the user has filled in the form correctly, the fields that have been filled in correctly can be repopulated.

Brew
Copy linkTweet thisAlerts:
@k0r54authorJan 08.2005 — Ok, so how exactly can I tell what fields have not been filled in.

And i still dont rly understand that code b4?
Copy linkTweet thisAlerts:
@BrewsterJan 08.2005 — [code=php]$_SESSION = array_merge($_SESSION, $_POST);[/code]

This code puts all of the values that are posted into the session array, so you can access them like this:

[code=php]$_SESSION['value'][/code]

instead of this:

[code=php]$_POST['value'][/code]

The advantage of this is that you can reference the values that have been posted on different pages.

To see if the fields have been filled in, try this:

[code=php]if ( strlen( $_SESSION['value'] ) < 1 )
echo 'The field value has not been filled in';
else
echo 'The field value has been filled in';[/code]


Hope that helps

Joe
Copy linkTweet thisAlerts:
@k0r54authorJan 08.2005 — Yes that helps alot ?

One last thing though, what if i wanted to miss anything that begins with OPT_ like my original script.

Can can u clarify how i can actually know what fields have not been filled in?

Thanks

Adam
Copy linkTweet thisAlerts:
@BrewsterJan 08.2005 — Hi Adam

Is the form that you are using dymanic? Are the form field names generated by php?

The code that I posted check if the fields had been filled in by checking the length of the data that had been posted - if the string length is less than 1, then nothing has been posted.

Joe
Copy linkTweet thisAlerts:
@k0r54authorJan 08.2005 — Hi, yes i understand the <1 means if there is no data, but i need to know which fields are not field in and return to the page with a msg of what fields are not filled in.

The Fields are not currently dynamic, but if i am to use the $_SESSION to repopulate the fields then it may have to be

Any ideas?

Thanks

Adam
Copy linkTweet thisAlerts:
@k0r54authorJan 08.2005 — Using that technique of storing form values in a session in one go.

If i have a 3 page registration, after storing the first pages values in a session, how do i then add the second pages to that session and again the 3rd, but more importantly how do i then seperate them back to there individual variables again to input them on a database.

Does any1 have any code, tuts or advice on this

Many Thanks

Adam
Copy linkTweet thisAlerts:
@k0r54authorJan 08.2005 — What i need to know now is how do i check and bring a messgage back with the incorrect fields.

E.g.

Page 1 has a form with the fields in a, b, c and D

a = must be a number so it begins with N_fielda

b = can be all charactors so it is just fieldb

c = is optional so it is OPT_fieldc

d = email so my email validations will go in there so it is E_fieldd

the form links to page2.php

In page2.php there is validations, (what i dont know how to do) to check the fields firstly if they are null if it does NOT begin with OPT_, then to check that anything that begins with N_ is just numeric and anything the begins with E_ check for the general email validation for charactors and that the @ sign is in there.

If any of the fields are not correct i need it to then go back to page 1 and to have a msg of what fields where wrong and what was wrong and also repopulate the fields that are correct.

Now that is what i need lol, quite long i know.

If some1 can point me in the right direction or has any code for this then that would be brilliant?

Please get back soon

Thanks

Adam
Copy linkTweet thisAlerts:
@k0r54authorJan 09.2005 — hi,

I have this code so far, but it dont work ? any1 know why,i have field all the fields in and it is cumin up with 'The field value has not been filled in, also how do I display the indivdual variables when using this session array?

[code=php]
$_SESSION = array_merge($_SESSION, $_POST);

//Check to make sure that fields that DO NOT start with OPT_ are filled in
if (substr($key, 0, 4) != 'OPT_') {
if ( strlen( $_SESSION['value'] ) < 0 ) {
echo 'The field value has <strong>not</strong> been filled in';
} else {
echo 'The field value <strong>has</strong> been filled in';
}
}
[/code]


Thanks Adam
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...