What I expect on execution:
The function Email() will be called each time the page loads. If $Valid exists Email() should return either true or false. If $Valid does not exist Email() checks the Postback() (example: has the page been submitted and we’re seeing it for the second time?) and if true Validate()s the form fields and either sets $Valid=1 (for true) and sends mail() or sets $Valid=0 (for false).
Now $Valid exists Email() should just return a true or false when called for the second time, say for example by a function to echo a warning or confirmation message if mail() successfully sent or not.
What’s happening?
$Valid isn’t set it seems when Email() is initially called so when we call Email() a second time the isset($Valid) if clause always returns false.
I really hope you can help, I appreciate any answers and thank you all!
BTW: I know we could “try” and “catch” mail() too but I’ve cut down the complexity of the code as much as I can so it’s easier to digest!
Here’s the code I need help with:
function Validate(){
if(!empty($_POST["From"])&&!empty($_POST["Body"])){
return true;
}else{
return false;
};
};
function Postback(){
if(isset($_POST["Return"])){
return true;
}else{
return false;
};
};
function Email(){
if(isset($Valid)){
if($Valid!=0){
return true;
}else{
return false;
};
}else{
if(Postback()){
if(Validate()){
$Valid=1;
}else{
$Valid=0;
};
};
};
};
Email();
You need to declare
$Validas static if you want its value to be preserved across function calls, for example:See http://php.net/manual/en/language.variables.scope.php:
Note that the value will not be preserved across HTTP requests, if you need that you should look at storing the data in a Session.