Page 1 of 1

Php contact form

Posted: Sun Aug 28, 2011 9:07 am
by Webbyturtle
I'm learning PHP to enable my contact form to work. It seems like I am almost there - when the user clicks submit it generates an email to my account, but it doesn't give the user the "Thank you your email was sent" or refresh the form. Both pages below are located in root folder of server site. If anyone can take a look I would appreciate any comments.

Here's the code on my contact.html page:

    <script src="js/jquery-1.4.2.min.js"></script> <!-- jquery library -->
    <script src="js/jquery.prettyPhoto.js"></script> <!-- lightbox plugin -->
    <script src="js/jquery.nivo.slider.pack.js"></script> <!-- nivo slider plugin -->
    <script src="js/cufon-yui.js"></script><!-- cufon library -->
    <script src="js/tweets.js"></script><!-- twitter plugin -->
    <script src="js/Vegur_300-Vegur_700.font.js"></script><!-- vegur font -->
   <script src="js/jquery.form.js"></script>   <!-- jQuery form plugin-->
    <script src="js/custom.js"></script><!-- custom jquery stuff-->
   <link rel="stylesheet" href="style.css" type="text/css" media="screen" /><!-- general stylesheet -->
   <link rel="stylesheet" href="custom.css" type="text/css" media="screen" /><!-- custom stylesheet -->
   
   <!-- version number: 1.0.0 -->

</head>
<body>
<div id="header">
   <div class="wrapper">
      <h1 id="logo"></h1>
      <ul id="navigation">
         <li><a href="index.html">Home</a></li>
         <li><a href="portfolio.html">Work</a></li>
         <li><a href="contact.html">Contact</a></li>
         <li><a href="about.html">About</a></li>
         <li><a href="blog.html/">Blog</a></li>
      </ul><!-- end of #navigation -->
   </div><!-- end of .wrapper -->
</div> <!-- end of #header -->

<div id="slice1" class="slice fabric-slice slice-light"> <!-- a normal slice used to create a contact form -->
   <div class="wrapper clearfloat">         
      <div class="slice-caption clearfloat">
         <h1 class="slicetitle "><span>Contact</span></h1>
      </div>
      <br class="clear">
            <script type="text/javascript" charset="utf-8">
                  $(document).ready(function(){
                        $('#contactForm').ajaxForm(function(data) {
                           if (data==1){
                              $('#bademail').fadeOut("fast");
                              $('#success').fadeIn("slow");
                              $('#myForm').resetForm();
                           }
                           else if (data==2){
                              $('#bademail').fadeOut("fast");
                              $('#badserver').fadeIn("slow");
                           }
                           else if (data==3)
                           {
                              $('#bademail').fadeIn("slow");
                           }
                        });
                     });
               </script>
      <div class="twocol-one">
               <p id='success' style="display:none;">Your email is sent! Thanks!</p>
               <p id='bademail' style="display:none;">Your email could not be sent. Please fill in the data correctly.</p>
               <p id='badserver' style="display:none;">Your email failed due to server error. Try again later.</p>
                  <form action="contact_send.php" id="contactForm" method="post">

                        <ol class="forms">
                           <li><label for="contactName">Name:</label>
                              <input type="text" name="contactName" id="contactName" value="" class="requiredField" />
                           </li>
                           
                           <li><label for="email">Email:</label>
                              <input type="text" name="email" id="email" value="" class="requiredField email" />
                           </li>
                           
                           <li class="textarea"><label for="commentsText">Message:</label>
                              <textarea name="comments" id="commentsText" rows="20" cols="18" class="requiredField"></textarea>
                           </li>
                           
                           <li class="screenReader"><label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label><input type="text" name="checking" id="checking" class="screenReader" value="" /></li>
                           <li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><input class="submit" type="submit" value="Submit" /></li>
                        </ol>
                        
                     </form>



