Jump to content

[SOLVED] What needs to be added here?


Scooby08

Recommended Posts

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?

Link to comment
Share on other sites

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'] = "";

}
?>  

Link to comment
Share on other sites

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'] = "";
}
?>

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.