Scooby08 Posted August 13, 2008 Share Posted August 13, 2008 I have a short form error checking code that I need to make an addition to, but nothing I have done is working.. Maybe somebody here knows an alternative.. Here is process.php <?php //array & variable declaration $errorarray = array(); //used to store error messages //validate your input if (trim($_REQUEST['firstname']) == "" ) { $_SESSION['errorstate'] = 1; $errorarray[] = "Please enter firstname!"; } if (trim($_REQUEST['lastname']) == "" ) { $_SESSION['errorstate'] = 1; $errorarray[] = "Please enter lastname !"; } //check for errors if ($_SESSION['errorstate'] == 1) { // if error $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session header("Location: ".SCRIPT_ADD.""); } elseif ($_SESSION['errorstate'] == 0) { // if no error $_SESSION['errorstate'] = 1; $errorarray[] = "You have successfully added a new customer!"; $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session header("Location: ".SCRIPT_ADD.""); } ?> Then next I have the code back on the form page: <?php if((isset($_SESSION['errorstate'])) && ($_SESSION['errorstate'] == 1)) { //check for the sessions and if there is an error echo "<div id='message' class='fade_error'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> Im trying to figure out how to toggle the class on this line from this: echo "<div id='message' class='fade_error'>"; // is error to this: echo "<div id='message' class='fade_valid'>"; // is no error What would I need to do here? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 What do you mean? If there's no error, it doesn't even go back to that page, am I right? Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 yes it does.. This header("Location: ".SCRIPT_ADD.""); == header("Location: index.php"); Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 leave $_SESSION['errorstate'] as 0 when you send it back to SCRIPT_ADD; this will allow you to discriminate whether you're outputting a success or an error message using a simple if() statement. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 yes it does.. This header("Location: ".SCRIPT_ADD.""); == header("Location: index.php"); Oh, okay. Try what akitchin said. You're basically adding an elseif to your if that you already have. Quote Link to comment Share on other sites More sharing options...
adam84 Posted August 13, 2008 Share Posted August 13, 2008 if I am following you correct, would this work?? <?php if((isset($_SESSION['errorstate'])) && ($_SESSION['errorstate'] == 1)) { //check for the sessions and if there is an error if( $_SESSION['errorstate'] < 1 ) $class = 'fade_valid'; else $class = 'fade_error'; echo "<div id='message' class='$class'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 as long as the overall if() is changed, since right now it won't enter that block unless errorstate is 1: if(!empty($_SESSION['errorstate'])) { //check for the sessions and if there is an error Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Heh.. Im still not getting this to work.. Here is what I have tried: <?php // I left the errorstate at 0 if success in process.php if(!empty($_SESSION['errorstate'])) { //check for the sessions and if there is an error if( $_SESSION['errorstate'] < 1 ) $class = 'fade_valid'; else $class = 'fade_error'; echo "<div id='message' class='$class'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> and also this: <?php if((isset($_SESSION['errorstate'])) && ($_SESSION['errorstate'] == 1)) { //check for the sessions and if there is an error echo "<div id='message' class='fade_error'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; } elseif(!empty($_SESSION['errorstate'])) { //check for the sessions and if there is an error echo "<div id='message' class='fade_valid'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 use the first one, but don't forget to leave $_SESSION['errorstate'] as 0 (rather than 1) in the processing block where you assign the success message in the first place. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 I did it exactly like you said and it validates for errors, but when there is success it displays nothing at all This is the process if statement: <?php //check for errors if ($_SESSION['errorstate'] == 1) { // if error $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session header("Location: ".SCRIPT_ADD.""); } elseif ($_SESSION['errorstate'] == 0) { // if no error $_SESSION['errorstate'] = 0; $errorarray[] = "You have successfully added a new customer!"; $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session header("Location: ".SCRIPT_ADD.""); } ?> And did the first way.. Why would it not recognize the success?? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 my mistake - empty() will return true in the event that the variable is a 0. perhaps just use isset() instead: if (isset($_SESSION['errorstate'])) { or even just use both: if ($_SESSION['errorstate'] == 0 || $_SESSION['errorstate'] == 1) { Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 The first way seems to work, but the session seems to store the errorstate, and when entering that page there is an error displayed immediately.. It seems to say the errorstate is set immediately.. Otherwise it works perfectly! Warning: Invalid argument supplied for foreach() in /home/content/.../add.php on line 231 Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 i would suggest using the second if() from my last message - you're setting the $_SESSION['errorstate'] = "" when you're done, which means it is still set and simply has an empty string value. alternatively, you could use unset($_SESSION['errorstate']) instead of setting it equal to "" and keep the isset() if() condition. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 Thank you for all your patience, but that does not seem to fix it.. This code below works, but is still displaying that error on page enter.. I have added an echo of the errorstate just before the code to see what is running through the if statement and this doesnt make sense to me... <?php echo $_SESSION['errorstate']; // displays nothing on page enter, 0 if valid, and 1 if error if ($_SESSION['errorstate'] == 0 || $_SESSION['errorstate'] == 1) { //check for the sessions and if there is an error if( $_SESSION['errorstate'] < 1 ) $class = 'fade_valid'; else $class = 'fade_error'; echo "<div id='message' class='$class'>"; foreach($_SESSION['errormessage'] as $key => $value){ //print the errors echo $value.'<br />'; } echo "</div>"; //clear the errors $_SESSION['errorstate'] = ""; $_SESSION['errormessage'] = ""; } ?> If my echo statement is correct, then the if statement shouldn't process until there is a value?? correct?? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 it would seem so. are you sure the foreach() error is for this block? it appears to be fairly low in the script (line 231). Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 13, 2008 Author Share Posted August 13, 2008 it sure is because thats the same line its on in my file, and if I take it out the page works fine.. I have 740 lines in that doc. This is really funky to me.. Everything is specified correctly, but yet no go.. Well I just tried your unset($_SESSION['errorstate']); idea and that seems to work now.. I guess they just weren't clearing.. Thanks for all who helped.. 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.