Scooby08 Posted August 13, 2008 Share Posted August 13, 2008 I have a form that I'm trying to validate.. I have 3 files involved: index.php; process.php; functions.php.. When submitted it runs through process.php.. In that file I call on a function from the functions.php file to check for errors, but my problem is that the function is not actually getting called.. For some reason it is overlooked and I can't figure out why. Here is the function code.. <?php function check_for_errors() { if ($_REQUEST['firstname'] == '') { $message = "Please enter firstname!" . "<br />"; } if ($_REQUEST['lastname'] == '') { $message .= "Please enter lastname!" . "<br />"; } return $message; header("Location: index.php?message=$message"); exit(); } ?> What am I overlooking here? Or maybe I'm just not supposed to call on validate form function like that?? Im not sure?? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 you should also post the code of process.php. Did you include functions.php in process.php before calling the function?? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 Also, I think there is no use of using header after function return. Quote Link to comment Share on other sites More sharing options...
peddel Posted August 13, 2008 Share Posted August 13, 2008 be sure that ur process.php file has on top this code <?php include("functions.php"); ?> Also make sure ur index file has <?php include("process.php"); ?> Next make sure that inside index.php ur form header is right. Something like this <form name="form1" method="post" action="" onsubmit="return check_for_errors()" Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 be sure that ur process.php file has on top this code <?php include("functions.php"); ?> Also make sure ur index file has <?php include("process.php"); ?> Next make sure that inside index.php ur form header is right. Something like this <form name="form1" method="post" action="" onsubmit="return check_for_errors()" Including process.php in index.php is not necessary. You just keep process.php as your form action. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Here is the form action line <form action="process.php?action=add" method="post"> Here is process.php <?php // Connect to database include ('config.inc.php'); include ('database.inc.php'); include ('functions.inc.php'); // Actions function main() { if ($_REQUEST['action'] == 'add'){ add(); } elseif ($_REQUEST['action'] == 'update'){ update(); } elseif ($_REQUEST['action'] == 'delete'){ delete(); } } // Call actions function main(); function add() { check_for_errors(); } ?> Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 Here you include functions.inc.php, but in your first post, u said ur function page name functions.php. Is it a typing mistake during post?? Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Yes typing error.. Here is path.. index.php (the form), process.php (the action), functions.inc.php (calls the error check function into process.php) Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 Sorry .. I read wrong... Truncated .. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 No, you were right the first time.. It was my typing error.. I was just trying to give example code because it all works up until it doesnt call the function.. Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 .. Ok, Now what you should do is, 1) First print a test line before you call the function add() just to check whether it enter inside the if condition. 2) If yes, then make a same check before you call function check_for_errors(). 3) If that test also passed, then remove header inside your function and print $message. I think you will be able to troubleshoot the problem in this manner. By the way, what is the exact error it gives? Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 It passes both those tests and gets inside the add() function.. It's just not recognizing the check_for_errors() function.. I have info below that from the form that is inserted into the database, so I know it's getting there, but that dang function is hiding for some reason.. If I take the code out of the function it works fine.. I might just end up doing it that way.. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 I guess I just have one more question then... I am passing the error messages through the url.. Is this the way to do this?? Seems like that string could get really long being that I have a message for each field that errored. Also, when I get the string form the url and display it on the page it is displayed as so: Please enter the Bride\\\'s name! Then I added this: $message = stripslashes(mysql_escape_string($_REQUEST['message'])); Which still left one slash like so: Please enter the Bride\'s name! So I added another stripslashes around the other stripslashes: $message = stripslashes(stripslashes(mysql_escape_string($_REQUEST['message']))); That got rid of all slashes, but that seems like stripslash overkill.. Is this the correct way to do that as well?? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 It passes both those tests and gets inside the add() function.. It's just not recognizing the check_for_errors() function.. I have info below that from the form that is inserted into the database, so I know it's getting there, but that dang function is hiding for some reason.. If I take the code out of the function it works fine.. I might just end up doing it that way.. Buddy, Your code works very fine on my PC!! I don't know what the problems there!!! And again I am saying, your header portion inside the function wont work, you have to take that in ur mind. Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 13, 2008 Share Posted August 13, 2008 what I did normally is instead of passing the error message through url (which may be much longer some time), i send error code. And then in the target page, I read the error code and generate the error msg accordingly. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Now that's interesting.. Can you give me a more descriptive example as to how you're doing this please?? Quote Link to comment Share on other sites More sharing options...
waynew Posted August 13, 2008 Share Posted August 13, 2008 I reckon there's something up with the way you're giving out these errors. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 13, 2008 Share Posted August 13, 2008 if you are wanting to redirect when an errors occurs then your best off having all your logic in a seperate script. in the processing scipt, you no output. you store the errors in an array then when you wan to output the errors you store the array in a session then redirect to the appropriate page and use a loop to display each of the errors processing script: <?php //start session session_start(); //array & variable declaration $errorarray = array(); //used to store error messages //validate your input if (1 == 1) { $_SESSION['errorstate'] = 1; $errorarray[] = "1 ='s 1"; } if (1 != 1) { $_SESSION['errorstate'] = 1; $errorarray[] = "1 does not equal 1"; } //check for errors if ($_SESSION['errorstate'] == 1) { $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session header('location: page.php'); //redirect accordingly } ?> main page: <?php if((isset($_SESSION['errorstate'])) && ($_SESSION['errorstate'] == 1)) { //check for the sessions and if there is an error foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Hey thanks a bunch for that bit of code!! Now I've never used a session before.. The code didn't seem to output anything from copy paste.. I had to add the session_start() to the main page as well, and before the html, and it seemed to work. The question I have is this.. I take it the session cannot start in the process.php and carry over to the main page.. I must start a session on the main page before the process for it to be correct then? And do sessions need to be specified on all pages? And do they need to be specified in the function itself, or can it just be at the top of that process page as well? Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 13, 2008 Share Posted August 13, 2008 its the same session, as the session is classed as the period that the user is using the website. you just need to make sure you star the session on top of every page that uses sessions and np's Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Now I'm trying to add an if success message.. Here's what I have, but I'm not getting it.. <?php //check for errors if ($_SESSION['errorstate'] == 1) { // if error $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session //header('location: page.php'); //redirect accordingly header("Location: ".SCRIPT_ADD.""); } elseif ($_SESSION['errorstate'] == 0) { // if no error $errorarray[] = "You have successfully added a new customer!"; $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session //header('location: page.php'); //redirect accordingly header("Location: ".SCRIPT_ADD.""); } ?> Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 13, 2008 Share Posted August 13, 2008 right, you need to set the errorstate to 1 in the elseif because if u dont then the error reader wont read that theres any errors/success messages Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 14, 2008 Share Posted August 14, 2008 well, the thing is I've never been a great fan of using SESSIONS here and there. For your case, I prefer the following method mypage.php $errcode=0; if(error type 1 occurs){ $errcode=1; }elseif(error type 2 occurs){ $errcode=2; }elseif(error type 3 occurs){ $errcode=3; } if($errcode>0){ header('Location: index.php?errcode='.$errcode) } errcodelist.php $errcodearr[]=""; // as we have no error for error code zero $errcodearr[]="1st error message"; $errcodearr[]="2nd error message"; $errcodearr[]="3rd error message"; index.php include ("errcodelist.php"); if(isset($_GET['errcode'])){ $errcode=$_GET['errcode']; echo $errcodearr[$errcode]; exit(); } 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.