phpvalidationspry

php form validation function not working properly


Hi I need some help please, if its possible.

I have created a PHP function that shows the errors of a form server-side validation right below each field. Here are the basic parts:

$errors = array();

//check if e-mail is valid
    if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        $errors['email'] = "Please give a valid email.";
    }
.....

function display_error($field_name){
    global $errors;
    if($errors[$field_name]){
        echo "<span class=\"form_error\">" . $errors[$field_name] . "</span>\n";
    }
}

....

<span id="spryEmail">
<label>E-mail *<br />
<input type="text" name="email" id="email" />
</label>
<span class="textfieldRequiredMsg">Your e-mail is required.</span><span class="textfieldInvalidFormatMsg">Please provide a valid e-mail.</span></span>
<?php display_error('email'); ?>

When I preview the form on the browser (I temporarly disable javascript), it shows me this:

Notice: Undefined index: email in C:.......\functions.php on line 60 , which points to the if statement inside the function. How can I fix this Notice, so that doesn't show up? Note: If I submit the form, the error message show up correctly.

2) AS you notice I've also added some Spry validation that validates onblur. I have a captcha field

<span id="spryCaptcha">
<label>Security code *<br />
<img src="captcha.php" id="imgCode" />
<input type="text" name="captcha" id="captcha" />
</label>
<span class="textfieldRequiredMsg">Please type the code you see.</span></span>
<?php error_for('captcha'); ?>

and what I'd like to make is if the user enters the wrong code the message from the error_for function also to display onblur, before hitting the submit button. How can I make this work?

Any help would be appreciated.


Solution

  • Actually this change in the code inside the function if(!empty($errors[$field_name])){ ... } worked pretty good on my 1st question!