Omzy Posted December 18, 2008 Share Posted December 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/ Share on other sites More sharing options...
Omzy Posted December 18, 2008 Author Share Posted December 18, 2008 So 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... Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-718595 Share on other sites More sharing options...
Omzy Posted December 18, 2008 Author Share Posted December 18, 2008 Anybody able to help or do you need any more info, etc? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-718817 Share on other sites More sharing options...
Maq Posted December 18, 2008 Share Posted December 18, 2008 So basically there are three forms each with their own submit button. Why have 3 forms? Can you post all of the code. Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-718826 Share on other sites More sharing options...
Omzy Posted December 18, 2008 Author Share Posted December 18, 2008 Cheers I'll post the code when I get home. I have 3 forms so that they can take $_POST variables from the previous form. Is there an easier of achieving EXACTLY what I am trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-718836 Share on other sites More sharing options...
Maq Posted December 18, 2008 Share Posted December 18, 2008 I have 3 forms so that they can take $_POST variables from the previous form. I don't think you have to do that... I would have to examine your code. Is there an easier of achieving EXACTLY what I am trying to do? Yes there is. Again, code is the easiest way to determine a good solution. Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-718856 Share on other sites More sharing options...
Omzy Posted December 18, 2008 Author Share Posted December 18, 2008 Essentially 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> } Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-719019 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 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="process" value="1"/> </form> } else if ($_POST['process'] == 1) { <form id="submit2" method="post" action="signup1.htm"> //form fields, etc <input type="submit" value="Proceed to Final Step"/> <input type="hidden" name="process" value="2"/> </form> } else if ($_POST['process'] == 2) { <form id="submit3" method="post" action="signup1.htm"> //form fields, etc <input type="submit" value="Submit Form"/> <input type="hidden" name="process" value="3"/> </form> } That would probably be a simpler way of doing it, keep process the same, just change the value. If I get bored here in a second I will draft up a much better solution =) Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-719118 Share on other sites More sharing options...
Omzy Posted December 18, 2008 Author Share Posted December 18, 2008 Right, but all you've done there is change the hidden attribute's name & value - that wont help me get any further! Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-719166 Share on other sites More sharing options...
premiso Posted December 18, 2008 Share Posted December 18, 2008 Right, but all you've done there is change the hidden attribute's name & value - that wont help me get any further! lol But you are missing the point! Anyhow, not knowing how your setup is (if you have one). Here is a "sample" install script. The following is for "signup.php" <?php session_start(); $installForm = '<form name="install" method="POST" action="signup.php">'; if (isset($_POST['process'])) $process = validateData($_POST, $_POST['process']); if (isset($_SESSION['error'])) { $installForm .= '<span id="error">' . $_SESSION['error'] . '</span><br />'; unset($_SESSION['error']); // reset the error data } if (!isset($_POST['process']) || $_POST['process'] == 0) { $installForm .= '<input type="text" name="username" value="' . (isset($_SESSION['username']))?$_SESSION['username']:"" . '" />'; $installForm .= '<input type="hidden" name="process" value="1" />'; $submitText = "Proceed to Step 2"; }elseif ($_POST['process'] == 1) { $installForm .= '<input type="password" name="password" value="' . (isset($_SESSION['password']))?$_SESSION['password']:"" . '" />'; $installForm .= '<input type="hidden" name="process" value="2" />'; $submitText = "Proceed to Step 3"; }elseif ($_POST['process'] == 2) { $installForm .= '<input type="phone" name="phone" value="' . (isset($_SESSION['phone']))?$_SESSION['phone']:"" . '" />'; $installForm .= '<input type="hidden" name="process" value="3" />'; $submitText = "Finish This!"; }elseif ($_POST['process'] == 1) { header("Location: thankyou.php"); // tell them thank you. } $installForm .= '<input type="submit" value="' . $submitText . '"/></form>'; echo $installForm; function validateData($data, $process) { switch ($process) { case 1: if ($data['username'] == "") { $_SESSION['error'] = "Username is empty"; return 0; }else { $_SESSION['username'] = $data['username']; return 1; } break; case 2: if ($data['password'] == "") { $_SESSION['error'] = "Password is empty"; return 1; }else { $_SESSION['password'] = $data['password']; return 2; } break; case 3: if ($data['password'] == "") { $_SESSION['error'] = "Phone is empty"; return 2; }else { $_SESSION['phone'] = $data['phone']; return 3; } break; // etcc.... } } ?> There you go. Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-719257 Share on other sites More sharing options...
sloth456 Posted December 18, 2008 Share Posted December 18, 2008 sounds like a lot of work. I'm not gonna go right into a full blown solution here with written code, but here's some tips. Put your form(s) html in a seperate file and at the top of each file put if(isset($message){echo $message;} Then in the main file whenever you want your form to come up you can just use something like $message="you've entered something incorrectly"; include("form.php"); Another tip for form processing is use a switch statement. $do=$_GET['do']; switch($do) { case validate: //validation process here //if validation fails $message="Validation failed"; include("form.php"); //if successful //execute some code - maybe put some details into a database //go to thank you page or success page header(location:'thankyoupage.php'); //end this junk 'o code break; default: include("form.php"); } Now just make sure that your form action in forms.php is "yourmainprocessingfile.php?do=validate" You were also asking about carrying data from one page to another. Are you aware of SESSION variables. If you need a bit of info to carry around do the following. Firstly at the top of each file that will be using these session variables you need to use session_start(); Then say you can just say something like $_SESSION['username']="whatever username"; And call it later like this echo $_SESSION['username']; doesn't matter what you call it, can be anything $_SESSION['dog'], $_SESSION['cat'], $_SESSION['I really like monkeys far too much'] Quote Link to comment https://forums.phpfreaks.com/topic/137513-muliple-form-processing-validation/#findComment-719303 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.