Jump to content

New Field Added To Contact Form Doesn't Work


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...