Contact form Problem

  • Slowm
  • Born
  • Born
  • Slowm
  • Posts: 3

Post 3+ Months Ago

Hello, I've tried to apply a contact form to my website and everything seems to be working perfectly. I can write and send messages but I can't receive them.
Can you tell me if there's something wrong with the code?

I am providing the original code without any changes made by me. And thank you for your valuable time, and answers! :)

This is the HTML
Code: [ Select ]
<form id="contact" action="#">
             <fieldset>
               <label for="name" id="name_label">Your User Name: <span class="required">*</span></label>
               <input type="text" name="name" id="name" size="50" value="" class="text-input" />
               <label for="email" id="email_label">Your Email Address: <span class="required">*</span></label>
               <input type="text" name="email" id="email" size="50" value="" class="text-input" />
               <label for="subject" id="subject_label">Subject</label>
               <input type="text" name="subject" id="subject" value="" class="text-input" />
               <label for="msg" id="msg_label">Your Message: <span class="required">*</span></label>
               <textarea cols="60" rows="10" name="msg" id="msg" class="text-input"></textarea>
               <br />
               <input type="submit" name="submit" class="button" id="submit_btn" value="Send Message &rarr;"/><br class="clear" />
             <span class="error" id="name_error">Please enter name !</span>
             <span class="error" id="email_error">Please enter email address !</span>
             <span class="error" id="email_error2">Please enter valid email address !</span>
             <span class="error" id="msg_error">Please enter message !</span>
             </fieldset>
            </form>
          </div>
  1. <form id="contact" action="#">
  2.              <fieldset>
  3.                <label for="name" id="name_label">Your User Name: <span class="required">*</span></label>
  4.                <input type="text" name="name" id="name" size="50" value="" class="text-input" />
  5.                <label for="email" id="email_label">Your Email Address: <span class="required">*</span></label>
  6.                <input type="text" name="email" id="email" size="50" value="" class="text-input" />
  7.                <label for="subject" id="subject_label">Subject</label>
  8.                <input type="text" name="subject" id="subject" value="" class="text-input" />
  9.                <label for="msg" id="msg_label">Your Message: <span class="required">*</span></label>
  10.                <textarea cols="60" rows="10" name="msg" id="msg" class="text-input"></textarea>
  11.                <br />
  12.                <input type="submit" name="submit" class="button" id="submit_btn" value="Send Message &rarr;"/><br class="clear" />
  13.              <span class="error" id="name_error">Please enter name !</span>
  14.              <span class="error" id="email_error">Please enter email address !</span>
  15.              <span class="error" id="email_error2">Please enter valid email address !</span>
  16.              <span class="error" id="msg_error">Please enter message !</span>
  17.              </fieldset>
  18.             </form>
  19.           </div>


