I have my error catching variables set as false so whenever the page loads there isn't any error messages thrown out. I have a variable "$error" that's initially set as a just an initializer.. e.g. $error = " ";. Whenever there is an error, I would like for $error to be set to the message each error is echoing and if $error doesn't equal what it was originally initialized with then it should include my form page to allow the user to try again.
What I've tried is to set the $error variable to a new string for the specified error.. e.g. "You can't leave the price box empty!" and at the end of my initial if loop, there's another if loop checking whether or not $error was changed and if it was then include the form page again. As of now, my variables never change and only stay what they we're initially set as.
$pError = false;
$cError = false;
$error = " ";
if(!isset($_GET['pPrice']) || (!isset($_GET['cPrice']))){ //if not set include form
include "form.php";
}
if(isset($_GET['pPrice']) && isset($_GET['cPrice'])){ //if set get variables from form
$pPrice = $_GET['pPrice'];
$cPrice = $_GET['cPrice'];
$validateP = filter_input(INPUT_GET, 'pPrice', FILTER_VALIDATE_FLOAT); //checks to see if value is a float
$validateC = filter_input(INPUT_GET, 'cPrice', FILTER_VALIDATE_FLOAT); //checks to see if value is a float
if(empty($_GET['pPrice']) && (empty($_GET['cPrice']))){ //both fields empty
$error = "<h2 class='error'>Please make sure you fill out all the fields!</h2>";
}
else if(empty($_GET['pPrice'])){ //first field empty
$error = "<h2 class='error'>Previous price can't be empty!</h2>";
}
else if(empty($_GET['cPrice'])){ //second field empty
$error = "<h2 class='error'>Current price can't be empty!</h2>";
}
else if(!$validateP && !$validateC){ //both fields have incorrect inputs
$error = "<h2 class='error'>Please enter numbers only!</h2>";
}
else if(!$validateP){ //first field has a incorrect input
$error = "<h2 class='error'>Previous Price must be a valid number!</h2>";
}
else if(!$validateC){ //second field has a incorrect input
$error = "<h2 class='error'>Current Price must be a valid number!</h2>";
}
else {
echo "Your results have been submitted!";
}
if (!$error == " ") {
$pError == true;
$cError == true;
echo $error;
include "form.php";
} else{
$pError = false;
$cError = false;
}
Expected results are if user enters a incorrect value or doesn't enter a value at all, update the variable $error for that specific error, change the boolean from false to true, then allow for another try. If no errors, echo the "success" message.
Aucun commentaire:
Enregistrer un commentaire