Need help with complex server side validation on email form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dustiehollon
Forum Newbie
Posts: 2
Joined: Tue Apr 24, 2007 2:16 pm

Need help with complex server side validation on email form

Post by dustiehollon »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello Everyone,

I am creating a form and there are several fields in this form. The validation is on the server and NOT on the html page. 

The form needs to have special validation that looks at the company name and if the company name is AMCO or BIGBIZ , then the Purchase Number field is required. If the company is a company other than the ones specified, then the Purchase Number isn't required.

My knowledge in PHP is very elementary so any help that can be offered would be greatly appreciated.

Heres the code I have so far:

Code: Select all

<?php
$to                   = "dustiehollon@yahoo.com"; 
$Purchase_Order     = $_POST['Purchase_Order'];
$Ordered_By        = $_POST['Ordered_By'];
$Month          = $_POST['Month'];
$Day        = $_POST['Day'];
$Year        = $_POST['Year'];
$Phone_Number     = $_POST['Phone_Number'];
$Email_Address     = $_POST['Email_Address'];

$d        = date('l dS \of F Y ');
$sub      = "Order Form Submittal from Website";
$headers  = "From: $Ordered_By <$Email_Address>\n";  
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";

$mes     .= 'Date & Time: '.$d. "\n";
$mes     .= "\n";
$mes     .= "Purchase Order: ".$Purchase_Order."\n";
$mes     .= "Ordered By: ".$Ordered_By."\n";
$mes     .= "Date of Installation: ".$Month." ".$Day.", ".$Year."\n";
$mes     .= "\n";
$mes     .= "ADDRESS AND CONTACT INFORMATION \n";
$mes     .= "\n";
$mes     .= "Phone Number: ".$Phone_Number."\n";
$mes     .= "E-mail Address: ".$Email_Address." \n";

$companys = Array("AIMCO", "AMCO", "BigBiz");

if ($Ordered_By == $companys) && (empty($Purchase_Order));
{
     echo "   <div align='center'><h1>Please click the back button on your broswer enter a Purchase Order Number.</h1></div>";
}
else
{
     mail($to, $sub, $mes, $headers);
     print "   <h1><center>Thank you for contacting us!<br>We will be in touch with you very soon.</center></h1>"; 
}
?>

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Please use php tags when posting code.
The basic principle of validation requires a state variable. So you assume it is valid initially and then do checks to against various bits of data and if the checks proove false you set the state to false. Here's what I mean in code:

Code: Select all

$isValid = true;
if ($_POST['foo'] != 'foo') {
    $isValid = false;
}
// more checks
if ($isValid) {
    echo 'Your form is valid yay! I\'m going to do some stuff for you now';
} else {
    echo 'There were some errors on your form, please try again';
}
dustiehollon
Forum Newbie
Posts: 2
Joined: Tue Apr 24, 2007 2:16 pm

Unclear on that

Post by dustiehollon »

Hi,
That's a little unclear for me.
If someone could expand on that or send other advice, I would greatly appreciate it.
Thanks!
Dustie
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Try searching for some tutorials on it. Come back if you have a more specific question.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
if (in_array($Ordered_By, $companys) && empty($Purchase_Order))
{
    echo '<div align="center"><h1>Please click the back button on your browser enter a Purchase Order Number.</h1></div>';
}
?>
Post Reply