This is the JS
Code: [ Select ]
jQuery(function() {
 jQuery('.error').hide();
 var messagetext = jQuery("textarea#msg");
 messagetext.focusout(function(){
        if (messagetext.val() == ''){messagetext.text('Message'); }
 });
 messagetext.focus(function(){
        if (messagetext.val() == 'Message') {messagetext.text(''); }                     
 });
 jQuery(".button").click(function() {
        // validate and process form
        // first hide any error messages
  jQuery('.error').hide();
     var name = jQuery("input#name").val();
        if (name=="Name" || name == "") {
   jQuery("span#name_error").show();
   jQuery("input#name").focus();
   return false;
  }
     var email = jQuery("input#email").val();
     if (email == "Email" || email == "") {
   jQuery("span#email_error").show();
   jQuery("input#email").focus();
   return false;
  }
    
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailReg.test(email)) {
    jQuery("span#email_error2").show();
  jQuery("input#email").focus();
   return false;
    }
    
     var subject = jQuery("input#subject").val();

     var msg = jQuery("textarea#msg").val();
     if (msg == "Message" || msg == "") {
     jQuery("span#msg_error").show();
     jQuery("textarea#msg").focus();
     return false;
  }
        
        var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&msg=' + msg;
        //alert (dataString);return false;
        
     jQuery.ajax({
   type: "POST",
   url: "process.php",
   data: dataString,
   success: function() {
    jQuery('#contactform').html("<div id='message'></div>");
    jQuery('#message').html("<b>Contact Form Submitted!</b>")
    .append("<p>We will be in touch soon.</p>")
    .hide()
    .fadeIn(1500, function() {
     jQuery('#message');
    });
   }
   });
  return false;
    });
});
  1. jQuery(function() {
  2.  jQuery('.error').hide();
  3.  var messagetext = jQuery("textarea#msg");
  4.  messagetext.focusout(function(){
  5.         if (messagetext.val() == ''){messagetext.text('Message'); }
  6.  });
  7.  messagetext.focus(function(){
  8.         if (messagetext.val() == 'Message') {messagetext.text(''); }                     
  9.  });
  10.  jQuery(".button").click(function() {
  11.         // validate and process form
  12.         // first hide any error messages
  13.   jQuery('.error').hide();
  14.      var name = jQuery("input#name").val();
  15.         if (name=="Name" || name == "") {
  16.    jQuery("span#name_error").show();
  17.    jQuery("input#name").focus();
  18.    return false;
  19.   }
  20.      var email = jQuery("input#email").val();
  21.      if (email == "Email" || email == "") {
  22.    jQuery("span#email_error").show();
  23.    jQuery("input#email").focus();
  24.    return false;
  25.   }
  26.     
  27.     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  28.     if(!emailReg.test(email)) {
  29.     jQuery("span#email_error2").show();
  30.   jQuery("input#email").focus();
  31.    return false;
  32.     }
  33.     
  34.      var subject = jQuery("input#subject").val();
  35.      var msg = jQuery("textarea#msg").val();
  36.      if (msg == "Message" || msg == "") {
  37.      jQuery("span#msg_error").show();
  38.      jQuery("textarea#msg").focus();
  39.      return false;
  40.   }
  41.         
  42.         var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&msg=' + msg;
  43.         //alert (dataString);return false;
  44.         
  45.      jQuery.ajax({
  46.    type: "POST",
  47.    url: "process.php",
  48.    data: dataString,
  49.    success: function() {
  50.     jQuery('#contactform').html("<div id='message'></div>");
  51.     jQuery('#message').html("<b>Contact Form Submitted!</b>")
  52.     .append("<p>We will be in touch soon.</p>")
  53.     .hide()
  54.     .fadeIn(1500, function() {
  55.      jQuery('#message');
  56.     });
  57.    }
  58.    });
  59.   return false;
  60.     });
  61. });


The PHP
Code: [ Select ]
<?php
$emailTo = 'your mail adress';
$subject = 'Template Contact Form';
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$msg=$_REQUEST['msg'];

$body = "Name: $name \n\nEmail: $email \n\nMessage: $msg";
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);
?>
  1. <?php
  2. $emailTo = 'your mail adress';
  3. $subject = 'Template Contact Form';
  4. $name=$_REQUEST['name'];
  5. $email=$_REQUEST['email'];
  6. $msg=$_REQUEST['msg'];
  7. $body = "Name: $name \n\nEmail: $email \n\nMessage: $msg";
  8. $headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
  9. mail($emailTo, $subject, $body, $headers);
  10. ?>
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

What's the code for process.php?
  • Slowm
  • Born
  • Born
  • Slowm
  • Posts: 3

Post 3+ Months Ago

Bogey wrote:
What's the code for process.php?

Hi Bogey.
Please forgive me, I totally forgot to paste the php code. I've updated the original post with said code.
Thank you for pointing out my mistake!
  • Pbpixels
  • Novice
  • Novice
  • User avatar
  • Posts: 17

Post 3+ Months Ago

Hey Slowm, on my website you can actually create your own contact form with a cms system, if you like you can try it out, it has a password but send me a pm if you want to, then I can give it to you :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Sorry for the long wait for my reply... didn't get on the computer for days and was browsing the forum on my iPhone. Much easier to reply to these kind of questions on a bigger screen and a keyboard :)

PHP Code: [ Select ]
<?php
$emailTo = 'your mail adress';
$subject = 'Template Contact Form';
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$msg=$_REQUEST['msg'];
 
$body = "Name: $name \n\nEmail: $email \n\nMessage: $msg";
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
 
