Results 1 to 5 of 5

Thread: one page website contact form help

  1. #1
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default one page website contact form help

    Hi

    I have built a one page website and got a contact form at the bottom and have added required attribute in the input fields but when I click submit, it submits the form whereas it should not submit the form and say the input fields are required, below is the link to the site

    Below is the code for the form part and the php

    Code:
    <div class="col-lg-6 col-sm-5 wow fadeInUp delay-05s">
                	<div class="form">
        <form method="post" action="index.php#contact">
                    	<input class="input-text" type="text" name="name" id="name" value="Your Name *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                        
                        <input class="input-text" type="text" name="email" id="email" value="Your E-mail *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                    	
                        <input class="input-text" type="text" name="phone" id="phone" value="Your Phone Number *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                        
                        <textarea class="input-text text-area" cols="0" rows="0" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" name="message" id="message" required>Your Message *</textarea>
                        
                        <p class="robotic" id="pot">
          <label>If you're human leave this blank:</label>
          <br />
          <input name="robotest" type="text" id="robotest" class="robotest" />
        </p>
                        
                        <input class="input-btn" type="submit" value="send message" id="btn">
                        </form>
                    </div>	
                </div>
            </div>
            
            <?php
      if($_POST){
        $to = 'myemail';
        $subject = 'New Enquiry From Website';
        $from_name = $_POST['name'];
        $from_email = $_POST['email'];
    	$from_phone = $_POST['phone'];
        $message = $_POST['message'];
        $robotest = $_POST['robotest'];
    	
        if($robotest)
          $error = "You are a gutless robot.";
        else{
    		
          if($from_name && $from_email && $from_phone && $message){
    		  
    		  $body = "A user  $name submitted the contact form:\n".
    		"Name: $from_name\n".
    		"Email: $from_email \n".
    		"Phone: $from_phone \n".
    		"Message: \n ".
    		"$message\n".
    		  
            $header = "From: $from_name <$from_email>";
    		
            if(mail($to, $subject, $body, $header))
              $success = '<div><strong>You are human and your message was sent!</strong></div>';
            else
              $error = '<div><strong>You are human but there was a problem sending the e-mail.</strong></div>';
          }else
            $error = '<div><strong>All fields are required.</strong></div>';
        }
                    if($error)
          echo '<div>'.$error.'</div>';
        elseif($success)
          echo '<div>'.$success.'</div>';
    	
      }
    ?>
            
    </section>
    </div>
    Thank you in advance

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    What doctype are you using and what browser are you testing in?

    Note that the "required" attribute is new to HTML5 (and so requires the HTML5 doctype) but is unsupported in IE9 and under, and Safari)
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Beverleyh View Post
    What doctype are you using and what browser are you testing in?

    Note that the "required" attribute is new to HTML5 (and so requires the HTML5 doctype) but is unsupported in IE9 and under, and Safari)
    Ahh ok, the doctype is <!doctype html> and am testing in firefox

  4. #4
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I got the form checking if fields are empty now using javascript but now the form is not sending the email where as it was before

    the updated code is below

    Code:
    <form name="form" method="post" action="index.php#contact" onsubmit="return checkForm(this);">
                    	<input class="input-text" type="text" name="name" id="name" value="Your Name *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                        
                        <input class="input-text" type="text" name="email" id="email" value="Your E-mail *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                    	
                        <input class="input-text" type="text" name="phone" id="phone" value="Your Phone Number *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" required>
                        
                        <textarea class="input-text text-area" cols="0" rows="0" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" name="message" id="message" required>Your Message *</textarea>
                        
                        <p>
                        <img src="captcha.php" width="120" height="30" border="1" alt="CAPTCHA">
                        </p>
    <p>
    <input type="text" size="6" maxlength="5" name="captcha" value="">
    <br>
    <small>copy the digits from the image into this box</small>
    </p>
                        
                        <input class="input-btn" type="submit" value="send message">
                        </form>
                              
                        <script>
    					
    					function checkForm(form)
      {
    
        if(!form.captcha.value.match(/^\d{5}$/)) {
          alert('Please enter the CAPTCHA digits in the box provided');
          form.captcha.focus();
          return false;
        }
    
        return true;
      }
    					
    					window.onload = function (event) {
        var form = document.getElementsByName('form')[0];
        form.addEventListener('submit', function (event) {
            var inputs = form.getElementsByTagName('input'), input, i;
            for (i = 0; i < inputs.length; i += 1) {
                input = inputs[i];
                if (input.type === 'text' && input.value.trim() === '') {
                    event.preventDefault();
                    alert('You have empty fields remaining.');
                    return false;
                }
            }
        }, false);
    };
    </script>

  5. #5
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Sorry got it working, I had some PHP code left in that was not needed

Similar Threads

  1. Contact form error messages on same page
    By gemzilla in forum PHP
    Replies: 2
    Last Post: 07-25-2013, 08:42 AM
  2. Contact form..how..?
    By JFlores in forum HTML
    Replies: 12
    Last Post: 05-08-2012, 05:49 PM
  3. contact form in a single page website
    By M rosi in forum PHP
    Replies: 5
    Last Post: 07-06-2011, 01:43 PM
  4. Contact form using php
    By biomike in forum PHP
    Replies: 13
    Last Post: 12-26-2008, 03:25 AM
  5. HTML Contact Form Results Page
    By SChaput in forum HTML
    Replies: 2
    Last Post: 10-12-2008, 08:34 PM

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
  •