I am new to PHP and I want to make it so every field is required to send the form. I've tried a few different ways but nothing seems to work. Below is the script with no required fields. Can anyone show me how this would be done? Thanks, Dave

<?php
        $name = $_POST['name'] . ' ' . $_POST['lastname'];
        $practicename = $_POST['practive-name'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $postalcode = $_POST['postalcode'];
        $phonenumber = $_POST['phone'];
        $email_address = $_POST['email'];
        $speciality = $_POST['speciality'];
        $comments = $_POST['comments'];

        if(empty($name) {
Print($name." must be filled in please go back and do it again you twit!");
} 

        $to = 'myemailaddress';
        $subject = 'Web contact form';
        $msg = 
            "Name: $name\n" .
            "Address: $address\n" .
            "City: $city\n" .
            "State: $state\n" .
            "Postal Code: $postalcode\n" .
            "Phone: $phone\n" .
            "E-Mail Address: $email\n" .
            "Speciality: $speciality\n" .
            "Comments: $comments\n";


        mail ($to, $subject, $msg, 'From:' . $email);

        echo 'Thank you for submitting the CMB contact form. We will contact you back shortly. <br />';

        ?>
// EMPTY FIELD CHECK
	if( (empty($name)) || (empty($address)) || (and so on) )
	{
		echo '<script>alert("The name and address fields are required.");</script>';
		echo '<script>history.back(1);</script>';
		exit;
	} /* EMPTY FIELD CHECK */

That would work once the form is submitted... if you want to check that the fields are filled in before allowing the form to be submitted.. then you would need to have a javascript function to check that..

commented: This worked great thank you. The spry worked as well. Thanks again. +0

spry validation is the easiest way to do this,
Dreamweaver comes with this attached in "insert, form, spry validation..."
otherwise i believe programs can install the addon.

if this is only going to be a one off thing then i would suggest doing it in PHP as explained above

commented: Thanks I got it! Much appreciated. +0

When i fill the form it just goes to a blank screen and doesnt display echo. Is the if statement in the right area?

<?php
        $name = $_POST['name'] . ' ' . $_POST['lastname'];
        $practicename = $_POST['practive-name'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $postalcode = $_POST['postalcode'];
        $phonenumber = $_POST['phone'];
        $email_address = $_POST['email'];
        $speciality = $_POST['speciality'];
        $comments = $_POST['comments'];


if( (empty($name)) || (empty($address)) || ($email_address)) 
{
echo '<script>alert("The name and address fields are required.");</script>';
echo '<script>history.back(1);</script>';
exit;
}

        $to = 'droesch@parkerandpartners.com';
        $subject = 'Web contact form';
        $msg = 
            "Name: $name\n" .
            "Address: $address\n" .
            "City: $city\n" .
            "State: $state\n" .
            "Postal Code: $postalcode\n" .
            "Phone: $phone\n" .
            "E-Mail Address: $email\n" .
            "Speciality: $speciality\n" .
            "Comments: $comments\n";


        mail ($to, $subject, $msg, 'From:' . $email);

        echo 'Thank you for submitting the CMB contact form. We will contact you back shortly. <br />';

        ?>

Im guessing

if( (empty($name)) || (empty($address)) || ($email_address))

should be

if( (empty($name)) || (empty($address)) || empty($email_address)) )
if( (empty($name)) || (empty($address)) || (empty($email_address)) )

or

if((empty($name))||(empty($address))||(empty($email_address)))

or

if( (empty($name)) or (empty($address)) or (empty($email_address)) )

or

if((empty($name))or(empty($address))or(empty($email_address)))

you decide what is nicest

try this

<?php

/* Email Variables */

$webMaster = 'droesch@parkerandpartners.com';
$emailSubject = 'Web contact form';
$msg = 
"Name: $name\n" .
"Address: $address\n" .
"City: $city\n" .
"State: $state\n" .
"Postal Code: $postalcode\n" .
"Phone: $phone\n" .
"E-Mail Address: $email_address\n" .
"Speciality: $speciality\n" .
"Comments: $comments\n";


/* Data Variables */

$name = $_POST['name'] . ' ' . $_POST['lastname'];
$practicename = $_POST['practive-name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$postalcode = $_POST['postalcode'];
$phonenumber = $_POST['phone'];
$email_address = $_POST['email'];
$speciality = $_POST['speciality'];
$comments = $_POST['comments'];



$body = <<<EOD
<br><hr><br>
from: $name <br>
Email: $email_address <br>

message: $msg <br>
EOD;


$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);


/* Results rendered as HTML */

$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content="3;URL=http://yoursite.co.nz/index.html">
<style type="text/css">
<!--
body {
background-color: #000;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #CCC;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}

-->
</style>
</head>
<div align="center">Your message has been received!<br />
any questions will be answered as soon as possible<br>
You will return to our homepage in a few seconds !</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>

this will display a whole page rather than a complex javascript sentence

make sure you check your code for errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.