mail($emailTo, $subject, $body, $headers);
?>
  1. <?php
  2. $emailTo = 'your mail adress';
  3. $subject = 'Template Contact Form';
  4. $name=$_REQUEST['name'];
  5. $email=$_REQUEST['email'];
  6. $msg=$_REQUEST['msg'];
  7.  
  8. $body = "Name: $name \n\nEmail: $email \n\nMessage: $msg";
  9. $headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;
  10.  
  11. mail($emailTo, $subject, $body, $headers);
  12. ?>


I see a number of problems with that form there. I will list them below for you.
  1. The PHP code is not very secure. In fact, it's not secure at all.
    1. You are using $_REQUEST to retrieve the data from the form.
      • I guess it may not really matter for mail since it's not changing any databases or anything like that, but I've grown used to $_POST and I guess it would be wrong to tell others that anything else is wrong, but it's better practice to not use $_REQUEST and use either $_GET or $_POST of which I recommend the latter.
    2. You are not sanitizing the user input.
      • This is a big one. Because you are not doing this, the user could manipulate the code to do whatever you don't expect them to do. Unsanitized input could really cause you problems. Never, never, never trust users on the web... don't expect the best out of people when it comes to the internet.
      • To sanitize, I usually use preg_match to make sure that all of the information entered by the user is clean from unsanitary data. I'll help you with this a bit later.
  2. You are not checking if the fields are field in.
    • This means that none of the fields are required and you could get a bunch of blank email messages. You don't really want that, do you? Just check to see if a field is empty and if it's empty then stop the script from sending email and give the user an error message.
  3. Using mail function is fine in most cases. I like to use SMTP though, as it gives (or seems to give) more control over the sending process of the email. And I like SMTP classes over the mail function because the mail function has never worked for me :lol:


I found the following function in w3schools to use to sanitize and validate an email address. I've never used it before (haven't coded for a while) but looks like it would be good practice to start using it. I would like to get a chance to test this function a bit for myself and see how it works, but I'm not on my computer and I don't want to start downloading stuff on this computer that I'm borrowing (stuff like WAMP, Notepad++, etc).
PHP Code: [ Select ]
function spamcheck($field)
{
    // filter_var() sanitizes the e-mail
    // address using FILTER_SANITIZE_EMAIL
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);
 
    // filter_var() validates the e-mail
    // address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return true;
    }
    else
    {
        return false;
    }
}
  1. function spamcheck($field)
  2. {
  3.     // filter_var() sanitizes the e-mail
  4.     // address using FILTER_SANITIZE_EMAIL
  5.     $field = filter_var($field, FILTER_SANITIZE_EMAIL);
  6.  
  7.     // filter_var() validates the e-mail
  8.     // address using FILTER_VALIDATE_EMAIL
  9.     if(filter_var($field, FILTER_VALIDATE_EMAIL))
  10.     {
  11.         return true;
  12.     }
  13.     else
  14.     {
  15.         return false;
  16.     }
  17. }


Here is a very very simplistic code to check if the field is filled in. It doesn't stop the script from sending the message though. You may want to [url=http://www.ozzu.com/php-tutorials/tutorial-sending-email-via-smtp-t104370.html]check this tutorial out[/b] about sending email VIA SMTP. Even if you don't want to use SMTP, check the tutorial out anyway (near the end) because I'm showing an example of an HTML form and a corresponding form processor for it that goes for the SMTP class I give there. But you can edit it to fit your needs... it needs very little editing to make it work without SMTP if you don't want SMTP. I'll leave this part to you.
PHP Code: [ Select ]
if(empty($field))
{
    echo 'This field is not field in';
}
  1. if(empty($field))
  2. {
  3.     echo 'This field is not field in';
  4. }
  • Slowm
  • Born
  • Born
  • Slowm
  • Posts: 3

Post 3+ Months Ago

Bogey, thank you for pointing those faults. But the fact that i can't receive any e-mail worries me more thank receiving spam mail :roll: because i'f I can't send an email to myself, i doubt that I'll receive spam! :|
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Try reading the entire post and fixing those faults and then come back if it doesn't fix your sending mail problem.

Make sure your smtp credentials are filled in at php.ini (I think it's their... Whatever the php config file is) if you insist on using the mail() function rather then using an smtp class like I have suggested.

Also, your form doesn't have a set method, so it's not sending any information to the php. I would recommend using $_POST.

Sorry, didn't notice that you validate your stuff in jQuery, but remember, JavaScript could be turned off, and then all of that validation would be rendered pointless since it just won't work.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.