Zubrag.com :: Forum

Programmers Lounge => PHP / MySql / Smarty Questions => Topic started by: Ahren on May 03, 2009, 08:10:33 AM



Title: Making fields "required"
Post by: Ahren on May 03, 2009, 08:10:33 AM
Hi, I have this contact form below which works just fine, except, I can submit it no problem even if nothing has been entered in any of the fields.
How do I add a code, and where, so that the fields are required? Name, Email, Subject and Message?
Thanks for any help.

Regards, Ahren


Code:
<?
///////////////////////////
//Begin Configuration
///////////////////////////

// Your email
$to = "info@example.com" . ", " ;

// Email Subject
$subject = "Message from " . "Example Online.com";

// Server name, to display in the headers
$server_name = "Example Online";

///////////////////////////
//End Configuration
///////////////////////////

if (!empty($_POST['send']) || !empty($_GET['send']))
{
$action = (!empty($_POST['send'])) ? $_POST['send'] : $_GET['send'];
}
else
{
$action = '';
}


$build_message = false;
if($action != "")
{
$build_message = true;
$message = trim($_POST['message']);
$name = trim($_POST['name']);
$email = trim($_POST['email']);
        $subject = trim($_POST['subject']);
$time = time();
$date = date("F j, Y", time());
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
$headers .= "From: $email\r\n";
$headers .= "X-mailer: " . $server_name . " Server\r\n";
}

if($build_message)
{
/* message */
$message = "
Sender Name: $name\n
Sender E-Mail: $email\n
Sender Subject: $subject\n 
Date Sent: $date\n
Message:\n----------------------------------------\n$message \n";

if(mail($to, $subject, $message, $headers))
{
echo '<p>' . $txt['Thank_you_for_message'] . '</p>';

}
else
echo '<p>' . $txt['error_occured'] . '</p>';
}


?>