PHP Form

Discussion in 'PHP' started by sonu21, Feb 20, 2012.

  1. #1
    Hello Guys,I am creating one simple php form all works great but I want solution for one thing.I want cc optional like I have checkbox in the form & if user click it then only it should send CC mail other wise not.Here is the code which I am using.

    <?php ob_start();
    $fromemail="No-Reply <demo@demo.com>"; // change here if you want
    $toemail="demo@demo.com";   // change here if you want
    $sub="Online Feedback";          // change here if you want
    $success_page_name="index.php";
    ////// do not change in following
    if($_SERVER['REQUEST_METHOD']=="POST")
    {
    $fieldnm_1=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_1']));  
    $fieldnm_2=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_2']));  
    
    
    $contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
    <table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Name :</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_1</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email :</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_2</td>
    </tr>
    
    </table>
    ");
    
    ////
    $headers  = "MIME-Version: 1.0
    ";
    $headers .= "Content-type: text/html; charset=iso-8859-1
    ";
                    
    $from=$fromemail;
                    
    $headers .= "From: ".$from." 
    ";
    
    $headers.= "Cc: ".$fieldnm_2."
    ";
                    
    @mail($toemail,$sub,$contentmsg,$headers);
                    
                    
    header("Location:$success_page_name");
    
    }
    ?>
    
    
    PHP:

    Thanks in Advance!
     
    Solved! View solution.
    sonu21, Feb 20, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    I think you need something like :
    
    // at the FORM :
    ?>
    <input type="checkbox" name="send_cc" value="1"  />Send a copy // add a checkbox
    <?
    // at your POST page :
    $send_cc = $_POST['send_cc'];
    
    if ($send_cc == 1) {
       $headers.= "Cc: ".$fieldnm_2;
    } // if the checkbox was checked, add a cc field to the mail headers
    
    PHP:
    good luck...
     
    dujmovicv, Feb 21, 2012 IP
  3. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    Thanks for solution,but I don't know how to write & where to write the following code.Please write & show me.

    $send_cc = $_POST['send_cc'];

    if ($send_cc == 1) {
    $headers.= "Cc: ".$fieldnm_2;
    } //

    Thanks For your Valuable Help!

     
    sonu21, Feb 21, 2012 IP
  4. #4
    You can add the line
    
    $send_cc = $_POST['send_cc'];
    
    PHP:
    Just below your lines :
    
    $fieldnm_1=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_1']));  
    $fieldnm_2=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_2']));  
    
    PHP:
    And you can add the lines :
    
    if ($send_cc == 1) {
       $headers.= "Cc: ".$fieldnm_2;
    }
    
    PHP:
    Below your line :
    
    $headers .= "From: ".$from;
    
    PHP:
     
    dujmovicv, Feb 21, 2012 IP
  5. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    Thank You! So Much.It works Great as I want.Can you tell me 1 more thing is it possible to send only selected details to user once form submitted.

    Thanks For HELP!
     
    sonu21, Feb 21, 2012 IP
  6. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #6
    I'm afraid I don't understand your question... Can you be please more specific? Which details do you mean? You can send whatever you want via a form (with method POST) and then propagate at the post page...
     
    dujmovicv, Feb 21, 2012 IP
  7. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    Sorry,I mean I have a form which have 3 fields 1.Name 2.Email 3.Contact so when user submit it then he will get details with CC function but he should get only 2 details name & contact through CC.

    Hope you Understand.

    Thanks!
     
    sonu21, Feb 21, 2012 IP
  8. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #8
    If you send an email (To) and a copy to someone else (Cc) you can't filter what to send to one and s'thing else to another. Both will see the SAME content. If that's what you asked... :)
     
    dujmovicv, Feb 22, 2012 IP
  9. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #9
    Ok,So is there any way to send limited details to the user through contact form?

    Thanks!
     
    sonu21, Feb 22, 2012 IP
  10. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #10
    Yes, you can send an email with those limited data. You'll have to create separate message body and call the mail() function twice. S'thing like:

    
    $contentmsg_1 = stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
    <table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Name :</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_1</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email :</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_2</td>
    </tr>
    
    </table>
    ");
    $contentmsg_2 =stripslashes("WHAT DO YOU WANT TO PASS TO THE VISITOR");
    
    ////
    $headers  = "MIME-Version: 1.0";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
                    
    $from=$fromemail;
                    
    $headers .= "From: ".$from;
    
    if ($send_cc == 1) {
       @mail($toemail,$sub,$contentmsg_2,$headers);
    }
                   
    @mail($toemail,$sub,$contentmsg,$headers);                                
    header("Location:$success_page_name");
    
    
    PHP:
    Hope you'll get the idea... :)
     
    dujmovicv, Feb 22, 2012 IP
  11. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #11
    Sorry,but can you tell me little more how to do this.

    Thanks!
     
    sonu21, Feb 22, 2012 IP
  12. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #12
    Without any additional code:

    You send an email to the user with name and contact in the $message variable. Then send an email to you, using $message .= $email, so you get all 3.
     
    Rukbat, Feb 25, 2012 IP