Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Required field validation not working

Featured Replies

Hi. I really know little to nothing about PHP at all. My limit is to more or less understand one PHP mail script I have except for this bit which is annoying me. I have a contact form on my website and a php file to which the form gets posted to (i dont even know the correct terminologies here).

 

This all works, however I'm modifying it for another form. It's nearly identical but on the new form i want 3 required fields and not 2 like in my old working form.

 

Bellow, you'll see the code as it was which works and then the new code which doesn't work. It gives me the following error:

"Parse error: syntax error, unexpected $end in mail.php on line 36".

 

code that works with 2 required fields.

<?php
$to = "my@email.com";
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];
$message = $_REQUEST['Message'];
$headers = "From: $from";
$subject = "Website Enquiry";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Organization"} = "Organization";
$fields{"City"} = "City";
$fields{"Message"} = "Message";

$body = "Details submitted:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: my@email.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us";
if($name == '') {print "You have not entered a name, please go back and try again.";}
else {
if($from == '') {print "You have not entered an email address, please go back and try again.";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.website.com/sent.php" );}
else
{print "There was an error sending your request. Please try contacting us direct at my@email.com"; }
}
}
?>

 

 

code that doesn't work with 3 required fields

<?php
$to = "my@email.com";
$from = $_REQUEST['Email'];
$name = $_REQUEST['Name'];
$message = $_REQUEST['Message'];
$headers = "From: $from";
$subject = "Website Enquiry";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Organization"} = "Organization";
$fields{"City"} = "City";
$fields{"Message"} = "Message";

$body = "Details submitted:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: my@email.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us";
if($message == '') {print "You have not entered any message.";}
else {
if($name == '') {print "You have not entered a name, please go back and try again.";}
else {
if($from == '') {print "You have not entered an email address, please go back and try again.";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.website.com/sent.php" );}
else
{print "There was an error sending your request. Please try contacting us direct at my@email.com"; }
}
}
?>

 

The only difference is that in the newer version, I've added:

if($message == '') {print "You have not entered any message.";}
else {

 

...and as someone who knows nothing about PHP, it has me stumped why this won't work. Surely it's an exact copy of the other two required fields validators that are there. Any help is greatly appreciated!

if ($message == '') {
print "You have not entered any message.";
} else {
if ($name == '') {
	print "You have not entered a name, please go back and try again.";
} else {
	if ($from == '') {
		print "You have not entered an email address, please go back and try again.";
	} else {
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);

		if ($send) {
			header("Location: [url="http://www.website.com/sent.php&quot%3b%29;"]http://www.website.com/sent.php");[/url]
		} else {
			print "There was an error sending your request. Please try contacting us direct at my@email.com"; 
		}
	}
}

 

Above is your current code, as you can see with proper formatting it doesn't contain enough "}" closing brackets.

 

I think the code below should fix your problem.

 

if ($message == '') {
print "You have not entered any message.";
} else {
if ($name == '') {
	print "You have not entered a name, please go back and try again.";
} else {
	if ($from == '') {
		print "You have not entered an email address, please go back and try again.";
	} else {
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);

		if ($send) {
			header("Location: http://www.website.com/sent.php");
		} else {
			print "There was an error sending your request. Please try contacting us direct at my@email.com"; 
		}
	}
}
}

 

Anyway, you're new to this so I'd recommend you always format your code properly it makes reading easier and allows for you to spot small mistakes better.

 

You could always use

 

if (!empty($message)) {

 

instead of

 

if ($message == '') {

 

Anyway keep coding and have fun :)

  • Author
if ($message == '') {
print "You have not entered any message.";
} else {
if ($name == '') {
	print "You have not entered a name, please go back and try again.";
} else {
	if ($from == '') {
		print "You have not entered an email address, please go back and try again.";
	} else {
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);

		if ($send) {
			header("Location: [url="http://www.website.com/sent.php&quot%3b%29;"]http://www.website.com/sent.php");[/url]
		} else {
			print "There was an error sending your request. Please try contacting us direct at my@email.com"; 
		}
	}
}

 

Above is your current code, as you can see with proper formatting it doesn't contain enough "}" closing brackets.

 

I think the code below should fix your problem.

 

if ($message == '') {
print "You have not entered any message.";
} else {
if ($name == '') {
	print "You have not entered a name, please go back and try again.";
} else {
	if ($from == '') {
		print "You have not entered an email address, please go back and try again.";
	} else {
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);

		if ($send) {
			header("Location: http://www.website.com/sent.php");
		} else {
			print "There was an error sending your request. Please try contacting us direct at my@email.com"; 
		}
	}
}
}

 

Anyway, you're new to this so I'd recommend you always format your code properly it makes reading easier and allows for you to spot small mistakes better.

 

You could always use

 

if (!empty($message)) {

 

instead of

 

if ($message == '') {

 

Anyway keep coding and have fun :)

 

Thanks very much, can't add this right now but will tomorrow. I'll let you know if it fixes it. Much appreciated!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.