Page 1 of 1

Need help with complex server side validation on email form

Posted: Tue Apr 24, 2007 2:21 pm
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]

Posted: Tue Apr 24, 2007 2:37 pm
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';
}

Unclear on that

Posted: Fri Apr 27, 2007 3:08 pm
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

Posted: Sun Apr 29, 2007 5:00 pm
by Ollie Saunders
Try searching for some tutorials on it. Come back if you have a more specific question.

Posted: Mon Apr 30, 2007 5:06 pm
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>';
}
?>