Copy link to clipboard
Copied
I have been trying for ages to create a Contact Form and recently discovered a followable tutorial on Youtube. I know that they cannot be 100% trusted.
Can someone please check out the code below and tell me why I am not receiving emails when I do a test. I have crossed out my email address for obvious reasons and this I have checked and correct.
I like the way this looks - www.ultimate-top-trumps.co.uk/contact.php and would like to keep it as close as possible. I do have a CSS file but its not this hence I have pasted in the php script page.
I have used Dreamweaver CS6 and using a Mac.
Any help would be greatly appreciated!
<?php
// Set email variables
$email_to = 'xxxxxxx';
$email_subject = 'Form submission';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<!-- Contact Form Designed by James Brand @ dreamweavertutorial.co.uk -->
<!-- Covered under creative commons license - http://dreamweavertutorial.co.uk/permissions/contact-form-permissions.htm -->
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="contact/css/contactform.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js"></script>
<script type="text/javascript" src="contact/validation/validation.js"></script>
<script type="text/javascript">
var nameError = '<?php echo $error_messages['fullname']; ?>';
var emailError = '<?php echo $error_messages['email']; ?>';
var commentError = '<?php echo $error_messages['comment']; ?>';
</script>
</head>
<body onload="MM_preloadImages('contact.images/x.png')">
<div id="formwrap">
<h2> We appreciate your feedback.</h2>
<div id="form">
<?php if($form_complete === FALSE): ?>
<form action="contact.php" method="post" id="comments_form">
<div class="row">
<div class="label">Your Name</div> <!--end .label -->
<div class="input">
<input type=="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>
"/><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>
</div><!-- end .input -->
<div class="context">e.g. John Smith or Jane Doe</div><!-- end .context -->
</div><!--end .row -->
<br>
<div class="row">
<div class="label">Your Email Address</div> <!--end .label -->
<div class="input">
<input type=="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>"/><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>
</div><!-- end .input -->
<div class="context">We will never share your details</div><!-- end .context -->
</div><!--end .row -->
<br>
<div class="row">
<div class="label">Your Message</div> <!--end .label -->
<div class="iput2">
<textarea id="comment" name="comment" class="mess"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?>
</textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>
</div><!-- end .input -->
</div><!--end .row -->
<div class="Submit">
<p>
<input type ="submit" id="submit" name="submit" value="Send Message"/>
</p>
</div><!-- end .submit -->
</form>
<?php else: ?>
<p style="font-size:18px; font-family:Verdana, Geneva, sans-serif; font-weight:bold; color:#000; margin-left:25px;">Thank you for your Message!</p>
<script type="text/javascript">
setTimeout ('ourRedirect()', 5000)
function ourRedirect(){
location.href='index.html'
}
</script>
<?php endif; ?>
</div><!--end of form --></div>
<p> </p>
<p> </p>
</body>
</html>
1 Correct answer
nigelh70638339 wrote
I asked 123 last week and they told me that the code is wrong and basically, tough, just as you said osgood.
There's nothing wrong with the code, tell 123 you have tested it along with other php mail scripts on several different php servers where the code works. If it works for markw39954149 and myself that's conclusive proof.
The problem is you are trying to run php on a Windows server which is not its native environment, that's a problem, especailly if the host refuses to ac
...Copy link to clipboard
Copied
First. Have you uploaded the file to your remote server and tested it? This wont work if you are testing it locally.
Second. Are you sure your server supports php and the php 'mail' function?
The code works just fine for me. It's a bit OTT (over the top) in my opinion, but works.
Copy link to clipboard
Copied
Yes it is all uploaded but not yet available to use as I have not linked to it.
My host is 123 and they told me that they have run a php script and things work for them.
I have done about 8 tests since it has been online and received nothing back. I have checked my spam also.
Copy link to clipboard
Copied
Can you test with this much simpler php script just to confirm your server is actually set up correctly to process php/php mail function.
Copy, paste into a new DW file and save as feedback_form.php then upload it to your server. Browse out to it and fill in the test fields and click submit - see if you get any response - (Obviously insert your email address where if says RECIPIENT_EMAIL_ADDRESS_HERE in the code below, line 6.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "RECIPIENT_EMAIL_ADDRESS_HERE";
$subject = "Message from your website";
$body = "$subject\n\n";
$body .= "From: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";
mail($recipient, $subject, $body);
echo "<script type='text/javascript'>
setTimeout ('ourRedirect()', 5000);
function ourRedirect(){
location.href='index.html'
}
</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Test</title>
</head>
<body>
<form name="feedback" action="feedback_form.php" method="post">
<div>
<label for="name">Your Name</label>
<input type="text" name="name" id="name">
</div>
<div>
<label for="email">Your Email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="message">Your Message</label>
<input type="text" name="message" id="message">
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Copy link to clipboard
Copied
Hi Osgood
Nope no email.
Copy link to clipboard
Copied
If you're using an email on your hosting server, check that your spam filtering isn't set above normal. That could kick it out before it ever reaches your inbox.
Or try using a different email address.
Copy link to clipboard
Copied
I am using Hotmail.co.uk. I have not changed any settings or indeed provider.
Copy link to clipboard
Copied
If your server doesn't have support for the PHP mail () function, you'll need a different script to send emails.
Ask your web host which form-to-email script they recommend you use on your particular hosting plan.
Copy link to clipboard
Copied
nigelh70638339 wrote
I am using Hotmail.co.uk. I have not changed any settings or indeed provider.
I would not use something like Hotmail. If you have hosting then set up a proper email address like - someone@your_domain_name.co.uk
I would think its definitely an issue with your email address, assuming your host can process php and you are allowed to use the php mail function
or try setting up a Google email account - I have a gmail email address for testing and I get any mail sent from web forms ok
Copy link to clipboard
Copied
osgood_ wrote
nigelh70638339 wrote
I am using Hotmail.co.uk. I have not changed any settings or indeed provider.
I would not use something like Hotmail. If you have hosting then set up a proper email address like - someone@your_domain_name.co.uk
I agree. It's worth trying. Some hosting plans insist that you use an email address on the named server.
Copy link to clipboard
Copied
OK, well as always some great advice.
I will set my address to my domain name as suggested and will test this out.
I will let you know in the morning.
Again, thanks for all the help!
Copy link to clipboard
Copied
Tried using the email account I have with 123 and nothing back.
I cannot believe how frustrating this is!
Copy link to clipboard
Copied
nigelh70638339 wrote
Tried using the email account I have with 123 and nothing back.
I cannot believe how frustrating this is!
I'm not sure what to suggest. If below is the 123 you are refering to then try their simple php script:
How to use PHP scripting for a contact form | 123-reg Support
Copy link to clipboard
Copied
I found out some more info about your host server - it is a Windows server:
Microsoft-IIS/7.5 .
Although it says its technologies is PHP/5.6.27 I have my doubts whether its set up correctly to run all php.
Have you tried a simple page test:
Copy and paste the below into a completely blank DW file, save as php_test.php and upload to server, browse to it and see what the results are.
<?php
echo "Hello World";
?>
I'm never keen on Windows hosting which runs php. If the people who manage the server are clueless they can be a bit of a problem to configure.
Copy link to clipboard
Copied
The test php did work showing the words Hello World, nothing else.
I will give the 123 script a test.
Copy link to clipboard
Copied
nigelh70638339 wrote
The test php did work showing the words Hello World, nothing else.
I will give the 123 script a test.
We'll hell, if I was clear headed, your server of course must run some php because your form actually shows BUT I still have some lingering doubts as to whether the php mail function configuration is set up correctly on your server.
I've tested the original php script you posted and the one I posted on 3 php enabled servers and they ALL work as I would expect, given the php mail function is supported.
Copy link to clipboard
Copied
I tried the script from 123 and when I opened the page expecting the form it went straight to my redirect page,my homepage. BUT I did receive the emails but as you would expect they were blank, showing just the field names with no info, as the page did not even appear.
So yes all seems to be working in a way so it has to be something I ma doing wrong, but for the love of money I have no idea what I am doing wrong.
Copy link to clipboard
Copied
You need to wrap the php code in an if statement to stop it running on page load, so it only executes when the forms submit button is clicked.
So if your form submit button has a 'name' attribute = to 'submit' like below:
<input type ="submit" id="submit" name="submit" value="Send Message"/>
You wrap the php form processing code as below: (so all of the php code, apart from the opening and closing <?php ?> tags go between the red brackets:
<?php
if(isset($_POST['submit'])) {
ALL YOUR PHP PROCESSING CODE GOES HERE
}
?>
Copy link to clipboard
Copied
Osgood, I tried that code but I just got a blank page. It didn't redirect either.
Copy link to clipboard
Copied
nigelh70638339 wrote
Osgood, I tried that code but I just got a blank page. It didn't redirect either.
Can you post the php code and the form code you are using in the forum so I can take a look at how you have it set up.
If set up correctly it should work ok, it does for me. You have already stated that the php code does work, so now you have to stop it running until the form submit button is clicked....
Copy link to clipboard
Copied
Here is the code from 123. I hope that I can keep the layout of the original.
<?php
$EmailFrom = "info@yourdomain.co.uk";
$EmailTo = "info@yourdomain.co.uk";
$Subject = "online form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://yourdommain.co.uk/contact.php");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=index.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=index.php\">";
}
?>
Thank you Osgood!
Copy link to clipboard
Copied
Yes, I know what that code looks like BUT what code are you using? Can you post the code from your page, including your form html - the code you say is not working after wrapping it in....
<?php
if(isset($_POST['submit'])) {
ALL YOUR PHP PROCESSING CODE GOES HERE
}
?>
Copy link to clipboard
Copied
Oh, right! Well the form code is at the start of thread, the validation code is
window.addEvent('domready', function() {
// Get the form
var form = $('comments_form');
// if the form is found...
if (form) {
// obtain error fields
var name = $('fullname');
var email = $('email');
var comment = $('comment');
// Set the default status
var isValid = true;
// input error function for the error messages
var addError = function (field, msg) {
field.addClass('error'); // Add error class to field
var error = field.getParent().getElement('span') || new Element('span', {'class': 'error'}); // add error message if not already placed
error.set('text', msg); // error text msg
error.inject(field, 'after'); // Insert error message after field
};
// detach error function used to delete any error messages and remove the error class
var removeError = function (field) {
field.removeClass('error'); // Remove error class from form fields
var error = field.getParent().getElement('span'); // find any existing error messages
// destroy if error message
if (error) {
error.destroy();
}
};
// insert submit form event
form.addEvent('submit', function (e) {
// Test name length
if (name.get('value').length === 0) {
isValid = false;
addError(name, nameError);
} else {
isValid = true;
removeError(name);
}
// check email length
if (email.get('value').length === 0) {
isValid = false;
addError(email, emailError);
// check email validity
} else if (!email.get('value').test(/^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/)) {
isValid = false;
addError(email, emailError);
} else {
isValid = true;
removeError(email);
}
// check comment length
if (comment.get('value').length === 0) {
isValid = false;
addError(comment, commentError);
} else {
isValid = true;
removeError(comment);
}
// If form invalid then stop event happening
if (!isValid) {
e.stop();
}
});
}
});
and the css is
body | {background:#F0F2F2; |
}
#formwrap {width:720px;
margin-top:30px; | |||
margin-left:30px; | |||
background:#FFF; | |||
border:1px solid #000; | |||
border-radius:20px; | |||
box-shadow:2px 2px 5px #666; | |||
padding:20px; |
}
#formwrap #form {border-top:1px solid #EEE;
width:720px; |
}
#form.row {border-bottom:1px dotted #EEE;
display:block; | |||
line-height:38px; | |||
overflow:auto; | |||
padding:24px 0px; | |||
width:100%; |
}
#form .row .label {font-size:14px;
font-weight:bold; | |||||
font-family:Verdana, Geneva, sans-serif; | |||||
width:180px; | |||||
text-align:right; | |||||
float:left; | |||||
padding-right:10px; | |||||
margin-right:10px; |
}
#form .row .input {float:left;
margin-right:10px; | |||||
/*width:auto;*/ | |||||
width:285px; |
}
}
#form .row .input2 {float:left;
margin-right:10px; | |||||
/*width:auto;*/ | |||||
width:466px; |
}
.detail | {width:260px; | ||
font-family:Verdana, Geneva, sans-serif; | |||
font-size:16px; | |||
padding:7px 8px; | |||
margin:0; | |||
display:block; | |||
border-radius:5px; | |||
background:#E9E9E9; | |||
border:1px solid #000; |
}
.mess | {width:450px; | ||
max-width:450px; | |||
height:280px; | |||
overflow:auto; | |||
font-family:Verdana, Geneva, sans-serif; | |||
font-size:16px; | |||
padding:7px 8px; | |||
line-height:16px; | |||
margin:0; | |||
display:block; | |||
border-radius:5px; | |||
background:#E9E9E9; | |||
border:1px solid #000; |
}
.detail:focus | {background-color:#FFF; | ||||
border:1px solid #999; | |||||
outline:none; |
}
.mess:focus | {background-color:#FFF; | ||||
border:1px solid #999; | |||||
outline:none; |
}
#form .row .context {color:#999;
font-size:11px;
font-style:italic;
line-height:14px;
font-family:Verdana, Geneva, sans-serif;
width:200px;
}
#form #submit {font-family:Verdana, Geneva, sans-serif;
margin-top:25px;
margin-left:200px;
color:#000;
font-size:16px;
text-shadow:1px 1px 1px #999;
padding:10px;
}
span.error | {color:rgba(0,0,0,1); | |||
display:block; | ||||
font-family:Verdana, Geneva, sans-serif; | ||||
font-size:12px; | ||||
background-image:url(../images/x.png); | ||||
background-repeat:no-repeat; | ||||
background-position: left 6px; | ||||
padding-left:25px; | ||||
line-height:30px; |
}
#formwrap h2 | {font-family:Verdana, Geneva, sans-serif; |
text-shadow:1px 1px 1px #333;
color:rgba(0,0,0,1);
margin-left:25px;
}
Hope something here shows what might be the cause!
Copy link to clipboard
Copied
No, nothing. I dont want to see the javascript or your css.
All I need is for you to provide the code for the page that you used when you said you had recieved a blank email.
Copy link to clipboard
Copied
Sorry!
<?php
if(isset($_POST['submit'])) {
$EmailFrom = "name@email_domain.com";
$EmailTo = "name@email_domain.com";
$Subject = "online form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://yourdommain.co.uk/contact.php");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=index.html\">";
}
}
?>


-
- 1
- 2