1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'PHP & Perl' started by kapub, Jan 7, 2013.

  1. kapub

    kapub Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 20, 2011
    Messages:
    237
    Likes Received:
    73
    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. Ayeyermaw

    Ayeyermaw Newbie

    Joined:
    Jan 7, 2013
    Messages:
    2
    Likes Received:
    0
    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. Gintoki

    Gintoki Regular Member

    Joined:
    Mar 24, 2012
    Messages:
    201
    Likes Received:
    152
    Occupation:
    Freelancer
    Location:
    Hungary
    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: Jan 8, 2013
  4. alias_unknown

    alias_unknown Newbie

    Joined:
    Dec 30, 2012
    Messages:
    41
    Likes Received:
    4
    Gintoki got it