nvee Posted December 14, 2009 Share Posted December 14, 2009 Hey guys. I feel like an idiot asking such a stupid question, so please be patient with my utter stupidity Scenario: okay, so I have a simple login script (yes, its very simple, should probably be more secure, but as I am sooo new to php, I figured I must start somewhere) which has a simple form (username and password) which post to logincheck.php - Logincheck.php does simple validation to firs check if either username and password has been assigned using the empty() function, and then if it was empty, creates an variable called $error with a simple <li>Error message</li> - Should more errors be found (e.g. password also being empty) it will just do a $error .= "<li>Another error</li>" My question is, how do I pass these error messages ($error) back to index.php (my original login page) and show the errors? I have only passed variables from one page to another using a form, so I am very stupid with how I will pass it without a form? Is there a way? I mean, there has to be one I guess I could do a header(location:index.php?error=1) or something, but that means that my validation errors variables must be on the index.php page, which takes away the idea of having the login page without any actual code, and having all of the code on one page. I would also prefer not to send the errors via the url as this i suppose may not have a security issue, but just looks nasty Any ideas, I will really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/ Share on other sites More sharing options...
mrMarcus Posted December 14, 2009 Share Posted December 14, 2009 OMGZ!! i can't believe you're asking this. just playing everybody was a beginner at one time. don't hesitate to ask anything here. you'll find, if you ask your questions the right way (explain the problem, post any error messages you are receiving, explain what you are trying to achieve and what the code is currently doing), people are more than willing to help out. now, on to your problem. i'll tell you right now that this is an inefficient way of form handling, in that, you're going back and forth between two different pages. my simple suggestion is to keep everything within one script. here's a basic layout of what i mean: <?php //any includes you might have go here; //has form been submitted? if (isset ($_POST['submit'])) { include 'error_handling.php'; //in this file is all your error handling, ie. check if $_POST vars are set, etc. } ?> <html> <head></head> <body> <form action="" method="post"> <?php //other form stuff here; ?> <input type="submit" name="submit" /> </form> </body> </html> that's a very simple example as to one way to handle forms. this way, your error handling is done above the form so that if any errors have been found, you can simply execute another condition to display them: <?php //any includes you might have go here; //has form been submitted? if (isset ($_POST['submit'])) { //include 'error_handling.php'; //in this file is all your error handling, ie. check if $_POST vars are set, etc. //example of error_handling.php $errors = array(); $display_errors = false; if (isset ($_POST['user_input']) || (!empty ($_POST['user_input']))) { $user_input = $_POST['user_input']; } else { $errors[] = 'Please enter some input, please'; } if (is_array ($errors)) { $display_errors = true; } else { //database query? redirection? send email? } } ?> <html> <head></head> <body> <?php if ($display_errors) { foreach ($errors as $error) { echo $error.'<br />'; } } ?> <form action="" method="post"> <?php //other form stuff here; ?> <input type="text" name="user_input" /> <input type="submit" name="submit" /> </form> </body> </html> just an example with example variables, etc. something to build off of. and sorry, it didn't solve your question per-say, but it's kind of a solution in that this would be a better alternative to what you're currently doing. Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977231 Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 BTW if (is_array ($errors)) { $display_errors = true; } will always be true because of this line $errors = array(); the array() function will always return an array, even if you don't give any data. an empty array is still an array. you probably want to check the count or something like if (count($errors) > 0){//there were errors Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977236 Share on other sites More sharing options...
mrMarcus Posted December 14, 2009 Share Posted December 14, 2009 my bad, hands were typing faster than my brain was thinking. good catch. basic idea is up there. $errors = array(); can be removed and all is well (assuming $errors isn't being defined further up the script, which it's not) as it will reset upon completion of the script. just a simple handling system. Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977239 Share on other sites More sharing options...
nvee Posted December 14, 2009 Author Share Posted December 14, 2009 Thank you for your responses Yes I agree its a more effecient way of doing it, but i am not sure how the procedures work (or better said, how the pro's do it) and want to do it the right way from the first time around. So i assume the right way of doing it is to keep all of the code in a single file (or splitting it but including it with include()) - out of curiousity, is there a way of doing my request? Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977293 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2009 Share Posted December 14, 2009 You have at two functional problems by using a page for the form and another page for the form processing. You must pass any errors from the form processing page back to the form and you must pass any entered data back so that you can populate the fields (assuming you don't want to make the visitor keep re-entering everything.) The best choice to pass this information between two pages is to use session variables. Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977302 Share on other sites More sharing options...
mrMarcus Posted December 14, 2009 Share Posted December 14, 2009 the "procedures" to get this working are pretty much the same as a multi-page form, given that instead of having the "go-to page" in the form action, you are just executing the current page instead, hence the action="". the example i gave earlier pretty much sums up how to do a basic form with error handling. at least it seems pretty straight forward to me, but if you have any more questions, don't hesitate to ask them here. and be specific. NOTE: while including your files is not necessary to make things work, it keeps things neat and organized. Quote Link to comment https://forums.phpfreaks.com/topic/185123-posting-variables-between-pages-without-a-form/#findComment-977309 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.