View Single Post
Old 05-12-2009, 06:30 AM   #1 (permalink)
Guezala
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default Form Validation Not Working :(

Hey guys!

Well I have really worn my brain down with what is prob very simple to you seasoned php'ers!

For me there are two issues here but the first is this:

When I try testing my form using adapted code from WildHoney here,
I find that it keeps giving me the email error message implying the email isn't valid. It is a valid email address and as far as I can see the regex is correct. I am getting no php errors (am testing locally on MAMP).

Here is the adapted code (have left WH's comments in to guide sad old me lol);
PHP Code:
<?php #Script 3.0  - for check3.php



function valid_form(array $aFormData)

{

    
/* Set a default return that will be returned if validation fails. */

    
$aValidatedForm     = array('status' => false);

    

    
/* The validation rules, specifying an optional regular expression to validate

    that field. Fields without expressions will be ignored. */
    

    
$aValidationRules     = array('name' => '[A-Za-z0-9\.|-|_/i]{2,20}''company' => '[A-Za-z0-9\.|-|_/i]{3,30}''address' => '[A-Za-z0-9\.|-|_/i]{3,50}''email' => '([^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4})''phone' => '(\s*\[?0\d{4}\]?\s*\d{6}\s*)|(\s*\[?0\d{3}\]?\s*\d{3}\s*\d{4}\s*)');

    

    
/* Loop through the validation rules specified above. We're going to be working in

    the fashion of: exclude, include. Check all the exclusions first (those that return false).

    If all is well at the end of the loop, return true as it passed! */
    

    
foreach ($aValidationRules as $szKey => $szRegex)

    {

        
/* Check to see if the item even exists in the form data. */

        
if (!array_key_exists($szKey$aFormData))

        {

            
/* Specify a message so that the user knows what he or she did wrong. */

            
$aValidatedForm['message'] = $szKey ' does not exist';

            return 
$aValidatedForm;

        }

        

        
/* Check to see if the item is simply empty (null, blank). */

        
if (empty($aFormData[$szKey]))

        {

            
/* Do the same as above to inform the individual. */

            
$aValidatedForm['message'] = $szKey ' cannot be empty';

            return 
$aValidatedForm;

        }

        

        
/* Check to see if we have specified a regular expression for this field. */

        
if (is_null($aValidationRules[$szKey]))

        {

            
/* Continue the loop if not. This item passed validation if it got this far. */

            
continue;

        }

        

        
/* Validate the value against the regular expression specified for this field at the top of

        the function. */
        

        
if (!preg_match('~' $aValidationRules[$szKey] . '~i'$aFormData[$szKey]))

        {

            
/* Set the message as usual to notify them of this error. */
            

            
$aValidatedForm['message'] = $szKey ' syntax is invalid';

            return 
$aValidatedForm;

        }

    }

    

    
/* Everything passed and validated as we expected and so we can

    return true knowing that if something failed in the form data, our script

    wouldn't have gotten this far. */
    

    
$aValidatedForm['status'] = true;

    return 
$aValidatedForm;

}



/* Our array that contains all the form data. Such form data fields can be

constructed using either PHP (Looping through _GET/_POST) or by naming

your form items as like: name="my_form[Name]" */


$aVariables = array

(

    
'name' => "name",

    
'company' => "company",

    
'address' => "address",

    
'email' => "email",
    
'phone' => "phone"

);



/* Validate the form using the values above. */

$aValidatedForm valid_form($aVariables);



if (!
$aValidatedForm['status'])

{

    
/* If the validation failed then display the message to the user. */

    
echo 'Oops: ' $aValidatedForm['message'];

}

else

{

    
/* Otherwise... voila! */

    
echo '<p><b>Thankyou - your questionnaire has been sent. I will contact you within 24 hours.</b></p>';
    
    
$service = ($_POST['service']);
            
$reason = ($_POST['reason']);
            
$target = ($_POST['target']);
            
$pages = ($_POST['pages']);
            
$important = ($_POST['important']);
            
$name = ($_POST['name']);
            
$company = ($_POST['company']);
            
$address = ($_POST['address']);
            
$phone = ($_POST['phone']);
            
$email = ($_POST['email']);
            
$contact = ($_POST['contact']);
            
$comments = ($_POST['comments']);
            
            
$features = ($_POST['features']);
            
            
$feature_list implode(", "$features); //implode array from checkboxes ready for email
            
            
$to 'name@domain.co.uk';
            
$subject 'Quote Request';    
            
            
$body =    "You have recieved a quote request from $name at $company.\n They want you to $service a website. Their main reason for a website is $reason and their target audience is $target.\n They want about $pages pages on their website.\n They also feel $important is the most important element for their website. \n$name would like the website to have a $feature_list.\n They also added this: $comments . \n Their email address is $email, address: $address, and phone number: $phone .\n $name would prefer to be contacted by $contact.";
                
            
                
mail ($to$subject$body);
            

}



?>
My second issue is that I wanted to include my validation code in the same page as my html form but when I do, the php executes before I have filled in the form. All I had done is put the php in the head and refer action to the html rather than a separate file. I was thinking maybe I do something like onsubmit function valid_form();

Is that right? I am sorry this is prob a very simple issue but this is big for me at the moment! LOL.

Thanks in advance :)
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote