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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 05-12-2009, 05:22 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You've not populated the $aVariables array with the items from your form. It should be something like the following:

php Code:
$aVariables = array
(
    'name' => $_POST['name'],
    'company' => $_POST['company'],
    'address' => $_POST['address'],
    'email' => $_POST['email'],
    'phone' => $_POST['phone']

);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-12-2009, 06:30 PM   #3 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Well now I am officially a prize prat! *Sigh* Thankyou and sorry :)
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 06:59 PM   #4 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

For fear of asking too much... any ideas for the second part?

I have added into my form this;
onSubmit="valid_form();"

in the form tag, and made the php and html one page with the action the same file (check.php).

I am getting a "undefined index" error on all the variables. I think I need another php book to help me understand more of this stuff :(
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 07:12 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Guezala View Post
For fear of asking too much... any ideas for the second part?

I have added into my form this;
onSubmit="valid_form();"

in the form tag, and made the php and html one page with the action the same file (check.php).

I am getting a "undefined index" error on all the variables. I think I need another php book to help me understand more of this stuff :(

Well I could be wrong, but you don't need onSubmit="" at all. Why are you doing that? Are you calling a javascript?


Secondly, you can definitley have the php and html all in one page. (check.php)
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 07:55 PM   #6 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

I think I have probably just lost the plot to be honest. I have spent so long trying to get this questionnaire working I have almost forgotten the original goal!

Ok so I have taken the on submit bit... the undefined variables error still coming up and it is testing the form validation before I have done anything to it. I put the php in the head. Should I be putting it somewhere else?
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 08:06 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Guezala View Post
I think I have probably just lost the plot to be honest. I have spent so long trying to get this questionnaire working I have almost forgotten the original goal!

Ok so I have taken the on submit bit... the undefined variables error still coming up and it is testing the form validation before I have done anything to it. I put the php in the head. Should I be putting it somewhere else?

Testing it will be tricky, I think you have to close out of your browser for each test, because once you fill out the form, the POST variables will exist, even if you keep hitting refresh. I could be wrong...

That could be why it seems the page keeps trying to validate your form?

As for the undefined variables, can you post your latest code?
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 08:11 PM   #8 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

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' => '[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})''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' => $_POST['name'],

    
'company' => $_POST['company'],

    
'address' => $_POST['address'],

    
'email' => $_POST['email'],
    
'phone' => $_POST['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 'me@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);
            

}



?>
There's the code. It all worked fine before I put it in one file ... (thanks to WildHoney :) )
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 08:34 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Where's the form!??
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 08:40 PM   #10 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Sorry...

Thanks for your patience :)

HTML Code:
<form action="send3.php" method="post">

<fieldset><legend>Quick Quote Questionnaire</legend>

<ol>

<li>Which service would you need?</li>

<p><input type="radio" name="service" value="create"/> Create a new website</p>

<p><input type="radio" name="service" value="update"/> Update my old website</p>

<p><input type="radio" name="service" value="maintain"/> Maintain my website</p>

<li>What is your main reason for having a website?</li>

<?php  # Script 1.0 Questionnaire Processing send.php

//create array and print radios for reason

$reason = array(

'a' => 'To create an online presence',

'b' => 'To make your business information available online',

'c' => 'To increase revenue by selling / promoting online',

'd' => 'To create a 24 hour service',

'e' => 'Low cost market research',

'f' => 'Promote ahead of businesses NOT online',

'g' => 'To reduce support, customer service, printing and mailing costs',

'h' => 'To reach a large audience',

'i' => 'To build a community or network',

'j' => 'To offer a fast and easily accessed source of information'

);

foreach ($reason as $key => $value) {
	
	echo "<p><input type=\"radio\" name=\"reason\" value=\"$value\" /> $value</p>";
	
}
?>
<li>What is your target audience?</li>
<p><textarea rows="4" columns="20" maxlength="30" name="target" ></textarea></p>

<li>How many pages do you think your website will need?</li>
<p><input type="text" maxlength="4" name="pages" /></p>

<li>Which of the following is most important for your website?</li>

<?php

//create array and print radios for important

$important = array(

'a' => 'Looking good',

'b' => 'Working in a wide range of browsers',

'c' => 'Google ranking',

'd' => 'Accurate content / frequent updates',

'e' => 'Improving your company\'s status /credibility',

'f' => 'Getting new customers',

'g' => 'Keeping old customers'

);

foreach ($important as $key => $value) {
	
	echo "<p><input type=\"radio\" name=\"important\" value=\"$value\" /> $value</p>";
	
}

?>

<li>What features do you want on your website?
You can select several options from this list.</li>



<?php
//create array and print checkboxes for features 
$features = array(

'a' => 'Contact form',

'b' => 'Private login area',

'c' => 'Questionnaire',

'd' => 'Custom images / logo',

'e' => 'A shopping cart',

'f' => 'A blog',

'g' => 'Photograph / image display',

'h' => 'Music / document / video clip integration',

'i' => 'A Forum'

);

