SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: PHP Form

  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Form

    I have a PHP form that works decently and it captures all the information I want. What I would like it to do is to email the person on the form using the email address that was captured when he/she inputted it. Is this possible?

  2. #2
    SitePoint Addict evertcollab's Avatar
    Join Date
    Apr 2006
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out the mail function

    http://www.php.net/mail

  3. #3
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you could add their email address variable to your $TO variable or you could create a new one $CC and use the same principle as sending a regular email, e.g.

    $CC = $FROM
    $TO = $TO .";". $CC;



    Quote Originally Posted by jiggawhat
    I have a PHP form that works decently and it captures all the information I want. What I would like it to do is to email the person on the form using the email address that was captured when he/she inputted it. Is this possible?

  4. #4
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    if ($_SERVER['REQUEST_METHOD']=="POST"){
    // In testing, if you get an Bad referer error
    // comment out or remove the next three lines
    if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
    !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
    die("Bad referer");
    $msg="Values submitted by the user:\n";
    foreach($_POST as $key => $val){
    if (is_array($val)){
    $msg.="Item: $key\n";
    foreach($val as $v){
    $v = stripslashes($v);
    $msg.=" $v\n";
    }
    } else {
    $val = stripslashes($val);
    $msg.="$key: $val\n";
    }
    }
    $recipient="myemail@domain.com";
    $subject="From Website";
    error_reporting(0);
    if (mail($recipient, $subject, $msg)){
    echo "<h1>Thanks</h1><p>Message sent. Please click the back button if you wish to continue browsing the site.</p>\n";
    echo nl2br($input);
    } else
    echo "An error occurred and the message could not be sent.";
    } else
    echo "Bad request method";
    ?>


    So where would I put that code. Sorry for the ignorance but my php coding skills are next to nothing.

  5. #5
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that's a good question since your code doesn't exactly spell out what the keys are in your array area.

    As it is your code, you should be able to extract which $key => $val you should extract the user's email address from. Then just create another part at the bottom in the if (mail area with the message that you want to send and add another (mail code portion to send it to the user.

    Peter


    Quote Originally Posted by jiggawhat
    PHP Code:
    <?php
        
    if ($_SERVER['REQUEST_METHOD']=="POST"){
           
    // In testing, if you get an Bad referer error
           // comment out or remove the next three lines
           
    if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>||
              !
    strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
              die(
    "Bad referer");

    $msg="Values submitted by the user:\n";
           foreach(
    $_POST as $key => $val){
              if (
    is_array($val)){
                 
    $msg.="Item: $key\n";
                 foreach(
    $val as $v){
                    
    $v stripslashes($v);
                    
    $msg.="   $v\n";
                 }
              } else {
                 
    $val stripslashes($val);
                 
    $msg.="$key$val\n";
              }
           }

     
    $recipient="myemail@domain.com";
           
    $subject="From Website";
           
    error_reporting(0);
     
           if (
    mail($recipient$subject$msg)){
              echo 
    "<h1>ThanksMessage sent. Please click the back button if you wish to continue browsing the site.

    \n"
    ;
              echo 
    nl2br($input);
           } else
              echo 
    "An error occurred and the message could not be sent.";
        } else
           echo 
    "Bad request method";
     
    ?>
    So where would I put that code. Sorry for the ignorance but my php coding skills are next to nothing.

  6. #6
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got this code from somewhere else and I just modified it. It's supposed to capture each form field and put it in the email and submit it to me.

  7. #7
    SitePoint Enthusiast PHPmySQLer's Avatar
    Join Date
    Jan 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form on my contact page has four input values:

    name
    email
    subject
    message

    The following is the confirmation page:

    PHP Code:
    Your message titled &quot;<?php echo "$subject"?>&quot; has been sent.
    We will contact you as soon as possible. Thank you.</p>
                 <p align="center" class="style6"><span class="style6"><a href="index.php">Home</a></span></p>
                 <?
     
    $to 
    "email@userdomain.com";
    $msg "$name\n\n";
    $msg .= "$message\n\n";
     
    mail($to$subject$msg"From: $email\n\nReply-To: $email\n");
     
    ?>
    works like a charm however bulky with html
    [\/] [/\\//\]

  8. #8
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So based on what you have written, you want to add this code after the
    echo "Bad request method";
    code that you already have:

    $cc = $_post["email"]; this copies the value of the email address

    mail($cc, $subject, $msg,
    "From: youremail@yourdomain.tld\n\n
    Reply-To:
    youremail@yourdomain.tld\n");

    this will send a copy of what was submitted to the user.

    Peter



    Quote Originally Posted by PHPmySQLer
    The form on my contact page has four input values:

    name
    email
    subject
    message

    The following is the confirmation page:

    PHP Code:
     Your message titled &quot;<?php echo "$subject"?>&quot; has been sent.
     We will contact you as soon as possible. Thank you.</p>
                 <p align="center" class="style6"><span class="style6"><a href="index.php">Home</a></span></p>
                  <?
      
     $to 
    "email@userdomain.com";
     
    $msg "$name\n\n";
     
    $msg .= "$message\n\n";
      
     
    mail($to$subject$msg"From: $email\n\nReply-To: $email\n");
      
     
    ?>
    works like a charm however bulky with html


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
  •