Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Using PHP To Email A Form

    I am wondering if I can use PHP to email the following form? And does anyone have any examples, or can anyone work with this example?
    Attached Files Attached Files

  • #2
    New Coder
    Join Date
    Jun 2006
    Location
    eth0
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <form method="POST" action="mailer2.php">
    Name:
    <input type="text" name="name" size="19"><br>
    <br>
    E-Mail:
    <input type="text" name="email" size="19"><br>
    <br>
     
    <input type="checkbox" name="check[]" value="blue_color"> Blue<br>
    <input type="checkbox" name="check[]" value="green_color"> Green<br>
    <input type="checkbox" name="check[]" value="orange_color"> Orange<br>
    <br>
    Message:<br>
    <textarea rows="9" name="message" cols="30"></textarea><br>
    <br>
    <input type="submit" value="Submit" name="submit">
    </form>
    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
     

    $to "mail@mail.com";
    $subject "Form";
    $name_field $_POST['name'];
    $email_field $_POST['email'];
    $message $_POST['message'];
     
    foreach(
    $_POST['check'] as $value) {

    $check_msg .= "Checked: $value\n";

    }
     
    $body "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";
     
    echo 
    "Data has been submitted to $to!";
    mail($to$subject$body);
     

    } else {

    echo 
    "blarg!";

    }
    ?>
    <? print("Hello World") ?>

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Better off using an if statement to check if mail was sent
    PHP Code:

    if (mail($to$subject$message)) //if the mails sent
    header(Location "http://www.server.com/thanks.html");
    }
    //page to redirect to if your email was sent
    else
    { echo 
    "Sorry. Error sending message!";} //show that email could not be sent 

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    Better off using an if statement to check if mail was sent
    PHP Code:

    if (mail($to$subject$message)) //if the mails sent
    header(Location "http://www.server.com/thanks.html");
    }
    //page to redirect to if your email was sent
    else
    { echo 
    "Sorry. Error sending message!";} //show that email could not be sent 
    So how do I put it all together? Sorry I'm a newbie and still get confused

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Code:
    <form method="POST" action="mailer2.php">
    Name:
    <input type="text" name="name" size="19"><br>
    <br>
    E-Mail:
    <input type="text" name="email" size="19"><br>
    <br>
     
    <input type="checkbox" name="check[]" value="blue_color"> Blue<br>
    <input type="checkbox" name="check[]" value="green_color"> Green<br>
    <input type="checkbox" name="check[]" value="orange_color"> Orange<br>
    <br>
    Message:<br>
    <textarea rows="9" name="message" cols="30"></textarea><br>
    <br>
    <input type="submit" value="Submit" name="submit">
    </form>
    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
     

    $to "mail@mail.com";
    $subject "Form";
    $name_field $_POST['name'];
    $email_field $_POST['email'];
    $message $_POST['message'];
     
    foreach(
    $_POST['check'] as $value) {

    $check_msg .= "Checked: $value\n";

    }
     
    $body "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg"
    mail($to$subject$body); 

    if (
    mail($to$subject$body)) //if the mails sent
    header(Location "http://www.server.com/thanks.html");
    }
    //page to redirect to if your email was sent
    else
    { echo 
    "Sorry. Error sending message!";} //show that email could not be sent  

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •