Contact form with required fields

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
bigdogcattle
Forum Newbie
Posts: 1
Joined: Fri Feb 26, 2010 5:58 pm

Contact form with required fields

Post 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
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Contact form with required fields

Post by mikosiko »

post the first lines of your form definition...

<form .....>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Contact form with required fields

Post 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.
} ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Contact form with required fields

Post by Apollo »

bigdogcattle wrote:if (isset($_POST['name']))
If the visitor posts an empty name, then $_POST['name'] is set. With an empty string.
Post Reply