Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Can't get this html/php contact form to work. Please help. I've spent hours.

    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:

    Code:
    <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:

    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?

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,949
    Thanks
    9
    Thanked 724 Times in 718 Posts
    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?

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The email address is correct.

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

  • #6
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Quote Originally Posted by obelizk View Post
    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?

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    $names $_POST['names'];
    $phone $_POST['phone'];
    $email $_POST['email_address'];
    $comment $_POST['comment']; 
    Change them to
    PHP Code:
    $names $_POST['contact-names'];
    $phone $_POST['contact-phone'];
    $email $_POST['contact-email'];
    $comment $_POST['contact-comment']; 

  • #8
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,137
    Thanks
    7
    Thanked 257 Times in 256 Posts
    You're also not checking the return status of the mail() function:
    Code:
    $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

  • #9
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Apostropartheid View Post
    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.

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

  • #10
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    PHP Code:
    $names $_POST['names'];
    $phone $_POST['phone'];
    $email $_POST['email_address'];
    $comment $_POST['comment']; 
    Change them to
    PHP Code:
    $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.

  • #11
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tracknut View Post
    You're also not checking the return status of the mail() function:
    Code:
    $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?

  • #12
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,137
    Thanks
    7
    Thanked 257 Times in 256 Posts
    Quote Originally Posted by obelizk View Post
    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:
    Code:
    $message = utf8_decode($message); 
    $status = mail($to, "Note from the Contact Form", $message, $headers);
    
    if ($status){
       echo 'sent';
    }else{
        echo 'failed';
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •