Jump to content

New Field Added To Contact Form Doesn't Work


404

Recommended Posts

I'm working on a Contact Form for my website. It works, except the "Phone" field doesn't show a phone number in the sent message. It's just blank where the number should be displayed.

What am I doing wrong?

- Thanks

 

HTML:

 <div class="contact-form">
				  	<h2 class="style">Contact Us</h2>
					       <form action="contact.php" method="post" id="form">
					    	<div>
						    	<span><label>NAME</label></span>
						    	<span><input name="cf_name" type="text" class="textbox"></span>
						    </div>
						    <div>
						    	<span><label>EMAIL</label></span>
						    	<span><input name="cf_email" type="text" class="textbox"></span>
						    </div>
						    <div>
						     	<span><label>PHONE</label></span>
						    	<span><input name="cf_phone" type="text" class="textbox"></span>
						    </div>
						    <div>
						    	<span><label>MESSAGE</label></span>
						    	<span><textarea name="cf_message"> </textarea></span>
						    </div>
						   <div>
						   		<span><input type="submit" value="Send"></span>
						  </div>
					    </form>

				    </div>

PHP:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_phone = $_POST['cf_phone'];
$field_message = $_POST['cf_message'];

$mail_to = 'somebody@invalid.com';
$subject = 'Message from a website visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		alert('Thank you for the message. We will contact you shortly.');
		window.location = 'index.html';
	</script>
<?php

 

Link to comment
Share on other sites

Thanks for responding. I made a typo in the code ...it's working now :).

The only thing that doesn't seem to work is the error section at the bottom of the PHP code (which I left out in my previous post - oops!). If the sender doesn't fill out any field correctly or even leaves it blank, the form sends the message anyway. Here's the missing bit of PHP:

}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Message failed. Please try again or contact us directly by phone.');
		window.location = 'index.html';
	</script>
<?php
}
?>

 

Edited by 404
Link to comment
Share on other sites

All the required parameters in mail() are filled (To, Subject, Message). It does not matter if any field form input is empty or wrong, it does not know that! If these have anything the mail is sent. You have to validate all the data before its allowed to get to mail() function.

Link to comment
Share on other sites

Thanks ...I guess that should have been obvious 🙂. Do you know of a tutorial somewhere online that would help me with adding the proper validation?

Edited by 404
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...