tefuzz Posted April 11, 2009 Share Posted April 11, 2009 Looking through posts here I have made my way into creating a multiple part form...With some help on code duplication I switched over to using functions to display my form fields, which makes looking at it 100% better. My entire first page of my form only has 66 lines of code instead of the almost 400 I had before...Now comes my dilemma. Sessions seem to be the way to go. I have questions though...I will have 6 parts all together: 4 forms (each on their own page), 1 review/submit page, and 1 thank you page. Where I am having the trouble is the "order" of doing things, and exactly how to accomplish what I am looking for. 1. I would like to validate each step as the 'next' button is pressed, if errors are found I want the page to reload with the good fields filled in and the error fields marked for errors. Should I have a separate "processing" page which will check which step the user is on, and validate the fields accordingly? 2. With the session, if the user hits the back button does the session keep the variables intact? And is there a way to repopulate the fields that were entered? I'll start with those 2 questions. I have already created "step 1" and I am ready to begin the "step 2" but not sure where to start. Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 I managed to create a multi-part form without needing to use sessions, all I used was functions. I have also got it to validate each part of the form upon submisssion, by using an errors array. If errors are found in the form input, the errors array is populated with the field name that has the error in it, then that form is redispalyed with the errors shown: if(!empty($errors)) { form1($errors); } else { form2($_POST); } Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 if($_SERVER['REQUEST_METHOD'] != 'POST') { form1($_POST); //use $_POST for the sake of providing an array to use } else if($_POST['process'] == 1) { if(strlen($_POST['name']) < 2 || trim($_POST['name']) == '') { $errors[]='name'; } if(!empty($errors)) { form1($errors); } else { form2($_POST); } } else if($_POST['process'] == 2) { // form 2 processing here } On your form have a hidden input field called "process" with value of "1" on your first form, and value of "2 on your second form, and so on. Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 function form1($values) { <input class="text" name="firstname" ', in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null ,' type="text" value="', isset($_POST[firstname]) ? $_POST[firstname] : null ,'" size="10"/> } Quote Link to comment Share on other sites More sharing options...
tefuzz Posted April 11, 2009 Author Share Posted April 11, 2009 Omzy, I;m a little confused at that (new to php ) can you elaborate on exactly what that is doing? specifically the second snippet you posted with the <input... Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 The input field is checking if 'firstname' is in the array $values. $values will either be a blank array or will contain the $errors array that will get passed in if there are any errors found. If 'firstname' is found in the array, it sets the background of that field to a certain colour, to indicate there was a problem with that field. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 11, 2009 Share Posted April 11, 2009 personally I'd have the whole thing on one form and use javascript to control which bits are displayed at each stage; all you need are back/next buttons implemented in javascript and you have a fantastic user experience with all the code in one one place and easy to manage. this would cut down the number of requests made to the server - you could use some ajax to validate each stage and all in all be an awesome form to use. Quote Link to comment Share on other sites More sharing options...
tefuzz Posted April 11, 2009 Author Share Posted April 11, 2009 Ok, I think im getting closer to understanding this... so this piece of code else if($_POST['process'] == 1) { if(strlen($_POST['name']) < 2 || trim($_POST['name']) == '') { $errors[]='name'; } if(!empty($errors)) { form1($errors); } Is First checking to see if "process" is "1". If it IS, then it will begin checking the fields for errors. If name has an error, it adds name to the $errors array then, after the fields are validated, if there are errors found, it runs the form() function passing through the $errors array. right so far? Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 But using JavaScript/Ajax as the method of validation would not make it bot/spam proof... Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 Yep tefuzz, that's right! And if no errors are found it goes straight to form2() whilst passing the $_POST data to it! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 11, 2009 Share Posted April 11, 2009 @omzy - you ALWAYS validate server side anyway you should NEVER leave validation to javascript. AJAX is different as you can validate server side 'on-the-fly' - this just removes some of the frustration the user could have if they fill out lots of info and have to do some again after hitting submit. Quote Link to comment Share on other sites More sharing options...
tefuzz Posted April 11, 2009 Author Share Posted April 11, 2009 and now we've called the form function, and passed trough $errors array as $values. function form1($values) { <input class="text" name="firstname" ', in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null ,' type="text" value="', isset($_POST[firstname]) ? $_POST[firstname] : null ,'" size="10"/> } so, the function is going to display the input field, this piece of it: in_array(firstname, $values) ? ' style="background-color: #FFEACE"' : null is checking the array, and if firstname is found (referred to as name before, but i know what you meant) '?' thats a shortened 'if' right? so if returns that firstname is present, it will change the background color ( : being the else?) it will return nothing? am i seeing this right? and it does the same for the value of the field, checking if the $_POST was set with this: isset($_POST[firstname]) ? $_POST[firstname] : null , did i get it all? Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 Yep I think you've got it spot on! That should be enuff for you build upon now :-) @ toonMariner - if JavaScript is disabled then AJAX won't work. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 11, 2009 Share Posted April 11, 2009 @omzy - really? I didn't know that... perhaps that's why I code so that my forms are validated on submit anyway and then just use ajax when its available - I know it sounds crazy buy hey - its the way I roll Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 Yep - AJAX (Asynchronous JavaScript and XML) But yep, each to their own ;-) Quote Link to comment Share on other sites More sharing options...
tefuzz Posted April 11, 2009 Author Share Posted April 11, 2009 Yep - AJAX (Asynchronous JavaScript and XML) But yep, each to their own ;-) I had looked at an AJAX approach, but I figured I'd stick to learning one language at a time...last time i built a website was before CSS came around...only dabbled in since then, and now I'm getting back into it thanks for all the help omzy. i'm going to leave this topic as unsolved until I get it working, that way I can post in here if I have problems (dont forget about me ) Quote Link to comment Share on other sites More sharing options...
Omzy Posted April 11, 2009 Share Posted April 11, 2009 no worries man, i'm just a beginner myself but what i've posted above is what works for me :-) Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 11, 2009 Share Posted April 11, 2009 lol - i've been building sites for a number of years now and even if I say so myself I have a very good grasp of what can do what. pitty some miss my sarcasm mind Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.