1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form to Email - problems with php code

Discussion in 'PHP' started by madmn, Oct 18, 2009.

  1. #1
    I have a form to email tutorial and corresponding script files. The tutorial doesn't explain how to add more fields to the form.

    Originally there was only name , email and message. On the form I managed to add more feilds. I added "company" and "phone" between Name and Email.

    Then I added a field that is labeled "found" with the caption "Please tell us how you found us:

    Below is the php file that came with it. Do I have to change anything in the php file because I added more fields?



    <?php
    if(!isset($_POST['submit']))
    {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    //Validate first
    if(empty($name)||empty($visitor_email))
    {
    echo "Name and email are mandatory!";
    exit;
    }

    if(IsInjected($visitor_email))
    {
    echo "Bad email value!";
    exit;
    }

    $email_from = 'tom@amazing-designs.com';//<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

    $to = "tom@amazing-designs.com";//<== update the email address
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');


    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    ?>

     
    madmn, Oct 18, 2009 IP
  2. madmn

    madmn Well-Known Member

    Messages:
    431
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Here is the form part:



    <form method="post" name="myemailform" action="form-to-email.php">
    <p>
    <label for='name'>Enter Name: </label><br>
    <input type="text" name="name">
    </p>


    <p>
    <label for='company'>Company: </label><br>
    <input type="text" name="company">
    </p>


    <p>
    <label for='phone'>Phone: </label><br>
    <input type="text" name="phone">
    </p>


    <p>
    <label for='found'>Please tell us how you found us:</label><br>
    <input type="text" name="found">
    </p>


    <p>
    <label for='email'>Enter Email Address:</label><br>
    <input type="text" name="email">
    </p>
    <p>
    <label for='message'>Enter Message:</label> <br>
    <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
    </form>
    <script language="JavaScript">
    // Code for validating the form
    // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
    // for details
    var frmvalidator = new Validator("myemailform");
    frmvalidator.addValidation("name","req","Please provide your name");
    frmvalidator.addValidation("email","req","Please provide your email");
    frmvalidator.addValidation("email","email","Please enter a valid email address");
    </script>
     
    madmn, Oct 18, 2009 IP
  3. Gungz

    Gungz Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    if(!isset($_POST['submit']))
    {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $found = $_POST['found'];

    //Validate first
    if(empty($name)||empty($visitor_email))
    {
    echo "Name and email are mandatory!";
    exit;
    }

    if(IsInjected($visitor_email))
    {
    echo "Bad email value!";
    exit;
    }

    $email_from = 'tom@amazing-designs.com';//<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message\n".
    "They found us from : $found";

    $to = "tom@amazing-designs.com";//<== update the email address
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');


    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    ?>


    I modify your PHP code. My modification is in black.
     
    Gungz, Oct 18, 2009 IP