...

View Full Version : Can't get this html/php contact form to work. Please help. I've spent hours.



obelizk
02-03-2012, 05:33 PM
Hey guys.

I'm having a lot of trouble trying to figure out why my contact form on my website isn't working. The layout of it looks great, and when I click send, a message pops up that says 'message sent,' but no e-mails go through to my e-mail. I'd appreciate help with this. I'm not the best at this stuff, but I've spent a couple hours trying to find out what was wrong, and really couldn't.

This is the HTML code for the contact form:


<div class="grid_6 alpha">
<form id="contact-form" method="post" name="contact-form" class="infield rounded-fields form-preset">
<p><span class="note">All fields are required!</span>
<p><label for="name">Your Name</label>
<input class="text" name="contact-names" type="text" id="name" />
<span class="name-required"></span></p>
<p><label for="phone">Your Phone</label>
<input class="text" name="contact-phone" type="text" id="phone" />
<span class="phone-required"></span></p>
<p><label for="email">Your Email</label>
<input class="text" name="contact-email" type="text" id="email" />
<span class="email-required"></span></p>
<p><label for="message" class="message-label">Message</label>
<textarea class="text-area" name="contact-comment" id="message"></textarea>
<span class="comment-required"></span></p>
<p><input class="send" id="submit-form" name="submit" type="submit" value="Send" /></p>
</form>
<div class="clear"></div>
</div>

and this is the PHP code:


<?php
$names = $_POST['names'];
$phone = $_POST['phone'];
$email = $_POST['email_address'];
$comment = $_POST['comment'];
$to ='MY EMAIL';

$message = "";
$message .= "*Name: " . htmlspecialchars($names, ENT_QUOTES) . "<br>\n";
$message .= "*phone: " . htmlspecialchars($phone, ENT_QUOTES) . "<br>\n";
$message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
$message .= "Comment: " . htmlspecialchars($comment, ENT_QUOTES) . "<br>\n";
$lowmsg = strtolower($message);

$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"" . $names . "\" <" . $email . ">\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$message = utf8_decode($message); mail($to, "Note from the Contact Form", $message, $headers);

if ($message){
echo 'sent';
}else{
echo 'failed';
}
?>

Is there anything wrong with these?

Rowsdower!
02-03-2012, 06:09 PM
Unless I'm missing it, nothing in your provided code would produce the text "message sent" and nothing would produce a popup element (or a popup window) at all.

This probably is not an issue with HTML/CSS (more than likely a problem in your PHP). Is this all of the relevant code?

obelizk
02-03-2012, 06:19 PM
Yes this is all the relevant code. I actually was working with a web designer, and we tested the contact form when he hosted the files on his website, and it worked, but it isn't working on mine.

I have tested to make sure my hosting allows me to generate emails with PHP, and it does.

SB65
02-03-2012, 06:34 PM
Just been looking at this as well and I can't see any problem with the code. Copying and pasting this works fine for me. Might be worth checking the obvious things first - is it sending to the correct email address? has the email been filtered by out as spam?

obelizk
02-03-2012, 07:12 PM
The email address is correct.

It's crazy that it's working for you. Is there anything else I can check?

Apostropartheid
02-03-2012, 07:38 PM
The email address is correct.

It's crazy that it's working for you. Is there anything else I can check?

Are you sure email is set up properly on your server? Often the host will provide a webmail interface. Can you successfully send mail from that?

Nightfire
02-03-2012, 07:49 PM
$names = $_POST['names'];
$phone = $_POST['phone'];
$email = $_POST['email_address'];
$comment = $_POST['comment'];

Change them to


$names = $_POST['contact-names'];
$phone = $_POST['contact-phone'];
$email = $_POST['contact-email'];
$comment = $_POST['contact-comment'];

tracknut
02-03-2012, 08:39 PM
You're also not checking the return status of the mail() function:


$message = utf8_decode($message); mail($to, "Note from the Contact Form", $message, $headers);

if ($message){
echo 'sent';
}else{
echo 'failed';
}


Your "if ($message)..." is testing to see if there was content in the message body, not what value mail() returned.

Dave

obelizk
02-03-2012, 09:55 PM
Are you sure email is set up properly on your server? Often the host will provide a webmail interface. Can you successfully send mail from that?

I used this code to test the e-mail capability, and it worked - it sent me an e-mail.


<?php
mail('MYEMAIL@gmail.com','Test mail','The mail function is working!');
echo 'Mail sent!';
?>

obelizk
02-03-2012, 09:56 PM
$names = $_POST['names'];
$phone = $_POST['phone'];
$email = $_POST['email_address'];
$comment = $_POST['comment'];

Change them to


$names = $_POST['contact-names'];
$phone = $_POST['contact-phone'];
$email = $_POST['contact-email'];
$comment = $_POST['contact-comment'];


I also changed this to what you said, and it didn't seem to make a difference.

obelizk
02-03-2012, 09:57 PM
You're also not checking the return status of the mail() function:


$message = utf8_decode($message); mail($to, "Note from the Contact Form", $message, $headers);

if ($message){
echo 'sent';
}else{
echo 'failed';
}


Your "if ($message)..." is testing to see if there was content in the message body, not what value mail() returned.

Dave

Will this fix my problem? or is this something else? What should I change it to?

tracknut
02-03-2012, 10:50 PM
Will this fix my problem? or is this something else? What should I change it to?

I don't know if it will fix it, but it certainly means the "sent" message you were getting was not telling you whether the message was or was not sent. Change it to:


$message = utf8_decode($message);
$status = mail($to, "Note from the Contact Form", $message, $headers);

if ($status){
echo 'sent';
}else{
echo 'failed';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum