Hi:

I have created a simple contact form (see Form below), and a send_mail.php (see Send below) processor that verifies (not validates) the user has entered info all fields. Send then provides the user information and redirects them back to the contactus page. The problem I have is with the else statement; it does not echo anything back and only provides the end of the HTML page (</div></body></html>) to the user (resulting in a blank screen). The user is also not redirected back to the contact us page.

I have already tried various changes and found nothing works.

Any quidance would be appreciated.

Best Regards,
dennishall

Form:

<div>
	<div>
		<span style="font-family: Arial">
			<span style="font-size: 14px">
				<strong>
					<span style="color: #003366">
						CONTACT US
					</span>
				</strong>
			</span>
		</span>
	</div>
	<div>
		&#160;
	</div>
	<div>
		<span style="font-size: 12px">
			<span style="font-family: Arial">
				To contact us, please fill out and submit the form below. We will respond to your inquiry as soon as possible.
			</span>
		</span>
	</div>
	<div>
		<div>
			&#160;
		</div>
		<table width="100%">
			<tbody>
				<tr>
					<td valign="top" width="50%" align="left">
						<div>
							<strong>
								<span style="color: #003366">
									<span style="font-size: 14px">
										<span style="font-family: Arial">
											Location
										</span>
									</span>
								</span>
							</strong>
						</div>
						<p>
							<span style="font-size: 14px">
								<span style="font-family: Arial">
									Company name<br />
									Address line 1<br />
									Address line 2<br />
									City, State/Province<br />
									ZIP/Postal Code<br /><br />
									Phone: (###) ###-####<br />
									Fax: #-###-###-####
								</span>
							</span>
						</p>
					</td>
					<td align="left" style="font-size: 14px">
						<form method="post" action="send_mail.php" name="contact">
							<div style="text-align: center; color: #003366">
								<span style="font-size: 14px">
									<span style="font-family: Arial">
										<strong>
											All fields must be completed to send us a message.
										</strong>
									</span>
								</span>
							</div>
							<div style="text-align: left">
								Name:<br />
								<input maxlength="40" size="40" style="background-color: #efefef" name="name" type="text" />
							</div>
							<div>
								E-Mail:<br />
								<input maxlength="40" size="40" style="background-color: #efefef" name="email" type="text" />
							</div>
							<div>
								Tel:<br />
								<input maxlength="40" size="40" style="background-color: #efefef" name="phone" type="text" />
							</div>
							<div>
								Subject:<br />
								<input maxlength="40" size="40" style="background-color: #efefef" name="subject" type="text" />
							</div>
							<div>
								Message:<br />
							</div>
							<div>
								<textarea rows="12" cols="50" style="background-color: #efefef" name="message"></textarea>
							</div>
							<div style="text-align: center">
								<input type="submit" name="submit" value="Send" />
							</div>
						</form>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

Send:

<?
error_reporting(E_ALL);
ini_set('display_errors', '1');

$contact_name = $_POST['name'];
$contact_email = $_POST['email'];
$phone = $_POST['phone'];
$contact_subject = $_POST['subject'];
$contact_message = $_POST['message'];

if( $contact_name && $contact_email && $phone && $contact_subject && $contact_message == true )
	{
		$sender = $contact_email;
		$receiver = "name@domain.type";
		$email_body = "Name: $contact_name \nEmail: $contact_email \nPhone: $phone \nSubject: $contact_subject \nMessage: $contact_message \n\nEmail forwarded from – name@domain.type";
		$extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();
		$subject = $_POST['subject'];
		if( mail( $receiver, "Company name Contact Form - $subject", $email_body, $extra ) )
		{
		?>
<html>
	<head>
		<title>Contact Us Results</title>
		<meta http-equiv="refresh" content="10;URL=http://www.domain.type/contactus.php">
		<style type="text/css">
			body {background-color: #efefef;
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 20px;
			font-style: normal;
			line-height: normal;
			font-weight: normal;
			color: #123456;
			text-decoration: none;
			padding-top: 200px;
			margin-left:auto;
			margin-right:auto;
			width: 800px;
			}
		</style>
	</head>
	<body>
		<div align="center">
			<?	echo "Nous vous remercions de nous contactant à Nom du compangie.<br/>";
				echo "S'il vous plaît patienter, nous vous revenez à la page CONTACTEZ-NOUS.<br/><br/>";
				echo "Thank you for contacting us at Company name.<br/>";
				echo "Please wait, we are returning you to the CONTACT US page.";
			}
			else // else routine only presents a blank screen and does not return the user in 10 seconds
				 // when source is viewed, only the last three lines are present </div></body></html>
			{
				echo "S'il vous plaît remplir tous les champs obligatoires.<br/><br/>";
				echo "Please complete all the required fields.";
			}
		}
		?>
		</div>
	</body>
</html>

Recommended Answers

All 11 Replies

The else belongs to the if(mail), just like your redirect. You need to redo your logic.

Thanks for your thoughts on this pritaeas, however, each time I attempt to do this, all I keep getting is a 500 response (with our without user input being correct) from my host - not even an error log.

I tried moving the else statement previously (changing the logic) to no avail.

I have done if else statements before without any problems, but want this one to have a formated output.

The true output works, why would the false output not work in my case below?

Can you provide me a specific example (I can get generic examples anywhere)?

Best Regards,
dennishall

Line 11 - This line is failing every time. The reason is that you have:

&& $contact_message == true)

I think this might be actually checking to see if the value is "true". Try making it

if( trim($contact_name) && trim($contact_email) && trim($phone) && trim($contact_subject) && trim($contact_message))

