0

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 />';

        ?>

Edited by mike_2000_17: Fixed formatting

4
Contributors
6
Replies
23
Views
7 Years
Discussion Span
Last Post by metalix
2
// 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..

Votes + Comments
This worked great thank you. The spry worked as well. Thanks again.
1

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

Votes + Comments
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 />';

        ?>

Edited by mike_2000_17: Fixed formatting

0

Im guessing

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

should be

if( (empty($name)) || (empty($address)) || empty($email_address)) )
0
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

Edited by lat78: forgot something impotent

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.