Page 1 of 1

Contact form with required fields

Posted: Fri Feb 26, 2010 5:59 pm
by bigdogcattle
I am trying to code some IF statements into PHP that warns if certain fields are not populated on my contact form (http://bigdogcattle.com/?a=Cattle_Contact). Here is the code I have so far, but it will send me an email even if nothing is populated:

<?php if (isset($_POST['name'])){
$header = 'From: ' .$_POST['email'] .'\r\nContent-type: text/plain; charset=iso-8859-1\r\n';
$body = 'Name:: '. $_POST['name'];
$body .= '\nEmail:: '. $_POST['email'];
$body .= '\nPhone:: '. $_POST['phone'];
$body .= '\nMessage:: '. $_POST['message'];
$body = wordwrap($body,70);
$send = mail('info@bigdogcattle.com', 'Form submitted from your website', $body, $header);
if($send){
echo '<h3>Your messages was successfully sent....</h3>';
}else{
echo '<h3>There was a problem sending this message....</h3>';
}
}?>

Thanks
Jamie
Http://bigdogcattle.com

Re: Contact form with required fields

Posted: Fri Feb 26, 2010 9:33 pm
by mikosiko
post the first lines of your form definition...

<form .....>

Re: Contact form with required fields

Posted: Sat Feb 27, 2010 9:00 am
by social_experiment
You would have something similar to :

Code: Select all

<?php
if ($_POST['namefield'] == '') {
 //display error message
}
else {
 //send the email if all required fields 
 //have filled.
} ?>

Re: Contact form with required fields

Posted: Sat Feb 27, 2010 9:53 am
by Apollo
bigdogcattle wrote:if (isset($_POST['name']))
If the visitor posts an empty name, then $_POST['name'] is set. With an empty string.