I have a multi form process on my page - signup.php which contains 3 forms. I am using POST as the submission method. I am trying to make the form do two things: 1) Validate the fields that have been filled in, i.e name, phone no, email address 2) Once submitted do the following: a) If there are errors in the input, show that form again with the errors highlighted b) If there are no errors, show the next form I need a bit of help on this - I have created the 3 forms on the page and they all link together but getting them to validate and process as above is proving to be tricky. The form action of each form is set to post to itself. So I am using a hidden input to determine which form is shown. As I am using POST unfortunately I cant use the header() function to do a redirect. And I don't wish to go down the GET route or else i'll be exposing private data! I don't need help on the full regex validation stuff, just help on how I need to link everything together in the script! Many thanks in advance.
Am I correct in saying you have three <form>s on one page? If you have, you'll only be getting the data from the submit button inside the single form. Or have I misinterpreted this?
Basically there are three forms each with their own submit button. I have put an IF condition on each form to say which form is displayed, e.g on the first form the condition is: if ($_SERVER['REQUEST_METHOD'] != 'POST'){ //show the first form } On my second form the IF condition is: else if ($_POST['process1'] == 1) { //show the second form } And on my third form the IF condition is: else if ($_POST['process2'] == 1) { //show the third form } The $_POST['process'] variables are the hidden inputs I have put on the forms. The form action attribue is set to post to itself. The forms all link together and are working fine but I now need it to work as outlined in my main post...
Without posting some code it's near impossible to diagnose a problem. Bit like calling the doctor and saying "I've got a lump on my knee what do you think it is?"
Erm... no it isn't - by far. There's no indication of how you're reading the data from the forms, what method you're using to transfer data from one page call to another or how you're building the forms.
Fair enough. The code is as follows: if ($_SERVER['REQUEST_METHOD'] != 'POST') { <form id="submit1" method="post" action="signup1.htm"> //form fields, etc <input class="normal" type="submit" value="Proceed to Next Step"/> <input type="hidden" name="process1" value="1"/> </form> } else if ($_POST['process1'] == 1) { <form id="submit2" method="post" action="signup1.htm"> //form fields, etc <input type="submit" value="Proceed to Final Step"/> <input type="hidden" name="process2" value="1"/> </form> } else if ($_POST['process2'] == 1) { <form id="submit3" method="post" action="signup1.htm"> //form fields, etc <input type="submit" value="Submit Form"/> <input type="hidden" name="process3" value="1"/> </form> } Code (markup):
OK as this is like squeezing blood from a stone, I'll do what I can. In a script where you're building and populating multi forms on one page this is what I would do. First, at the start of the script grab ALL form data and store in variables - these variables can then be used when and where needed - $_POST method. Any data being passed from (let's call them form1, form2 and form3) form1 to form2 can be preserved on form2 by the use of <input type="hidden"> Not a great way of doing it because you can get the user hitting "back" on their browser and other things that may confuse things. The only other way to carry data from one form to another is to use session variables. Sorry but that's the best I can do with so little you're willing to give.
That essentially is all the code, the only bits I have missed out are the form fields - I took those out to make the code easier to understand. There is no more relevant code apart from what I have posted in my last post! Anyway I'll work this out myself, appreciate the help so far.
It won't so much be a solution but more a "how to" example. If you want to list the data that each form needs to collect I'll see what I can come up with - you can PM me the details if you don't want to make it public. As soon as I've got some code for you I'll PM it back (or post here) depending on what you want.
OH, I thought you had them all on the PAGE, you are using php to only send one of the three TO the page. Well, that's different then. The way I usually handle that is to make the different sub-forms their own functions, then pass an array to each function - if the array is empty/undefined, you get a blank form. Your 'checking' routine can then send $_POST to the form if it's invalid. Have a look at my form validation code (which I wrapped up for another user) here: http://www.cutcodedown.com/for_others/meginoz/phpVersion/ Some of that may be of assistance to you... though the handling of each sub-form will require a bit of reworking.
deathshadow - yep that's spot on. i'm going to put the sub forms in their own functions now, but can you tell me how i go about passing an array to each function and how i call it?
Arrays can be passed just like any other variable - even $_POST. I like to set up the variable as a null array in the function. function firstForm($values=new Array()) { That makes $values optional, so you could call that as firstform(); and all indexes of $values will return nothing if you echo them... (though one should call isset for them) While if you call it thus: firstform($_POST); $values will now have a copy of $_POST. So for example in your function this: echo ' <input type="text" name="body" ',( isset($value['body']) ? 'value="'.htmlspecialchars($value['body']).'" ' : '' ),'/> '; Code (markup): Would work either way. I would pass the values instead of calling $_POST directly in the function so as to be sure you are indeed coming from the correct form with values.
Thanks mate I'll give that a go. Do you know how I can make reference to a variable within a function which has been declared outside of the function?