The trim tag will remove all spaces and see if there are any characters present. It will return either true or false (which is what you really want). If you want more info on the "trim" function you can look it up here: http://us2.php.net/manual/en/function.trim.php

You also really need to move line 11-20 to line 42 (inside your <body> tag). Because if line 11 or 18 fails then the <html> and <body> tags will not be written, hence the screen is only outputting "</div></body></html>".

I hope this helps! Have a good one!

Hi Caeon:

Thanks for the detailed reply. This helped :)
The rsulting output generated when a user enters nothing in the form and clicks "Submit" now displays the full html output and redirects back to the contact us page after 10 seconds, however, the else statement is still not processed.

As I led to in my fist post, it seems the else process is not performed.

I have posted the changed php source for your reference.
Send:

<?
$contact_name = $_POST['name'];
$contact_email = $_POST['email'];
$phone = $_POST['phone'];
$contact_subject = $_POST['subject'];
$contact_message = $_POST['message'];
?>

<html>
	<head>
		<title>Contact Us Results</title>
		<meta http-equiv="refresh" content="10;URL=http://www.domain.type/contactus.php">
		<style type="text/css">
			body {background-color: #efefef;
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 20px;
			font-style: normal;
			line-height: normal;
			font-weight: normal;
			color: #123456;
			text-decoration: none;
			padding-top: 200px;
			margin-left:auto;
			margin-right:auto;
			width: 800px;
			}
		</style>
	</head>
	<body>
		<div align="center">
			<?
				if( trim($contact_name) && trim($contact_email) && trim($phone) && trim($contact_subject) && trim($contact_message))
					{
						$sender = $contact_email;
						$receiver = "name@domain.type";
						$email_body = "Name: $contact_name \nEmail: $contact_email \nPhone: $phone \nSubject: $contact_subject \nMessage: $contact_message \n\nEmail forwarded from – name@domain.type";
						$extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();
						$subject = $_POST['subject'];
					if( mail( $receiver, "Company name Contact Form - $subject", $email_body, $extra ) )
						{
							echo "Nous vous remercions de nous contactant à Nom du compangie.<br/>";
							echo "S'il vous plaît patienter, nous vous revenez à la page CONTACTEZ-NOUS.<br/><br/>";
							echo "Thank you for contacting us at Company name.<br/>";
							echo "Please wait, we are returning you to the CONTACT US page.";
						}
					else
						{
							echo "S'il vous plaît remplir tous les champs obligatoires.<br/><br/>";
							echo "Please complete all the required fields.";
						}
					}
			?>
		</div>
	</body>
</html>

Thanks for getting me to this point, it really did help. Any furthur input would be great.

Still, the else belongs to the line with the if(mail()). Probably an additional closing curly bracket before the else fixes this.

Oh! Ok! I understand your problem now! =P You simply don't have your if-else statement nested quite correctly as Pritaeas is saying.

It should be:

<?
$contact_name = $_POST['name'];
$contact_email = $_POST['email'];
$phone = $_POST['phone'];
$contact_subject = $_POST['subject'];
$contact_message = $_POST['message'];
?>

<html>
	<head>
		<title>Contact Us Results</title>
		<meta http-equiv="refresh" content="10;URL=http://www.domain.type/contactus.php">
		<style type="text/css">
			body {background-color: #efefef;
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 20px;
			font-style: normal;
			line-height: normal;
			font-weight: normal;
			color: #123456;
			text-decoration: none;
			padding-top: 200px;
			margin-left:auto;
			margin-right:auto;
			width: 800px;
			}
		</style>
	</head>
	<body>
		<div align="center">
			<?
				if( trim($contact_name) && trim($contact_email) && trim($phone) && trim($contact_subject) && trim($contact_message))
				{
					$sender = $contact_email;
					$receiver = "name@domain.type";
					$email_body = "Name: $contact_name \nEmail: $contact_email \nPhone: $phone \nSubject: $contact_subject \nMessage: $contact_message \n\nEmail forwarded from – name@domain.type";
					$extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();
					$subject = $_POST['subject'];
					if( mail( $receiver, "Company name Contact Form - $subject", $email_body, $extra ) )
					{
						echo "Nous vous remercions de nous contactant à Nom du compangie.<br/>";
						echo "S'il vous plaît patienter, nous vous revenez à la page CONTACTEZ-NOUS.<br/><br/>";
						echo "Thank you for contacting us at Company name.<br/>";
						echo "Please wait, we are returning you to the CONTACT US page.";
					}
				}
				else
					{
						echo "S'il vous plaît remplir tous les champs obligatoires.<br/><br/>";
						echo "Please complete all the required fields.";
					}
				}
			?>
		</div>
	</body>
</html>

Hope this helps! Have a good one!

Remove line 52.

Yeah, good call. The indent screwed me up. =P

Thanks to both of you :) as the correction was the else statement which I did intially have outside the if double nesting but prior to this post had removed it ~!@#$%^&*()

I greatly appreciate the help from both of you - Caeon for helping me rearrange my script to make more sense and pritaeas for helping me make more sense out of my arrangement :) And removing line 52 :)

You both have solved this delema for me. Now I just gotta figure how to give you both credit in DaniWeb ???

Although it may not seem obvious here, I actually do have some talents in PHP, JavaScript, MySQL, Flash and any compination of them all together. If you need help with anything related, please ask.

Best Regards,
dennishall

Now I just gotta figure how to give you both credit in DaniWeb ???

You can choose to upvote our answers and/or add reputation. You see the choice if you click on the arrow-up button.

if condition should be like this. try this

if( $contact_name== true && $contact_email== true && $phone== true && $contact_subject== true && $contact_message == true )
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.