TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 05-12-2009, 06:30 AM   #1 (permalink)
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
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form to submit to another form patmagpantay General 8 07-21-2008 07:42 AM
Form validation with Regex [Prototype] Haris Javascript, AJAX, E4X 7 12-08-2007 10:49 AM
Form validation Tanax Javascript, AJAX, E4X 3 09-28-2007 08:45 AM
Form validation help 3gg1 General 0 10-06-2006 03:16 PM
Form Processing William Tips & Tricks 8 04-17-2005 03:24 PM


All times are GMT. The time now is 12:33 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design