The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

1. I understand I need to implement sessions, fine.
2. I am not sure if I will need cookies and, therefore, appropriate expiration time-out values?
3. I am not sure what session variables I will need.. any ideas (username/password etc)?
4. I'm not sure where this code should go in my structure (detailed below).5. However, the first thing I want to do is simply display the form again after submission so the user can correct any errors.
I can then try and implement an error-count array and utilise that to display the errors in red at the top of the form (somehow), for example.

Could you advise me how to ammend my structure (below) to accommodate point 5?

Once that is done, then I can move on to the rest.. bitesize. [img]images/smilies/biggrin.gif[/img]

Current structure is as follows:

PHP Code:

<?php if (isset($_POST['submit'])) { // a new member joins using the form below// Form validationelse { // If validation is all TRUE, then enter a record into DB}} else { // Drop-down box queries?>// Start FORM...// End FORM<?php} // end if main if else statement for if the form has been submitted?>

Sorry...not much time - I was about to log off then I'd seen you posted again so here goes w/ some structures that you may need first before scripting.

PHP Code:

if(!isset($_POST['_SubmitForm'])) {# FORM not sent so ask user to log in - first time}else {# FORM has been sent so need to look at values#$Username = (string) $_POST['Username'];$Password = (string) $_POST['Password'];# get values from FORM user has sent# make sure NAME of INPUTs each have# Username and Password...# or $_POST values will hold nothing#$ErrorArray = array();# I will only check for empty inputs# you can check for proper formats yourself# though the structure is basically the same (no time at the moment y/see)#if(empty($Username)) {$ErrorArray[] = 'Please Enter your Username.';}if(empty($Password)) {$ErrorArray[] = 'Please Enter your Password.';}# now array will hold what ever error you want# based on user forgetting to enter their# Username and/or Password## If no errors, then $ErrorArray will be empty## Now chech $ErrorArray# If empty - i.e. no errors then log user in# else re-display FORM again w/ error messages#if(empty($ErrorArray)) {# no errors so log user in....# this is where your sessions come in# look at script I posted to help you out} else {# found one or more errorsforeach($ErrorArray as $error) {echo('<b>'. $error .'</b><br />');}# now display your FORM here again using# same HTML etc - FORM variables and all} # closing off the else condition if no FORM sent} # close off function

No definite script as such though the method of gathering/printing errors is the standard method used by any number of PHP users and it's generally what you really need to use w/ minimum complications etc. 8)

This part actually checks if a FORM has been sent of not - by looking to see if the variable has been set.

The variable NAME exists w/in the SUBMIT of the FORM. If the variable isn't set, you display the FORM.

If this variable is set (ie FORM sent) you then check for input errors by the user, and display them if found... as I earlier described and ALSO re-display your FORM again.

If no errors found then you go ahead and put their (ie users) inputs into the database or whatever you need to do with them 8)

PHP Code:

..<form type='submit' name='_SubmitForm_' value='Send Form' />..

May I suggest that you use a seperate function to display your FORM ? Thus you can seperate this function from the actual piece of script you have that validates the inputs and deals with the logic procress....

It'll make your job a lot easier, plus you will have less typing to do 8) Another thing, your script will be a lot easier to read and debug as well....

-- EDIT --

You would place this at the top of your page - this is the piece of script that actually kicks things off for you 8)

Your script logic process HAS to begin with this part, so you know what part of your script is being used....

Hello, got your PM - you said look at page 5 - though I have the threads in reverse, so you actually meant was to look at page 1 in my case

But what did I do ? Yes - looked at page 5, page 2 in your case

point1 : yes, for all form INPUTs that you need to validate user inputs, put the $_POST[] values to variables, and Reg. Exp. them.

point2 : Keep this part as it's the only way you have to check if the form has been sent of not.

point2.1 : if not sent, then display the FORM by default. If sent, then check for user input against valid inputs; if okay then insert the user inputs into required database table(s), else if there are errors, you need to display the FORM again, though with the user inputs that were correct, leaving the ones - form INPUT elements w/ bad inputs - blank, and report - ECHO - errors found in the errors array.

point3 : I would assume that the dropdown SELECT box would be part of the form, no ? At the moment you have it before the FORM so it proberly wouldn't get included w/ the other data sent by the form once the user submitted the form, if you follow ?

So best bet would be to include the dropdown SELECT box within the <form...>....</form> somewhere convienent.

Hope this helps you out ? Btw... once you've put the data sent by the user into the database table(s) you'd normally re-direct to another page....

.... so if the user clicks <back or F5 - refresh - the form isn't submitted a second time - thus you get duplicate data in the database, which you want to avoid.

Just a tip really, though you don't HAVE to re-direct - which could be any page you want such as a thank you page etc.

Okay and now..

<?php if (isset($_POST['submit'])) { // a new member joins using the form below

I was wondering if you could elaborate a bit on how to code point 2.1:

a. if not sent, then display the FORM by default.

b. If sent, then check for user input against valid inputs; if okay then insert the user inputs into required database table(s), else if there are errors, you need to display the FORM again, though with the user inputs that were correct, leaving the ones - form INPUT elements w/ bad inputs - blank,

point2.1 : if not sent, then display the FORM by default. If sent, then check for user input against valid inputs; if okay then insert the user inputs into required database table(s), else if there are errors, you need to display the FORM again, though with the user inputs that were correct, leaving the ones - form INPUT elements w/ bad inputs - blank, and report - ECHO - errors found in the errors array.

Okay, here is a more descriptive structured english (as it is known in college nowadays - still is ? I don't know since it's been a while) format for you

2.1.3.5: Now completed user INPUTs validation
2.1.3.6: Test to see if $ErrorArray has any values
2.1.3.6.1: [NOTE] If so then bad INPUTs so DON'T put INPUTs to database
2.1.3.7: Arrive here so to re-display FORM.

Okay, this is about the best I can really explain how to do this. Really, it isn't a difficult task to do

See how you get on with checking for user INPUTs against bad validation first; putting error messages to the $ErrorArray first before you move onto next part.

foreach($nerrors as $error) {echo('You have this error:'.$error.' Please correct it.');}

By the look of your script - very end part - you are using another TABLE ?

First, where is your FORM for the user to correct his/her errors ? Also re-check your condition braces in there again to see if there is one of those out of place - which could stop the FOREACH happening.

I do not have a FORM for the user to correct their errors, where do you advise I put this in the code (or on another re-directed page)? Also, how do I have the 'previously entered but okay' values in their respective fields within this FORM?

else { // Found one or more errors $nerrors = count($ErrorArray); foreach($nerrors as $error) { echo('You have this error:'.$error.' Please correct it.'); } ?>// FORM same as earlier<FORM... etc</FORM>

Nothing happens!

Hi all

What I have now is as follows but when submitting the form, nothing actually happens! i.e. No validation/error messages are displayed in regards to submitting a blank form/fields. [img]images/smilies/confused.gif[/img]

// Get user inputs from $_POST# FORM has been sent so need to look at values$Forename = (string) $_POST['Forename'];$Surname = (string) $_POST['Surname'];... etc// Create ErrorArray Variable$ErrorArray = array();// Check each input form user// START FORM VALIDATIONif (!ereg ("[a-zA-Z]{1,}", $_POST["Forename"])) {$ErrorArray[] = "Please enter a Forename consisting of alphabetical characters.";}// Validate Surnameif (!ereg ("[a-zA-Z]{1,}", $_POST["Surname"])) {$ErrorArray[] = "Please enter a Surname consisting of alphabetical characters.";}... etc// END FORM VALIDATION// Test to see if ErrorArray has any valuesif(empty($ErrorArray)) {# no errors so log user in....# this is where sessions come in!!!// Enter record into database}else {// Found one or more errors$nerrors = count($ErrorArray);foreach($nerrors as $error) {echo('You have this error:'.$error.' Please correct it.');}

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) or die("The site database appears to be down."); if ($db!="" and ![email=@mysql_select_db($db]!@mysql_select_db($db[/email])) die("The site database is unavailable.");