RobMs Posted April 5, 2010 Share Posted April 5, 2010 hey all, Just wondering if I can pick your brains. I have a page with two forms that send information back to the current page (so form 1 and form 2). First the user has to fill in form 1, then they are presented with form 2. In my validation for form 2 I have to check whether an array contain any empty items, this I can do, however this causes form 1 to be displayed again (because Im checking what form was submitted by the submit button) eg: if(isset($_POST['submit']){ deal with form1, display form 2 } else if(isset($_POST['submit2']){ deal with form 2} else { display form 1} Is there anyway I can get back to form 2, without majorly modifying my code and having to load data back into form2 with session data? I also want to avoid javascript too. Any ideas? if I could make the browser go back, it would be handy :-/ Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 Have no idea where you are having a problem. I would assume that the Form 2 validation occurs in this section else if(isset($_POST['submit2']) { deal with form 2 } So, if validation fails, display form 2. The line of code you displayed above has nothing to do with what form is displayed. That apparently happens later. Personally I would have the actual forms as separate files so you can include() whichever one you need based upon what was submitted and the validation. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 I would use logic similar to this <?php if(isset($_POST['submit']) { //Form 1 was submitted, validate data $valid = true; if(empty($_POST['name'])) { $valid = false; } if(!$valid) { //Form 1 validation failed, redisplay the form $include = 'form1.php'; } else { //Form 1 data is valid. //Save the data somewhere and display form 2 $include = 'form1.php'; } } else if(isset($_POST['submit2']) { //Form 1 was submitted if(!$valid) { //Form 2 validation failed, redisplay the form $include = 'form2.php'; } else { //Form 2 data is valid. //Save the data somewhere and display confirmation page $include = 'thank_you.php'; } } else { //No data was, submitted display form 1 $include = 'form1.php'; } ?> <html> <body> <?php include($include); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
RobMs Posted April 5, 2010 Author Share Posted April 5, 2010 The main issue is losing the users input: if(isset($_POST['submit']) { deal with form1, display form 2 } else if(isset($_POST['submit2']){ deal with form 2} ^^ if it fails validation here, next time it returns to this same page it doesnt have submit or submit2 so it displays what is in the else ( which is an empty form 1), ideally I would like to return to form 2 and have the users data still in the form elements else { display form 1} Validation is happening like so: else if (isset($_POST['submit2']) && !isEmpty($_POST['fieldname']) ) { Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 I always create my forms to populate the post data for when validation fails. Not sure what you mean by "if it fails validation here, next time it returns to this same page it doesnt have submit or submit2". If validation fails on page 2 don't you reload page 2? What are you doing with the data from page 1 when validation passes? All you need to do is detemine how you will store the data when validation passes and then pull that data as needed. Here is what I would do. When validation passes for form 1, load form 2 with hidden fields to capture all the data from form 1. Then when validation fails for form 2, redisplay the form entering all the previous user submitted values (including the hidden fields). So you will then maintain the data previously submitted. 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.