the code below is working on validating the fields name, email and message.
include("validationclass.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// instantiate object
$fv = new FormValidator();
$fv->isEmpty("name", "Please enter a name");
$fv->isEmpty("email", "Please enter an email");
$fv->isEmpty("message", "Please enter an message");
Why it would now work below?
$strName = isset($_POST['frmSearch']['name'])?mysql_real_escape_string($_POST['frmSearch']['name']):'';
include("validationclass.php");
$fv = new FormValidator();
$fv->isEmpty("frmSearchname", "Please enter a name");
in the isEmpty method i have put the index [‘frmSearch’][‘name’] I have to say frmSearch is the name of the form and the field name.
it will still trow the error when taking the name of the form out.
The operation could not be performed because one or more error(s) occurred.
Please resubmit the form after making the following changes:
* Please enter a name
While in the first of the two script in the first post will trow this error only if the field hasn’t been filled. But now it will trow the error when the field is empty and when I have assign something to the field.
I have to say this is a form that will retrieve and search information from a as a difference as in a contact form the first example of the two script in the first post where it will submit and send the name, email and message field to my email or input it in the database.
What i assume is, the isEmpty method of the FormValidator class checks the $_POST array with the first parameter passed with it. So the name, or whatever that is passed as the first parameter, must exists in the submitting form (and in $_POST super global array). If not, then it will throw error.
Taking into account what you guys has suggest to take out the frmSearch parameter I have done this
$strName = isset($_POST['name'])?mysql_real_escape_string($_POST['name']):'';
include("validationclass.php");
print($strName);
$fv = new FormValidator();
$fv->isEmpty("name", "Please enter a name");
then a print($strName) and it is displaying the value that I have input in the form field. Which means the value is not empty and that’s what I don’t get, why it is not stopping from trowing the message.
Your validator class seems strange. First you need to store the values in a variable and should pass the variable name without the $ to the isEmpty’s first parameter.
Question of the year, first I have to say now it is working, and never so I should be put like that. Why the variable strName is put in there without the $ sign?