mds1256 Posted August 13, 2012 Share Posted August 13, 2012 Hi I am looking to create a form that will display an error message with the fields still populated. I am after some advice on how I would achieve it. If the form contains errors then I want to display an error message underneath the form and have the fields populated with the data they have already entered. If the form submission was successful then I want to hide the form and show just a confirmation message (this is the part I am having trouble with as I can achieve this but It means I would need to duplicate the form code and have it twice in my function and I dont want that). My code I have chucked together (very roughly) to demonstrate my issue: <?php function form() { $error = ""; $nameVal = ""; $emailVal = ""; if(isset($_POST['submit'])) { $nameVal = $_POST['name']; $emailVal = $_POST['email']; if($_POST['name'] <> "John") { $error = "not the correct name"; } else { // hide form and display confirmation message here } } echo " <form action=\"\" method=\"post\" name=\"form1\"> <input id=\"name\" name=\"name\" type=\"text\" value=\"{$nameVal}\" /><br /> <input id=\"email\" name=\"email\" type=\"text\" value=\"{$emailVal}\" /><br /> <input name=\"submit\" id=\"submit\" type=\"submit\" /> </form> <br /> {$error} "; } ?> <html> <head> </head> <body> <?php form(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/267011-help-with-form/ Share on other sites More sharing options...
MMDE Posted August 13, 2012 Share Posted August 13, 2012 You could have sent them to another page, but if you want to do it like that, you could set a boolean variable to be true along with $error and the other variables. Then if the script runs to this part: "// hide form and display confirmation message here", you set the boolean variable to false. When we finally come to the form, if boolean variable is true, show the form. Quote Link to comment https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368937 Share on other sites More sharing options...
mds1256 Posted August 13, 2012 Author Share Posted August 13, 2012 You could have sent them to another page, but if you want to do it like that, you could set a boolean variable to be true along with $error and the other variables. Then if the script runs to this part: "// hide form and display confirmation message here", you set the boolean variable to false. When we finally come to the form, if boolean variable is true, show the form. Thanks! That seems to work <?php function form() { $error = ""; $nameVal = ""; $emailVal = ""; $bool = true; if(isset($_POST['submit'])) { $nameVal = $_POST['name']; $emailVal = $_POST['email']; if($_POST['name'] <> "John") { $error = "not the correct name"; } else { $bool = false; echo "Posted Sucessfully"; } } if($bool == true) { echo " <form action=\"\" method=\"post\" name=\"form1\"> <input id=\"name\" name=\"name\" type=\"text\" value=\"{$nameVal}\" /><br /> <input id=\"email\" name=\"email\" type=\"text\" value=\"{$emailVal}\" /><br /> <input name=\"submit\" id=\"submit\" type=\"submit\" /> </form> <br /> {$error} "; } } ?> <html> <head> </head> <body> <?php form(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368940 Share on other sites More sharing options...
MMDE Posted August 13, 2012 Share Posted August 13, 2012 You don't need to write if($bool==true), just if($bool). You could also probably call the variable something like, $display_form, as that makes more sense. if($display_form) Quote Link to comment https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368941 Share on other sites More sharing options...
mds1256 Posted August 13, 2012 Author Share Posted August 13, 2012 You don't need to write if($bool==true), just if($bool). You could also probably call the variable something like, $display_form, as that makes more sense. if($display_form) Good idea! Thanks again for this Quote Link to comment https://forums.phpfreaks.com/topic/267011-help-with-form/#findComment-1368945 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.