Here's the code on my contact_send.php page:
<?php
   error_reporting(E_NOTICE);
   function valid_email($str)
   {
      return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
   }
   if($_POST['contactName']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comments'])>1)
   {
      $to = "bees@bees.com"; // Your e-mail here!!!
      $headers =  'From: '.$_POST['email'].''. "\r\n" .
            'Reply-To: '.$_POST['bees@bees.com'].'' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
      $subject = "Contact Form";
      $message = htmlspecialchars($_POST['comments']);
      if(mail($to, $subject, $message, $headers))
      {
         echo 1; //SUCCESS
      }
      else {
         echo 2; //FAILURE - server failure
      }
   }
   else {
      echo 3; //FAILURE - not valid email
   }
?>

Re: Php contact form

Posted: Sun Aug 28, 2011 12:12 pm
by social_experiment
What do you have where you echo the success message, this exact code?

Code: Select all

<?php
 if(mail($to, $subject, $message, $headers))
      {
         echo 1; //SUCCESS
      }
>?

Re: Php contact form

Posted: Sun Aug 28, 2011 12:57 pm
by phphelpme
Its funny that Social I was looking at this code racking my brain to find out what these echo 1, 2, 3 codes meant and referenced from... lol

Can not see it in the code so it looks to me like this is a code grabbed from the internet or an example but they have left out the:

Code: Select all

 header('Location: http://www.example.com/');
Would seem to me that nothing has replaced the // comments for each action required.

I must say I was shock to see how many jscripts their were in the header:

Code: Select all

<script src="js/jquery-1.4.2.min.js"></script> <!-- jquery library -->
    <script src="js/jquery.prettyPhoto.js"></script> <!-- lightbox plugin -->
    <script src="js/jquery.nivo.slider.pack.js"></script> <!-- nivo slider plugin -->
    <script src="js/cufon-yui.js"></script><!-- cufon library -->
    <script src="js/tweets.js"></script><!-- twitter plugin -->
    <script src="js/Vegur_300-Vegur_700.font.js"></script><!-- vegur font -->
   <script src="js/jquery.form.js"></script>   <!-- jQuery form plugin-->
    <script src="js/custom.js"></script><!-- custom jquery stuff-->
Now thats allot... lol

Best wishes

Re: Php contact form

Posted: Sun Aug 28, 2011 1:17 pm
by califdon
What the other 2 responders are saying is that it appears that you are not actually "learning PHP", but instead you are just grabbing some code from somewhere, hoping that it will meet your needs. Now there's nothing wrong with being a newbie (we all were at some point, of course) or using code that someone else has written, except that when you combine those 2 things, it's almost guaranteed not to work. Since you don't yet have the skills necessary to analyze someone else's code and make adjustments, you would indeed be lucky if you found code that does exactly what you want. The code lines they discussed, like

Code: Select all

 if(mail($to, $subject, $message, $headers))
      {
         echo 1; //SUCCESS
      }
      else {
         echo 2; //FAILURE - server failure
      }
don't do anything useful unless you replace them with your own code that actually does whatever you want it to do under those 2 conditions.

The problem with such a lengthy list of Javascript references is that it makes it impossible for anyone to understand your code unless they are familiar with each of those references, which is highly unlikely with so many of them.

Re: Php contact form

Posted: Sun Aug 28, 2011 4:27 pm
by Webbyturtle
I disagree with your thoughts calidon but thanks for your lengthy insights. I indeed found the other posters helpful.

Re: Php contact form

Posted: Sun Aug 28, 2011 9:00 pm
by califdon
You're certainly entitled to disagree with my thoughts, but you might consider evaluating them again when you have 20+ years of programming and teaching experience, as I have.

Re: Php contact form

Posted: Mon Aug 29, 2011 4:27 am
by phphelpme
In no way was anyone saying this person is not learning PHP in any manner what so ever? ? ? ? ? ? ? ? ????????????????????????

I was just stating that this looks like a script that has been taken from the web and like you stated you will find it hard to get one that does exactly what you want it to do. I merely stated that things had been left out of this script which the user was supposed to replace.

Your 20+ years of programming has unfortunately let you down this time Califdon. I have been programming since the age of 8 years old and made my first program up when I was 9. So that would make me also have 20+ years of programming both for myself and professionally. But I in no way arrived at your conclusion.

I think you might need to re-evaluate this thread for yourself.

@Webbyturtle glad that we helped in someway. Do you know what you need to do now?

Best wishes