Results 1 to 10 of 10

Thread: Contact form issues using html and php mixed.

  1. #1
    Join Date
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Contact form issues using html and php mixed.

    I have mixed and matched code to great a contact form to my own specification and I have managed to sort out most errors myself but just can't seem to see what I have done wrong.

    The echo $result variable states that its undefined, so that wont work when the form is submitted to give the correct message,

    then when i test to see if my error messages work, i just end up with a page stating: Undefined index: email in on line 30 and Undefined variable: human in on line 47

    Can anyone offer an extra pair of eyes and assistance please. Thank you.

    Form coding:

    PHP Code:
       <form id="contacts-form" method="post" action="php/mailer test.php">
          
          <fieldset>
          
              <div class="field">
                <label>Your Name:</label>
                <input name="name" type="text" placeholder="First & Last Name">
                <?php if (isset($errName)) {
        echo 
    '<p class="text-danger">'.$errName.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Your E-mail:</label>
                <input name="mail" type="email" placeholder="example@domain.com">
                <?php if (isset($errEmail)) {
        echo 
    '<p class="text-danger">'.$errEmail.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Phone Number:</label>
                <input name="number" type="text" placeholder="Mobile or Home number">
                <?php if (isset($errNumber)) {
        echo 
    '<p class="text-danger">'.$errNumber.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Subject:</label>
                <input name="subject" type="text" placeholder="Wedding, Birthday etc">
                <?php if (isset($errSubject)) {
        echo 
    '<p class="text-danger">'.$errSubject.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Your Message:</label>
                <textarea  name="message"  cols="1" rows="1" placeholder="Write your message here!"></textarea>
                <?php if (isset($errMessage)) {
        echo 
    '<p class="text-danger">'.$Message.'</p>';
    };
    ?>
              </div>
                  
        <div class="field">
            <label for="human" >2 + 3 = ?</label>
            <input type="text" id="human" name="human" placeholder="Your Answer">
            <?php if (isset($errHuman)) {
        echo 
    '<p class="text-danger">'.$errHuman.'</p>';
    }
    ?>
            </div>    
              
              <div class="button">
              <input type="Submit" name="Submit" value="Send your message!"></input></div>
              
                  <div class="field">
                    <?php echo $result?>    
            </div>
        
              
            </fieldset>
            
          </form>
    PHP running script:

    PHP Code:
    <?php 
      
    if(isset($_POST['Submit'])) {   
    $youremail 'example@domain.com';
    $fromsubject 'Website enquire';
    $name $_POST['name'];
    $number $_POST['number'];
    $mail $_POST['mail'];
    $subject $_POST['subject']; 
    $message $_POST['message']; 
        
    $to $youremail
        
    $mailsubject 'Masage recived from'.$fromsubject.' Contact Page';
        
    $body $fromsubject.'
        
        The person that contacted you is  '
    .$name.
         E-mail: '
    .$mail.'
         Phone number: '
    .$number.
         Subject: '
    .$subject.'
        
         Message: 
         '
    .$message.'
        
        |---------END MESSAGE----------|'

        
                
            if (!
    $_POST['name']) {
                
    $errName 'Please enter your name';
            }
            
            if (!
    $_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                
    $errEmail 'Please enter a valid email address';
            }
            
            if (!
    $_POST['number']) {
                
    $errNumber 'Please enter your mobile or home number ';
            }
            
            if (!
    $_POST['subject']) {
                
    $errSubject 'Please enter your subject';
            }
            
                
            if (!
    $_POST['message']) {
                
    $errMessage 'Please enter your message';
            }
            
            if (
    $human !== 5) {
                
    $errHuman 'Your anti-spam is incorrect';
            }
        
    if (!
    $errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman) {    
        if (
    mail($to$subject$body));
        
            
    $result='<div class="alert alert-success">Thank You! I will be in touch shortly</div>';
        } else {
            
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
        
    ?>

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

    Default

    Very quickly as I'm on mobile...

    Try defining the $result variable at the top of the page that contains the form. $result = ''; should hopefully do it.

    For the email error on line 30, I think it's because you're mixing up 'mail' and 'email' - your name attribute's value in the form is 'mail', and it's $_POSTed as 'mail' too at the top of the processing script, but further down it changes to 'email'.

    And the form data from the 'human' field isn't even $_POSTed to the processing script, yet an undefined variable for $human is being used in the !==5 if statement later on.
    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
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thank you for your response.

    Its amazing how and extra pair of eyes help. I have changed the mail around (not sure why i did that) and now that variable is not showing up anymore. I am yet to test it server side to see if it sends it to an email, but as the errors are not showing, will for now, persume it works.

    As for the $result, I have added what you said, but am still receiving the error for it as an Undefined variable.

    Here is my current update code :

    PHP Code:
         <form id="contacts-form" method="post" action="php/mailer test.php">
          
          <fieldset>
          
              <div class="field">
                <label>Your Name:</label>
                <input name="name" type="text" placeholder="First & Last Name">
                <?php if (isset($errName)) {
        echo 
    '<p class="text-danger">'.$errName.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Your E-mail:</label>
                <input name="email" type="email" placeholder="example@domain.com">
                <?php if (isset($errEmail)) {
        echo 
    '<p class="text-danger">'.$errEmail.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Phone Number:</label>
                <input name="number" type="text" placeholder="Mobile or Home number">
                <?php if (isset($errNumber)) {
        echo 
    '<p class="text-danger">'.$errNumber.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Subject:</label>
                <input name="subject" type="text" placeholder="Wedding, Birthday etc">
                <?php if (isset($errSubject)) {
        echo 
    '<p class="text-danger">'.$errSubject.'</p>';
    }
    ?>
              </div>
              
              <div class="field">
                <label>Your Message:</label>
                <textarea  name="message"  cols="1" rows="1" placeholder="Write your message here!"></textarea>
                <?php if (isset($errMessage)) {
        echo 
    '<p class="text-danger">'.$Message.'</p>';
    };
    ?>
              </div>
                  
        <div class="field">
            <label for="human" >2 + 3 = ?</label>
            <input type="text" id="human" name="human" placeholder="Your Answer">
            <?php if (isset($errHuman)) {
        echo 
    '<p class="text-danger">'.$errHuman.'</p>';
    }
    ?>
            </div>    
              
              <div class="button">
              <input type="Submit" name="Submit" value="Send your message!"></input></div>
              
                  <div class="field">
                    <?php echo $result?>    
            </div>
        
              
            </fieldset>
            
          </form>

    PHP Code:
    <?php 
      
    $result 
    '';
      
    if(isset(
    $_POST['Submit'])) {   
    $youremail '';
    $fromsubject 'Website enquire';
    $name $_POST['name'];
    $number $_POST['number'];
    $email $_POST['email'];
    $subject $_POST['subject']; 
    $message $_POST['message']; 
    $human intval($_POST['human']);
        
    $to $youremail
        
    $mailsubject 'Masage recived from'.$fromsubject.' Contact Page';
        
    $body $fromsubject.'
        
        The person that contacted you is  '
    .$name.
         E-mail: '
    .$email.'
         Phone number: '
    .$number.
         Subject: '
    .$subject.'
        
         Message: 
         '
    .$message.'
        
        |---------END MESSAGE----------|'

        
                
            if (!
    $_POST['name']) {
                
    $errName 'Please enter your name';
            }
            
            if (!
    $_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                
    $errEmail 'Please enter a valid email address';
            }
            
            if (!
    $_POST['number']) {
                
    $errNumber 'Please enter your mobile or home number ';
            }
            
            if (!
    $_POST['subject']) {
                
    $errSubject 'Please enter your subject';
            }
            
                
            if (!
    $_POST['message']) {
                
    $errMessage 'Please enter your message';
            }
            
            if (
    $human !== 5) {
                
    $errHuman 'Your anti-spam is incorrect';
            }
        
    if (!
    $errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman) {    
        if (
    mail($to$subject$body));
        
            
    $result='<div class="alert alert-success">Thank You! I will be in touch shortly</div>';
        } else {
            
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
        
    ?>

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

    Default

    As for the $result, I have added what you said, but am still receiving the error for it as an Undefined variable.
    You misunderstood....
    Try defining the $result variable at the top of the page that contains the form
    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

  5. The Following User Says Thank You to Beverleyh For This Useful Post:

    kwood30 (11-05-2016)

  6. #5
    Join Date
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Ah sorry, yes I miss read what you said.

    Thank you I put it in the <head> tag on my form page an the error has now gone.

    The trouble I am now facing is that when i execute the submit button it just goes to a blank page instead of running the script as designed, ie if i don't fill out anything instead of it stating under each section, it goes straigh to a blank page.

    What am I missing? Do i need to link the php in my <head> tag aswell as it being linked in the action= of my form? Sorry to be a pain, I always seem to get issues when I try and right my own script to how i want it.

  7. #6
    Join Date
    Jan 2015
    Posts
    58
    Thanks
    0
    Thanked 13 Times in 13 Posts

    Default

    For these two pieces of code to work, they would have to be on the same page. Otherwise, none of the variables for the error and result messages, that are being set in the form processing code, will exist where they are used in the form code.

    Your code should have the php form processing code at the top, before you start to output anything for the html document. Defining the $result variable would come before the php form processing code so that when it actually runs and assigns a message to $result, it will have that value when the html document is output.

  8. #7
    Join Date
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thank you, I have put all that code in the <head> of my contact page and it is now working, but with a new error and I don't know why. I am getting Undefined variable error for this line of code:

    PHP Code:
    if (!$errName && !$errEmail && !$errNumber && !$errSubject && !$errMessage && !$errHuman
    The complete section of code as you will see above in my previous message. To behonest I cannot see that it is wrong, so don't understand. Surley this is stating that if there is no errors the send the form??

  9. #8
    Join Date
    Jan 2015
    Posts
    58
    Thanks
    0
    Thanked 13 Times in 13 Posts

    Default

    The place where you found this style of coding would have had a line like the following, to initialize those variables, before the start of the form processing code -
    PHP Code:
    $errName $errEmail $errNumber $errSubject $errMessage $errHuman ''

  10. The Following User Says Thank You to DyDr For This Useful Post:

    kwood30 (11-05-2016)

  11. #9
    Join Date
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    To be honest, I have looked at many different types of codes and tried to mix and match to my own specification, and I am still really new to all this, as I am also trying to teach myself Javascript too.

    So have i got this right, what yours saying is that the line of code you have shown needs to be placed at the top of my <php> script before if(isset($_POST['Submit'])) or in a seperate tag on its own like the $result variable?

  12. #10
    Join Date
    Feb 2016
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    I have worked it out so all sorted. Thank you all for your help and input

Similar Threads

  1. Replies: 2
    Last Post: 06-08-2012, 03:07 PM
  2. Replies: 4
    Last Post: 11-25-2009, 08:00 AM
  3. HTML Contact Form Results Page
    By SChaput in forum HTML
    Replies: 2
    Last Post: 10-12-2008, 08:34 PM
  4. HTML contact form
    By awg in forum HTML
    Replies: 7
    Last Post: 08-27-2007, 05:47 PM
  5. Basic html contact form
    By wentwooddownhill in forum HTML
    Replies: 2
    Last Post: 08-22-2007, 03:31 PM

Tags for this Thread

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
  •