SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with a bug in my contact form

    Hi,

    I updated a small form to add a little spam security. I have tried to implement a simple math question but I am getting loads of php errors, I've made mistakes somewhere but cant see where.

    I now get bugs when I open the page in localhost. Any help would be much appreciated.


    Code:
    		<?php
                $name = $_POST['name'];
                $email = $_POST['email'];
                $company = $_POST['company'];
    	   $phone = $_POST['phone'];
    	   $message = $_POST['message'];
    			
                $to = 'me@mydomain.com'; 
                $subject = 'User Mail';
                $human = $_POST['human'];
                        
                $body = "From: $name\n 
    					 E-Mail: $email\n 
    					 Company: $company\n 
    					 Phone: $phone\n 
    					 Message:\n $message";
                
                $headers = "From: Website Visitor <someone_from@yourwebsite.com>";
            
                            
            if ($_POST['submit']) {
                if ($name != '' && $email != '' && $company != '' && $phone != '' && $message != '') {
                    if ($human == '10') {				 
                        if (mail ($to, $subject, $body, $headers)) { 
                        echo '<h2>Your message has been sent! :) We will get back to you very soon.</h2>';
                    } else { 
                        echo '<h2>Something went wrong. Please try again.</h2>'; 
                    } 
                } else if ($_POST['submit'] && $human != '10') {
                    echo '<h2>You answered the anti-spam question incorrectly. Please try again.</h2>';
                }
                } else {
                    echo '<h2>You need to fill in all the fields.  </h2>';
                }
            }
            
            ?>

    Code:
       
    
       <form method="post" action="enquiry_form.php">
                      
                  <div class="form_left">
                      <p>Name:<br>
                      <input type="text" name="name" size="24" autofocus="true" placeholder="Type Here" required="true" /></p>
                  </div>
             
             
                   <div class="form_right">
                      <p>Email:<br>
                      <input type="email" name="email" size="24" placeholder="Type Here" required="true" /></p>
                  </div>
                  
              
                 
                  <div class="form_left">
                      <p>Company Name:<br />
                      <input type="text" name="company" size="24" placeholder="Type Here" required="true" /></p>
                 </div>
             
             
                   <div class="form_right">
                       <p>Phone Number:<br />
                      <input type="text" name="phone" size="24" placeholder="Type Here" required="true" /></p>
                  </div>
                
            
                 <div class="form_left">
                        <p>Your Message:<br />
                         <textarea rows="6" name="message" cols="55" placeholder="Type Here" required="true"></textarea></p>
                
                 </div>
                 
                 
                  <div class="form_left">
                        <p>What is 7 + 3 (Anti Spam):<br />
                         <input type="text" name="human" size="24" placeholder="Type Here" required="true" /></p>
                 </div>
                 
                 
                
                  <div class="send_callback">
                  <p class="submit"><input type="submit" value="Send" name="submit" /></p>
                 </div>
    
    
    </form>

  2. #2
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was testing this in local host! Hmm.

    I put it live and it seems to work no problem at all.

    Do these forms not perform in localhost?

  3. #3
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Mails are sent via mail server. If you local host did not set up one most likely it will not work.

    I made some changes to your form where the form does not require re-input all the entries:

    PHP Code:
    <?php
       $name 
    $_POST['name'];
       
    $email $_POST['email'];
       
    $company $_POST['company'];
       
    $phone $_POST['phone'];
       
    $message $_POST['message'];
                
       
    $to 'me@mydomain.com'
       
    $subject 'User Mail';
       
    $human $_POST['human'];
                        
       
    $body "From: $name\n".
             
    "E-Mail: $email\n".
             
    "Company: $company\n".
             
    "Phone: $phone\n".
             
    "Message:\n $message";
                
       
    $headers "From: Website Visitor <someone_from@yourwebsite.com>";
            
                            
       if (
    $_POST['submit'])
       {
           if (
    $name != '' && $email != '' && $company != '' && $phone != '' && $message != '') {
               if (
    $human == '10') {                 
                   if (
    mail ($to$subject$body$headers)) { 
                       echo 
    '<h2>Your message has been sent! :) We will get back to you very soon.</h2>';
                       exit;
                   } else { 
                       echo 
    '<h2>Something went wrong. Please try again.</h2>'
                   } 
               } else if (
    $_POST['submit'] && $human != '10') {
                   echo 
    '<h2>You answered the anti-spam question incorrectly. Please try again.</h2>';
               }
           } else {
               echo 
    '<h2>You need to fill in all the fields.  </h2>';
           }
       }
            

    echo 
    '
    <form method="post" action="enquiry_form.php">
       <div class="form_left">
          <p>Name:<br>
         <input type="text" name="name" size="24" autofocus="true" placeholder="Type Here" required="true" value="'
    .$name.'"/></p>
       </div>
       <div class="form_right">
          <p>Email:<br>
         <input type="email" name="email" size="24" placeholder="Type Here" required="true" value="'
    .$email.'" /></p>
       </div>
       <div class="form_left">
          <p>Company Name:<br />
         <input type="text" name="company" size="24" placeholder="Type Here" required="true" value="'
    .$company.'" /></p>
       </div>
       <div class="form_right">
          <p>Phone Number:<br />
         <input type="text" name="phone" size="24" placeholder="Type Here" required="true" value="'
    .$message.'" /></p>
       </div>
       <div class="form_left">
          <p>Your Message:<br />
         <textarea rows="6" name="message" cols="55" placeholder="Type Here" required="true">'
    .$message.'</textarea></p>
       </div>
       <div class="form_left">
          <p>What is 7 + 3 (Anti Spam):<br />
         <input type="text" name="human" size="24" placeholder="Type Here" required="true" /></p>
       </div>
       <div class="send_callback">
          <p class="submit"><input type="submit" value="Send" name="submit" /></p>
       </div>
    </form>'
    ;
    ?>

  4. #4
    SitePoint Addict Zapppa's Avatar
    Join Date
    Aug 2009
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, can you explain what you did please ? Am I right in thinking that the form will remember the details and keep them filled in should the user make a mistake?

    I am still very much a php student.

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Basically I changed the form to populate with the contents that have already been entered so if you enter an invalid anti-spam code you would not have to re-input everything; otherwise the form would show the placeholder texts again.

    We all started as student. If you keep at it you will be a teacher soon enough.


Bookmarks

Posting Permissions

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