doubledee Posted March 31, 2011 Share Posted March 31, 2011 Dusting off my PHP skills after some time! How would I do this... IF form was submitted THEN do some PHP stuff and do not show the form again ELSE display the form for the first time Is it as simple as wrapping my HTML inside an If-Then-Else? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/ Share on other sites More sharing options...
creata.physics Posted March 31, 2011 Share Posted March 31, 2011 <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { echo '<form method="post" action="">'; // finish form info } The method I have it set up for you is to EITHER show an html form or have form action be taken, so when the form has been submitted your form will not be visible. If you want to show your form still even while the information is being processed, you can take the html out of the else statement, and remove the else statement completely. Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195124 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { echo '<form method="post" action="">'; // finish form info } Echoing an entire complex page of HTML would be insane! That isn't what I was looking for. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195127 Share on other sites More sharing options...
creata.physics Posted March 31, 2011 Share Posted March 31, 2011 Why can you not include the html? What other ways are you wanting to do this? I simply provided php code with how you would do that, I'm not sure how your html is set up so I cannot make all the code for you. I guess I should ask for more details, but I did follow what you asked for, IF form was submitted -did that THEN do some PHP stuff and do not show the form again - did that ELSE display the form for the first time - did that too So can you be a little more specific? Like I said, you can include an html file instead. <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { include_once('form_info.html'); } Also, to output 'Complex Html' it will eventually need to be echoed to be displayed, so it's not as insane as you would think. Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195131 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 (I am soo in over my head today!) :-\ Here is what I am trying to do... (Had a book with this pattern in it, but can't get to the book.) I have a "Payment Form". If the form is completed properly (i.e. no data entry errors) then the form data is submitted to the Payment Gateway and the user is sent to a different page. If the form is NOT completed properly, then the form is redisplayed with error messages displayed next to each problematic field. The way the author coded this was something like... <?php check form if valid send to payment gateway if not valid then tell the user how they screwed up by redisplaying for with errors ?> HTML for here display form I am trying to re-create that code out of my head and can post what I have in a bit. Does that help? What I do know is that you should avoid putting an entire page of HTML in echo statements! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195143 Share on other sites More sharing options...
DavidAM Posted March 31, 2011 Share Posted March 31, 2011 You can do exactly that: <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { // DROP OUT OF PHP FOR RAW HTML ?> <HTML> <HEAD> <!-- ALL OF MY HTML --> <?php // JUMP BACK INTO PHP TO CLOSE THE else OFF } Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195147 Share on other sites More sharing options...
creata.physics Posted March 31, 2011 Share Posted March 31, 2011 Alright, this helps. What I need to find out, is how you already have your code set up. Say you already have a file, called form.html. If so, you're already done with the html part. Say a user is viewing, www.example.com/form.html - when they submit the form on that page, they are redirected to the form controller, say, form_handle.php for example. When a user submits the form, they can be redirected to form_handle.php, in which the page checks post data to see if it is valid, if the data is not valid it displays errors and redirects them back to the form, if it data is valid then the user is redirected to another page based on success? form_handle.php <?php if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; if ( ! $post_field OR ! $post_field_two ) { $errors[] = 'You must enter both fields'; } if ( check_validation ( $post_field ) ) { $errors[] = "Field {$post_field} could not be validated"; } if ( is_array ($errors ) ) { // redirect user back to the form because we found errors } else { // extra processing? // Maybe a redirect? this area a user will only get to if the form submitted with no errors. } } I'm still a little confused and I'm not sure how your code is already set up. Basically I've just excluded the html, I've made some demonstrations on validating the form and redirecting back to the form if the user encountered any errors, if the user passed the form, well, that area in the code is highlighted. Now I'm not saying this is what you want to do exactly, I'm just trying to take this in small steps to avoid confusion. Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195154 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 You can do exactly that: <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { // DROP OUT OF PHP FOR RAW HTML ?> <HTML> <HEAD> <!-- ALL OF MY HTML --> <?php // JUMP BACK INTO PHP TO CLOSE THE else OFF } Don't I need something like exit(); In the example given, regardless of the path taken in the If-Then-Else, doesn't control always flow down to the HTML? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195158 Share on other sites More sharing options...
DavidAM Posted March 31, 2011 Share Posted March 31, 2011 The HTML code in my example is INSIDE the ELSE part of the IF statement. It will only be reached when the form is NOT submitted. When you drop out of PHP code (using ?>) the PHP engine just sends everything it finds to the browser. When you jump back into PHP (using <?php), the PHP engine starts interpreting what it finds as PHP code. In the example, we jumped back into PHP just to close the curly-brace for the ELSE, otherwise (I think) PHP will throw a parse error because we opened a curly-brace after the else, and PHP needs to find a closing one. Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195161 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 Alright, this helps. What I need to find out, is how you already have your code set up. Well, I can post it in a minute, but see my response in #4 because that is the approach I'm trying to follow. One file called "payment_form.php" When the user submits the forms, it submits back on to itself versus needing a "display_form.html" and then "process_form.php" I just have it all in one. I'm just trying to take this in small steps to avoid confusion. Me too! Maybe that is why my OP was a little vague. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195162 Share on other sites More sharing options...
creata.physics Posted March 31, 2011 Share Posted March 31, 2011 If payment_form.php is the controller then it shouldn't be hard. Does a user view each page? eg, payment_form.php, payment_process.php and display_form.html? You can make payment_form.php a controller to handle everything, so all the user would have to do is visit payment_form.php and the entire script is processed there, so we can make: payment_form.php is a controller, so it either includes the model (payment_process.php) or the raw html (display_form.html) So then it should be like my code in post #6 right? contents of payment_form.php <?php if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; if ( ! $post_field OR ! $post_field_two ) { $errors[] = 'You must enter both fields'; } if ( check_validation ( $post_field ) ) { $errors[] = "Field {$post_field} could not be validated"; } if ( is_array ($errors ) ) { foreach ($errors AS $error) { echo 'Error: ' .$error; } } else { // this handles the process of the payment require_once('payment_process.php'); } } else { // this is what is pulled up by default include_once('display_form.html'); } With it set up this way, a user can view payment_form.php and be taken care of there with the above code. If you do not want it set up this way, let me know exactly how you would have it set up, and hopefully I can adjust from there. Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195187 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 creata.physics, Let's back up a minute and look at the larger goal... I need to create a simple, one-page, credit card payment form. The process flow will go like this... 1.) Customer decides to checkout 2.) Customer arrives on "Payment Page" 3.) Customer enters his/her Name, Billing Address, and Credit Card details in the form 4.) Customer submits form 5.) PHP check for valid field entries If all values are valid... 6a.) Take Form values and create an XML Payment String 7a.) Submit XML string to Payment Gateway 8a.) and so on... If values are invalid... 6b.) Assign field-specific eror message to $Errors array 7b.) Continue checking 8b.) Re-display HTML form 9b.) Append error message to right of each Form control so Customer knows what is wrong 10b.) end If I can get #1-5 and #6b-10b working that would be a great start! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195199 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 Here is what I have so far, but the code doesn't work and is incomplete. It at least shows the approach I am taking... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href=".css"> <style type="text/css" > form{ width: 400px; margin: 0 auto; } </style> </head> <body> <?php if (isset($_POST['submitted'])){ // Handle Form. // Initialize. $errors = array(); // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Check First Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){ $firstName = $_POST['firstNamae']; }else{ $errors['firstName'] = 'Please enter your First Name.'; } // Check Last Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['lastName'])){ $lastName = $_POST['lastName']; }else{ $errors['lastName'] = 'Please enter your Last Name.'; } // if there are errors then go back to the form and display them }else{ } ?> <form action=""> <fieldset> <legend>Billing Details</legend> <ol> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" /> <?php echo $errors['firstName']; ?> </li> <li> <label for="lastName">Last Name:</label> <input id="lastName" name="lastName" class="text" type="text" /> <?php echo $errors['lastName']; ?> </li> </ol> <input class="submit" type="submit" value="Process Order" /> </fieldset> </form> </body> </html> Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232316-code-to-handle-form/#findComment-1195201 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.