pthurmond Posted November 14, 2006 Share Posted November 14, 2006 Ok so I have a script where I want to show a header and then below that I start my code that will only execute if the submit button is hit. The functionality from here I want is that if there are any errors I want to output them to the screen and then redisplay the fields to type in a user name and password. The purpose of this form is to signup for a new user id and password. I want the user name to redisplay, this is simple enough with a line or so of code and the value parameter added to the input field. However I want it to not show the fields if the username setup is successful and instead show a message stating the success and the new username. Also I want to do all of the processing on the same page (of course using PHP_SELF). How do I get it to show the fields on the first display, show them again if any errors are found (like non-matching password vs. password confirms, or a field not filled out), and then show only a success message on the screen? My only idea is to echo the form when the submit button has not been pressed and when there are errors, instead of leaving the form outside the code. Is this the only way or is there an easier way?Thanks,Patrick Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/ Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 I have it in two seperate files.contact.php[code]<?php $pgTitle = "Contact Us"; include("header.inc");//phpinfo();?><?php include("contact.inc"); ?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><table> <tr> <td align="right"><b>Name*:</b></td><td><input type="text" name="first_name" size="50" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name'];?>" ></td> </tr> <tr> <td align="right"><b>Email Address*:</b> </td><td><input type="text" name="email" size="50" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" > </td> </tr> <tr> <td align="right"><b>Subject*:<b></td><td><input type="text" name="subject" MAXLENGTH="50" SIZE="50" value="<?php if (isset($_POST['subject'])) echo $_POST['subject']; ?>"></td> </tr> <tr> <td align="right" valign="top"><b>Comments:</b></td><td><TEXTAREA name="msgBody" VALUE="<?php if (isset($_POST['msgBody'])) echo $_POST['msgBody']; ?>" ROWS=10 COLS=40></TEXTAREA></td> </tr> <tr> <td colspan="2"><div align="center"><input type="submit" name="submit" value="Submit"> <input type="submit" name="reset" value="Start Over" /></div></td> </tr></table> </form><!-- End of Form --><?php include("footer.inc");[/code]contact.inc[code]<?phpfunction escape_data($data){ global $dbh; if (ini_get('magic_quotes_gpc')){ $data = stripslashes($data); } return $data;}if (isset($_POST['submit'])) { // Handle the form. $validForm = true; // Check for a first name. if (eregi ("^[[:alpha:].' -]{2,50}$", stripslashes(trim($_POST['first_name'])))) { $fn = escape_data($_POST['first_name']); } else { $fn = FALSE; $validForm = false; echo '<p><font color="red" size="+1">Please enter your first name!</font></p>'; } $email = true; // Check for an email address. if(!empty($_POST['email'])){ if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))){ $e = escape_data($_POST['email']); } else { $validForm = false; echo '<p><font color="red" size="+1">Please enter a valid email address!</font></p>'; } }else{ $email = false; } // Check for a subject. if (!empty($_POST['subject'])) { $u = escape_data($_POST['subject']); } else { $u = FALSE; $validForm = false; echo '<p><font color="red" size="+1">Please enter a valid subject!</font></p>'; } if ($validForm) { // If everything's OK. $headers = "From: " . $fn . " <" . $e . ">\r\n"; mail($webmaster_email, $u, $_POST['msgBody'], $headers); echo '<br>Thank you for taking the time to contact me. I will respond as soon as possible.<br><br><br><br><br>'; include ('footer.inc'); // Include the HTML footer. exit(); } else { // If one of the data tests failed. echo '<p><font color="red" size="+1">Please try again.</font></p>'; }} // End of the main Submit conditional.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124198 Share on other sites More sharing options...
pthurmond Posted November 14, 2006 Author Share Posted November 14, 2006 The problem with that is how do you handle the situation that they need to try again? Even if the second page has the form in it too the user could and eventually probably will mess up on the entry again. I need a way to be able to infinitely show the page again if they messed up. Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124203 Share on other sites More sharing options...
pthurmond Posted November 14, 2006 Author Share Posted November 14, 2006 Another question I want to know is if a page processes another page, only assigning variables on the submit action from the prior screen, and then this page has to redisplay because of processing itself, will it loose all of the variables and their data that were generated by the post of the prior screen, or will those variables remain intact.Example:[code]if(isset($_POST['submit'])){ $name = $_POST['name']; $query = "INSERT //whatever"; $query = "SELECT //what was just inserted"; $id = //the row id num derived from the last query;}if(isset($_POST['dothenextthing'])){ //Do whatever, data comes from form on this page}[/code]Will the $id and $name variables still exist once it gets done processing "donextthing" from the form on this page?Thanks,Patrick Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124209 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 Unless you "pass" the varaibles to the next page, YES they're gone.There are three ways to "pass" variables from one page to another.Read up on Sessions, Post and Get. Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124211 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 [quote author=pthurmond link=topic=114879.msg467565#msg467565 date=1163464493]The problem with that is how do you handle the situation that they need to try again? Even if the second page has the form in it too the user could and eventually probably will mess up on the entry again. I need a way to be able to infinitely show the page again if they messed up.[/quote]This does EXACTLY that. Try it for yourself.EDIT: URL REMOVED. Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124212 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 to boil it down a little:[code]<?phpinclude('header');if (isset($_POST['Submit'])){ // <--if this is true, then the form was submitted $validForm = TRUE;// <--later we'll see if this is still true validate the form--if something is wrong in any field, then tell visitor there was a problem and what field it's in and change $validForm to FALSE if($validForm){ Perform the form's tasks (in this case, send the message) Thank the visitor include('footer'); exit(); //quit further processing }else{ Tell visitor to try again. }}this runs if $_POST['Submit'] is not set OR if $validForm was FALSE;HTML code of the forminclude('footer');[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124226 Share on other sites More sharing options...
pthurmond Posted November 14, 2006 Author Share Posted November 14, 2006 Thanks, that fixes alot of problems for me. I didn't realize the exit function would do all of that. That is awesome. I appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124486 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 Your test message arrived in my email.[quote]This is the test![/quote] Quote Link to comment https://forums.phpfreaks.com/topic/27160-question-about-sticky-forms-and-same-page-script-processing/#findComment-124489 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.