foreach ($features as $key => $value) {
	
	echo "<p><input type=\"checkbox\" name=\"features[]\" value=\"$value\" /> $value</p>";
	
}


	
?>

<li>Please use this section to mention anything else you want on your website or explain your website needs in more detail. </li>
<p><textarea rows="20" columns="40"  maxlength="800" name="comments" value="comments"></textarea></p>

</ol>

<p>Please include your name, company and contact details below.</p>
<p>Name: <input type="text" maxlength="30" name="name" /> Company: <input type="text" maxlength="40" name="company" /></p>
<p>Address: </p>
<p><textarea rows="5" cols="20" maxlength="150" name="address"></textarea></p>
<p>Telephone: <input type="text" maxlength="30" name="phone" /> Email: <input type="text" maxlength="40" name="email" /></p>
<p>How would you like me to contact you?</p>
<p><input type="radio" name="contact" value="email"/> Email</p>
<p><input type="radio" name="contact" value="phone"/> Telephone</p>


<input type="submit" value="Send Questionnaire!"/>
</fieldset></form>
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 08:50 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Ok, for your PHP code, you have it titled as check3.php, but for your HTML code it's send3.php.

Are both the HTML and PHP code within one page? It really doesn't matter just wondering.

Because if you want the HTML to go to the PHP code, that's fine. I'm just confused because it's submitting to send3.php but in the PHP code, you call it check3.php.....


Also, which undefined variables is it complaining about?
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 09:00 PM   #12 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

I'm really sorry...been a long day. The latest version is check4.php, and the action is to the same...posted older version but the rest of the code is exactly the same.

The errors I am getting...

Notice: Undefined index: name in /Applications/MAMP/htdocs/pf5/check4.php on line 77

Notice: Undefined index: company in /Applications/MAMP/htdocs/pf5/check4.php on line 78

Notice: Undefined index: address in /Applications/MAMP/htdocs/pf5/check4.php on line 79

Notice: Undefined index: email in /Applications/MAMP/htdocs/pf5/check4.php on line 80

Notice: Undefined index: phone in /Applications/MAMP/htdocs/pf5/check4.php on line 81
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 09:26 PM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Guezala View Post
I'm really sorry...been a long day. The latest version is check4.php, and the action is to the same...posted older version but the rest of the code is exactly the same.

The errors I am getting...

Notice: Undefined index: name in /Applications/MAMP/htdocs/pf5/check4.php on line 77

Notice: Undefined index: company in /Applications/MAMP/htdocs/pf5/check4.php on line 78

Notice: Undefined index: address in /Applications/MAMP/htdocs/pf5/check4.php on line 79

Notice: Undefined index: email in /Applications/MAMP/htdocs/pf5/check4.php on line 80

Notice: Undefined index: phone in /Applications/MAMP/htdocs/pf5/check4.php on line 81
Ok, so on check4.php on the top before anything happens. Let's
print out the POST data that's causing the issue and see what we are getting returned from the form....



echo "Name: " . $_POST['name'] . "<br />";

echo "Company: " . $_POST['company'] . "<br />";

echo "Address: " . $_POST['address'] . "<br />";

echo "Email: " . $_POST['email'] . "<br />";

echo "Phone: " . $_POST['phone'] . "<br />";
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 09:50 PM   #14 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Basically says the same..... Notice: Undefined index: name in /Applications/MAMP/htdocs/pf5/check4.php on line 12
Name:

Notice: Undefined index: company in /Applications/MAMP/htdocs/pf5/check4.php on line 14
Company:

Notice: Undefined index: address in /Applications/MAMP/htdocs/pf5/check4.php on line 16
Address:

Notice: Undefined index: email in /Applications/MAMP/htdocs/pf5/check4.php on line 18
Email:

Notice: Undefined index: phone in /Applications/MAMP/htdocs/pf5/check4.php on line 20
Phone:
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-12-2009, 10:07 PM   #15 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Guezala View Post
Basically says the same..... Notice: Undefined index: name in /Applications/MAMP/htdocs/pf5/check4.php on line 12
Name:

Notice: Undefined index: company in /Applications/MAMP/htdocs/pf5/check4.php on line 14
Company:

Notice: Undefined index: address in /Applications/MAMP/htdocs/pf5/check4.php on line 16
Address:

Notice: Undefined index: email in /Applications/MAMP/htdocs/pf5/check4.php on line 18
Email:

Notice: Undefined index: phone in /Applications/MAMP/htdocs/pf5/check4.php on line 20
Phone:

Unless WH has come up with new methods for accepting data, it's basically just saying that you're not passing any of those variables over to her script...
allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 06:30 AM   #16 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Ok so what I don't understand then is why it works when seperate but not when in the same file?

Thanks AWNP for all your time yesterday! :)
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-13-2009, 01:48 PM   #17 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Guezala View Post
Ok so what I don't understand then is why it works when seperate but not when in the same file?

Thanks AWNP for all your time yesterday! :)
Yes that is curious. Can you please post your latest complete code? So far I have yet to see the code in its entirety, it might be easier to spot any issues...

