[Help Pls] Adding an extra field in PHP Contact form

 

Results 1 to 4 of 4
Hi all, I'm hoping to add an extra field (Phone) to an existing php contact ...
  1. #1
    kapub's Avatar
    kapub is offline Jr. VIP
    Join Date
    Feb 2011
    Posts
    241
    Thanks
    7
    Thanked 67 Times in 40 Posts

    Default [Help Pls] Adding an extra field in PHP Contact form

    Hi all,

    I'm hoping to add an extra field (Phone) to an existing php contact form used in OptimizePress. I'm having some issues trying to get it working mainly because I'm not a coder. Was wondering if someone can quickly give me a hand just to add this one field?

    Thanks in advance!

    Code:
    <?php $errors = '';
    $myemail = 'email@youremail.com';//<-----Put Your email address here.
    if(empty($_POST['name'])  || 
       empty($_POST['email']))
    {
        $errors .= "\n Error: all fields are required. Press the back button in your browser to re-enter your information";
    }
    
    
    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    
    
    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email_address))
    {
        $errors .= "\n Error: Invalid email address. Press the back button in your browser to re-enter your information";
    }
    
    
    if( empty($errors))
    {
        $to = $myemail; 
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n Email: $email_address"; 
        
        $headers = "From: $myemail\n"; 
        $headers .= "Reply-To: $email_address";
        
        mail($to,$email_subject,$email_body,$headers);
        //redirect to the 'thank you' page
        header('Location: http://www.thankyoupage.com');//<-----Enter your thank you page URL here
    } 
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html>
    <head>
        <title>Form Submission Handler</title>
    </head>
    
    
    <body>
    <!-- This page is displayed only if there is some error -->
    <?php
    echo nl2br($errors);
    ?>
    
    
    
    
    </body>
    </html>




  2. #2
    Ayeyermaw is offline Newbies
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Help Pls] Adding an extra field in PHP Contact form

    Arrrgh sorry.
    I had typed out the answer but as I'm new to the forum it won't let me - even when i omit any URL,email addresses and images.

    It's a simple one so I'm sure someone with more permissions can help you out

  3. #3
    Gintoki's Avatar
    Gintoki is offline Junior Member
    Join Date
    Mar 2012
    Location
    Hungary
    Posts
    177
    Thanks
    71
    Thanked 139 Times in 56 Posts

    Default Re: [Help Pls] Adding an extra field in PHP Contact form

    Sorry, I made a reply but somehow the formatting got f*ked up... Will try again later if its still not solved.
    EDIT:
    Code:
    <?php $errors = '';
    $myemail = 'email@youremail.com';//<-----Put Your email address here.
    if(empty($_POST['name'])  || 
       empty($_POST['email']) ||
       empty($_POST['Phone'])   //<---throws error if the user hasn't filled out the phone field. delete line if its optional
       )
    {
        $errors .= "\n Error: all fields are required. Press the back button in your browser to re-enter your information";
    }
    
    
    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    $phone = $_POST['Phone']; //<---declares the $phone variable with the content of the form field called 'Phone'
    
    
    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email_address))
    {
        $errors .= "\n Error: Invalid email address. Press the back button in your browser to re-enter your information";
    }
    
    
    if( empty($errors))
    {
        $to = $myemail; 
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n Email: $email_address \n Phone: $phone"; //adds the user's phone number to the email
        
        $headers = "From: $myemail\n"; 
        $headers .= "Reply-To: $email_address";
        
        mail($to,$email_subject,$email_body,$headers);
        //redirect to the 'thank you' page
        header('Location: http://www.thankyoupage.com');//<-----Enter your thank you page URL here
    } 
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html>
    <head>
        <title>Form Submission Handler</title>
    </head>
    
    
    <body>
    <!-- This page is displayed only if there is some error -->
    <?php
    echo nl2br($errors);
    ?>
    
    
    </body>
    </html>
    Last edited by Gintoki; 01-07-2013 at 11:27 PM.

  4. #4
    alias_unknown is offline Newbies
    Join Date
    Dec 2012
    Posts
    41
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: [Help Pls] Adding an extra field in PHP Contact form

    Gintoki got it


Similar Threads

  1. [Help Pls?] Multipage PHP Form
    By Earthquake in forum BlackHat Lounge
    Replies: 5
    Last Post: 12-04-2011, 03:52 PM
  2. PHP Contact Form
    By s-c-0-r-p-i-a-n in forum General Programming Chat
    Replies: 4
    Last Post: 08-30-2011, 07:13 AM
  3. Need PHP Contact Form Script [Urgent]
    By Supernoob in forum Hire a Freelancer
    Replies: 7
    Last Post: 03-21-2011, 01:42 AM
  4. Need a quick php contact form fix + spam protection
    By dkanalie in forum Hire a Freelancer
    Replies: 1
    Last Post: 02-28-2011, 03:01 AM
  5. Replies: 1
    Last Post: 08-07-2010, 08:06 AM

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
  •  




BlackHatWorld on Twitter BlackHatWorld on FaceBook


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109