verdure Posted August 12, 2008 Share Posted August 12, 2008 Hi , i am a newbie to PHP coding. i am using the following method for form validations where i am having a problem. I am using PHP server side validations for my form. The script is printing a success messgae when required fields are eneterd. (checking the validations for fields) But if the validation fails the error messages are not displayed and form field values are not displaying post data ...(I have set the error messages and field values to post data) i tried looking for a similar problem from previous posts .... hoping u could point to revelant thread answer Quote Link to comment Share on other sites More sharing options...
Jabop Posted August 12, 2008 Share Posted August 12, 2008 Code would help us troubleshoot this. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 12, 2008 Share Posted August 12, 2008 1001 different people could write form validation code and there could be a different problem with each of them. You are going to need to post your code to get specific help with what your code is doing. Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 the form validation is in another script ... formValidation.php <?php //Register an error array if(!$_SESSION["errors"]) $_SESSION["errors"]; //Clear any errors that might have been found previously $errors= array(); //Set up $formVars array with POST variable and register with session if(!$_SESSION["formVars"]) $_SESSION["formVars"]; foreach($HTTP_POST_VARS as $varname => $value) $formVars[$varname] = trim($value, 50); if(empty($formVars["companyName"])) $errors["companyName"] = "Please enter a company name."; //Check if there are any errors if(count($errors)) { //If there are any errors. Back to registration page. header("Location: Registration2.php"); exit; } // echo "form submitted successfully" ?> i am able to come print the success message. But the problem is when the validation fails. the error message are not displayed in registration2.php Quote Link to comment Share on other sites More sharing options...
peddel Posted August 12, 2008 Share Posted August 12, 2008 If u wanna make it easy, keep ur validation client side using javascript. Then send the correct data to ur server using php. If u need more info, send me PM ill answer tomorrow, now im off home work day over Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 ok thank you, i will try and trouble shoot myself today. Quote Link to comment Share on other sites More sharing options...
Jabop Posted August 12, 2008 Share Posted August 12, 2008 If u wanna make it easy, keep ur validation client side using javascript. Then send the correct data to ur server using php. If u need more info, send me PM ill answer tomorrow, now im off home work day over That's a horrible idea. The first thing that he should do is server side validation, and then follow it with JS, IF necessary. Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 If u wanna make it easy, keep ur validation client side using javascript. Then send the correct data to ur server using php. If u need more info, send me PM ill answer tomorrow, now im off home work day over That's a horrible idea. The first thing that he should do is server side validation, and then follow it with JS, IF necessary. I agree to a point. Use JS first, then use a server-side verification. JS will allow more instantaneous feedback, but cannot be relied on. Quote Link to comment Share on other sites More sharing options...
Jabop Posted August 12, 2008 Share Posted August 12, 2008 I agree to a point. Use JS first, then use a server-side verification. JS will allow more instantaneous feedback, but cannot be relied on. Of course js is instant feedback, and yeah it can't be relied on. That's why I said he should write for functionality and then for features. Javascript could be disabled, people can bypass it, etc. But they can't change your backend validation. Javascript is nice to double this to make it easier, but server side should always be done first imo. Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 due to time constraints i am not doing both client side and server side validations. from reading about the best way to validate data ... i decided to go ahead with server side validations as js can be disabled at the client side. so i opted to validate my form data using server side validation. Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 12, 2008 Share Posted August 12, 2008 Verdure -> Well chosen. Jabop -> OH, you mean in a development sense...? I was more referring to the actual validation order. Quote Link to comment Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 If you want to use sessions, you need to use session_start() Quote Link to comment Share on other sites More sharing options...
Jabop Posted August 12, 2008 Share Posted August 12, 2008 @discomatt: in a programming sense, yes. @verdure: good choice Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 thank you any pointers on what might be missing ... the error messages arent displayed. i am thinking i am missing something small. I have tried some other ways of validating data... checking data in the same php file. had some issue there .... so i want to finally stick to one approach and fix my problem.of error messages not being displayed. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 Validating data on the same file that you display the form is often how I do it. It works better than having to use sessions for simple validation. You should try that approach. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 12, 2008 Share Posted August 12, 2008 formValidation.php: <?php session_start(); //Register an error array if(!$_SESSION["errors"]) $_SESSION["errors"]; //Clear any errors that might have been found previously $errors= array(); //Set up $formVars array with POST variable and register with session if(!$_SESSION["formVars"]) $_SESSION["formVars"]; foreach($HTTP_POST_VARS as $varname => $value) $formVars[$varname] = trim($value, 50); if(empty($formVars["companyName"])) $errors["companyName"] = "Please enter a company name."; //Check if there are any errors if(count($errors)) { //If there are any errors. Back to registration page. header("Location: Registration2.php"); exit; } // echo "form submitted successfully" ?> at the top of your form page: <?php session_start(); ?> revraz pointed this out. I just expounded upon it. Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 revraz : when i include session_start(); i get the following error : Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent on clicking [function.session-start] Not Found The requested URL /function.session-start was not found on this server. is there any problem on the server settings ??? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 12, 2008 Share Posted August 12, 2008 No, read the error. You're sending output before the session_start() call. It needs to be before ANYTHING. Quote Link to comment Share on other sites More sharing options...
revraz Posted August 12, 2008 Share Posted August 12, 2008 That means you are not putting session_start at the top of your page. Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 in the formValidation.php i have included the session_start() at the top of the page. in the registration2.php : session_start() is included. i still get the warning message Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent <?php session_start(); function fieldError($fieldName, $errors) { if (isset($errors[$fieldName] )) echo "<font color=RED>$errors[$fieldName]</font> <br> "; } ?> Thank you for your help so far . Quote Link to comment Share on other sites More sharing options...
verdure Posted August 12, 2008 Author Share Posted August 12, 2008 yipee i have successfully removed the session warning error. it was coming because there were white spaces. i am on to solving the problem of displaying error messages the forum has been very useful so far .... previous threads help me remove the session warning error. cheers !! Quote Link to comment Share on other sites More sharing options...
verdure Posted August 13, 2008 Author Share Posted August 13, 2008 I was able to solve my server side validation .. thanks to a simialr problem addresed in a previous post. Thanks people for your help . n here is the code that help me solve my issues ... used the same logic customised it for my form. form.php <?php session_start(); ?> <html> <head> <body> <form action = 'validate.php' method = 'post'> <?php // if there are errors... if ($_SESSION['error']) { // for each error... foreach($_SESSION['error'] as $error) { // echo out the error echo "$error <br />"; } // end for each error // unset the error array so that if user messes // up more than once, it won't keep stacking errors unset($_SESSION['error']); } // end if there are errors // if there is info if ($_SESSION['info']) { // for easier var handling... extract($_SESSION['info']); // no need for this session var anymore unset($_SESSION['info']); } // end if there is info // echo out the form, using the user's info as values // if there is none, it will simply be blank ?> Name <input type = 'text' name = 'username' value=<? echo "$username"?> > <br/> Something <input type = 'text' name = 'something' value=<? echo "$something"?> > <br/> <input type = 'submit' value = 'submit'> </form> </body> </head> </html> validate.php <?php session_start(); // if vars are posted... if ($_POST) { // for easier variable handling... extract($_POST); } // end if posted vars /* if nothing was posted, or page was directly accessed all error messages will be generated, user will be kicked to form and all messages will be displayed */ // check if username filled out if (!isset($username) || trim($username) == '') { $error[] = "name not filled out"; } // end if name not filled out // check if something filled out if (!isset($something) || trim($something) == '') { $error[] = "something not filled out"; } // end if something not filled out // if an error was generated... if($error) { // create a session var to carry the posted vars back to form $_SESSION['info'] = $_POST; // create a session var for error messages to display on form $_SESSION['error'] = $error; // redirect back to form header('Location: formtest.php'); exit(); // end if errors } else { // form filled out successfully. Do something here like echo something. echo "thank you for filling everything out, or whatever."; } // end if form filled out successfully ?> Quote Link to comment 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.