Thanks...
allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 02:48 PM   #18 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Hey

Here it is all in one chunk...

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" type="text/css" href=".css"/>


<?php #Script 3.0  


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' => '[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})', '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]))

		{

			/* error message for empty field */

			$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 validation if ok */

			continue;

		}

		

		/* validate data */
		

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

		{

			/* entered data not valid */
			

			$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;

}



/* define array to be validated */


$aVariables = array

(

	'name' => $_POST['name'],

	'company' => $_POST['company'],

	'address' => $_POST['address'],

	'email' => $_POST['email'],
	'phone' => $_POST['phone']

);



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

$aValidatedForm = valid_form($aVariables);



if (!$aValidatedForm['status'])

{

	/* validation failed  */

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

}

else

{

	/* Success */

	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 = 'me@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);
			

}



?>

</head>
<body>

<p>Please complete these questions if you are interested in having a website created, updated, or maintained.  I will then be able to provide you with a free estimate within 24 hours.</p>

<form action="check4.php" method="post">

<fieldset><legend>Quick Quote Questionnaire</legend>

<ol>

<li>Which service would you need?</li>

<p><input type="radio" name="service" value="create"/> Create a new website</p>

<p><input type="radio" name="service" value="update"/> Update my old website</p>

<p><input type="radio" name="service" value="maintain"/> Maintain my website</p>

<li>What is your main reason for having a website?</li>

<?php  # Script 1.0 Questionnaire Processing send.php

//create array and print radios for reason

$reason = array(

'a' => 'To create an online presence',

'b' => 'To make your business information available online',

'c' => 'To increase revenue by selling / promoting online',

'd' => 'To create a 24 hour service',

'e' => 'Low cost market research',

'f' => 'Promote ahead of businesses NOT online',

'g' => 'To reduce support, customer service, printing and mailing costs',

'h' => 'To reach a large audience',

'i' => 'To build a community or network',

'j' => 'To offer a fast and easily accessed source of information'

);

foreach ($reason as $key => $value) {
	
	echo "<p><input type=\"radio\" name=\"reason\" value=\"$value\" /> $value</p>";
	
}
?>
<li>What is your target audience?</li>
<p><textarea rows="4" columns="20" maxlength="30" name="target" ></textarea></p>

<li>How many pages do you think your website will need?</li>
<p><input type="text" maxlength="4" name="pages" /></p>

<li>Which of the following is most important for your website?</li>

<?php

//create array and print radios for important

$important = array(

'a' => 'Looking good',

'b' => 'Working in a wide range of browsers',

'c' => 'Google ranking',

'd' => 'Accurate content / frequent updates',

'e' => 'Improving your company\'s status /credibility',

'f' => 'Getting new customers',

'g' => 'Keeping old customers'

);

foreach ($important as $key => $value) {
	
	echo "<p><input type=\"radio\" name=\"important\" value=\"$value\" /> $value</p>";
	
}

?>

<li>What features do you want on your website?
You can select several options from this list.</li>



<?php
//create array and print checkboxes for features 
$features = array(

'a' => 'Contact form',

'b' => 'Private login area',

'c' => 'Questionnaire',

'd' => 'Custom images / logo',

'e' => 'A shopping cart',

'f' => 'A blog',

'g' => 'Photograph / image display',

'h' => 'Music / document / video clip integration',

'i' => 'A Forum'

);

foreach ($features as $key => $value) {
	
	echo "<p><input type=\"checkbox\" name=\"features[]\" value=\"$value\" /> $value</p>";
	
}


	
?>

<li>Please use this section to mention anything else you want on your website or explain your website needs in more detail. </li>
<p><textarea rows="20" columns="40"  maxlength="800" name="comments" value="comments"></textarea></p>

</ol>

<p>Please include your name, company and contact details below.</p>
<p>Name: <input type="text" maxlength="30" name="name" /> Company: <input type="text" maxlength="40" name="company" /></p>
<p>Address: </p>
<p><textarea rows="5" cols="20" maxlength="150" name="address"></textarea></p>
<p>Telephone: <input type="text" maxlength="30" name="phone" /> Email: <input type="text" maxlength="40" name="email" /></p>
<p>How would you like me to contact you?</p>
<p><input type="radio" name="contact" value="email"/> Email</p>
<p><input type="radio" name="contact" value="phone"/> Telephone</p>


<input type="submit" value="Send Questionnaire!"/>
</fieldset></form>


</body>
</html>
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Old 05-13-2009, 03:09 PM   #19 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I've got it working.

Test it out on my site, I commented out the part that actually sends the email, but I'm sure that would work...

http://www.gatebattle.com/check4.php
allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 03:33 PM   #20 (permalink)
The Contributor
 
Guezala's Avatar
 
Join Date: May 2009
Location: West Midlands
Posts: 26
Thanks: 2
Guezala is on a distinguished road
Default

Excellent.... how did u do that? I have been trying it on my site too but not werking :P
Send a message via MSN to Guezala Send a message via Skype™ to Guezala
Guezala is offline  
Reply With Quote
Reply



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 10:36 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