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 11 of 11
  1. #1
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Contact form with ability to change the reciever

    Hey!

    I've been looking around the net for this thing that I'd need for my website. You see, what I'd want to achieve is a form, where the person who is filling it out could choose between one of several different recievers. Like, he/she can send his/her message to person A, person B or person C by choosing one from a dropdown menu. Then the person would add his/her e-mail address, a topic and the message. Nothing fancier than that. Could someone be so kind and help me out on this? Thanks!


    ~Jan

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm assuming you're able to send mail to some hard-coded email address at present (based on scanactive.com).

    The only difference would be:
    1. You'd have an addition form element, a SELECT, with the various email addresses / receivers in it.
    2. Alter the mail() call in send.php and replace the hard-coded email address with the value submitted from the SELECT.

    You can hard-code the SELECT with the receivers, or you can store the receivers in a DB and populate the SELECT dynamically using PHP. Storing the receivers in a DB allows you to hide the receivers' email addresses, as instead of putting the email address as a value, you can put the ID number. Then before the call to mail() in send.php, grab the email address out of the DB using the ID number...

    Let me know if you have any trouble with that.
    Sadiq.

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Do you mean something like this:

    PHP Code:
    <?php

    function populate_recipient_select() {
       
    $to_send "";
       
    $recipients_array = array(
           
    "Recipient 1" => "recip1@example.com",
           
    "Recipient 2" => "recip2@example.com",
           
    "Recipient 3" => "recip3@example.com",
           
    "Recipient 4" => "recip4@example.com"
       
    );
       
       foreach (
    $recipients_array as $name => $address) {
           
    $to_send .= "<option value=\"" $name "&lt;" $address "&gt; \" >" $name "</option>\n";
       }
       
       return 
    $to_send;
    }

    if (isset(
    $_POST["sender"])) {

       
    $recipient $_POST["recipient"];
       
    $sender    $_POST["sender"];
       
    $subject   $_POST["subject"];
       
    $message   $_POST["message"];
       
    $reply_to  $_POST["sender_address"];

       @
    mail($recipient$subject$message"From: "$sender "Reply-To: " $reply_address);
       print 
    "<html><body>Thank you for your submission!</body></html>";
         
    } else {

    ?> 

    <html>
       <head>
          <title>Contact Form</title>
          <style type="text/css">
          <!--
          
             html body { text-align: center; background-color:#666666}
             h1 { font-size: 2em; }
             
             .bold { font-weight: bold; }
             
             #container { 
                text-align: left; 
                width: 450px; 
                margin: 0 auto;
                border: solid #000000;
                background-color:#ffffff;
             }
             
             
             #head { font-family: Garamond; text-align:center;}
             #form { margin: 20px 20px 20px 20px; }
             
             .lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
             .frmElement { width: 300px; text-align:left;}
             .frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
             .frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
             .frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}
             
             #sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
             .frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}
             
          //-->
          </style>
       <body>
          <div id="container">
             <div id="head"><h1>Contact Us</h1></head></div>
             <div id="form">
                <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
                   <div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
                   <div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
                   <div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
                   <div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div>
                   <div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>
                   
                   <div id="sub_container">
                      <input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
                   </div>

                </form>
             </div>
          </div>
          
          
       </body>
    </html>

    <?php ?>
    -Celt

  • #4
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help guys! Celtboy: I haven't had the chance to test out your script yet, but by the looks of it, it seems to do exactly what I asked for I'll try it out tomorrow and I'll let you know if I run into any problems, which I probably won't. Thanks!


    ~Jan

  • #5
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Celtboy: Would it be possible to hide the e-mail addresses completly, so that they aren't visible for the visitor, not even in the source?


    ~Jan

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You might be able to if you morph your array into a 2D array:
    PHP Code:
    $recipients_array = array();

    $recipients_array[0] = array();
    $recipients_array[0]['name'] = "Recipient 1";
    $recipients_array[0]['email'] = "recip1@example.com";

    $recipients_array[1] = array();
    $recipients_array[1]['name'] = "Recipient 2";
    $recipients_array[1]['email'] = "recip2@example.com";

    $recipients_array[2] = array();
    $recipients_array[2]['name'] = "Recipient 3";
    $recipients_array[2]['email'] = "recip3@example.com";

    $recipients_array[3] = array();
    $recipients_array[3]['name'] = "Recipient 4";
    $recipients_array[3]['email'] = "recip4@example.com";

    function 
    populate_recipient_select() { 
     global 
    $recipients_array//allow use of the recipients array...

     
    $to_send "";
     
     foreach (
    $recipients_array as $idx => $info_array) { 
      
    $to_send .= "<option value=\"" $idx "\" >" $recipients_array[$idx]['name'] . "</option>\n"
     } 
     
     return 
    $to_send


    if (isset(
    $_POST["sender"])) { 

       
    $recipient $recipients_array[$_POST["recipient"]]['email']; 
       
    $sender    $_POST["sender"]; 
       
    $subject   $_POST["subject"]; 
       
    $message   $_POST["message"]; 
       
    $reply_to  $_POST["sender_address"]; 

       @
    mail($recipient$subject$message"From: "$sender "Reply-To: " $reply_address); 
       print 
    "<html><body>Thank you for your submission!</body></html>"
          
    } else { 
    Untested, but that should do the trick.

    If this list of recipients is going to grow and/or change, you may want to consider putting this information in a database, but the concept is the same..

    Hope that helps/works,
    Sadiq.

  • #7
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    also untested, but you should be able to just change the top of the code to this (the rest of the code stays the same):

    PHP Code:
    <?php

    function populate_recipient_select() {
       
    $to_send "";
       
    $recipients_array = array(
           
    "Recipient 1" => "recip1@example.com",
           
    "Recipient 2" => "recip2@example.com",
           
    "Recipient 3" => "recip3@example.com",
           
    "Recipient 4" => "recip4@example.com"
       
    );
       
       foreach (
    $recipients_array as $name) {
           
    $to_send .= "<option value=\"" $name "\" >" $name "</option>\n";
       }
       
       return 
    $to_send;
    }

    if (isset(
    $_POST["sender"])) {

       
    $recipient $_POST["recipient"];
       
    $sender    $_POST["sender"];
       
    $subject   $_POST["subject"];
       
    $message   $_POST["message"];
       
    $reply_to  $_POST["sender_address"];

       @
    mail($recipients_array[$recipient], $subject$message"From: "$sender "Reply-To: " $reply_address);
       print 
    "<html><body>Thank you for your submission!</body></html>";
         
    } else {

    ?>
    -Celt

  • #8
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys for your replies. However, there are some issues. First off, Celtboys suggestion still shows the e-mail addresses Look here: http://private.scanactive.com/experi...lecontact1.php. Here's the code:

    PHP Code:
    <?php 

    function populate_recipient_select() { 
       
    $to_send ""
       
    $recipients_array = array( 
           
    "Recipient 1" => "recip1@example.com"
           
    "Recipient 2" => "recip2@example.com"
           
    "Recipient 3" => "recip3@example.com"
           
    "Recipient 4" => "recip4@example.com" 
       
    ); 
        
       foreach (
    $recipients_array as $name) { 
           
    $to_send .= "<option value=\"" $name "\" >" $name "</option>\n"
       } 
        
       return 
    $to_send


    if (isset(
    $_POST["sender"])) { 

       
    $recipient $_POST["recipient"]; 
       
    $sender    $_POST["sender"]; 
       
    $subject   $_POST["subject"]; 
       
    $message   $_POST["message"]; 
       
    $reply_to  $_POST["sender_address"]; 

       @
    mail($recipients_array[$recipient], $subject$message"From: "$sender "Reply-To: " $reply_address); 
       print 
    "<html><body>Thank you for your submission!</body></html>"
          
    } else { 

    ?> 

    <html> 
       <head> 
          <title>Contact Form</title> 
          <style type="text/css"> 
          <!-- 
           
             html body { text-align: center; background-color:#666666} 
             h1 { font-size: 2em; } 
              
             .bold { font-weight: bold; } 
              
             #container { 
                text-align: left; 
                width: 450px; 
                margin: 0 auto; 
                border: solid #000000; 
                background-color:#ffffff; 
             } 
              
              
             #head { font-family: Garamond; text-align:center;} 
             #form { margin: 20px 20px 20px 20px; } 
              
             .lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;} 
             .frmElement { width: 300px; text-align:left;} 
             .frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
             .frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
             .frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;} 
              
             #sub_container { width: 450px; margin: 0 -45px auto; text-align:right;} 
             .frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
              
          //--> 
          </style> 
       <body> 
          <div id="container"> 
             <div id="head"><h1>Contact Us</h1></head></div> 
             <div id="form"> 
                <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST"> 
                   <div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div> 
                   <div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div> 
                   <div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div> 
                   <div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div> 
                   <div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div> 
                    
                   <div id="sub_container"> 
                      <input type="submit" name="submit" value="Submit" class="frmSubmit bold" /> 
                   </div> 

                </form> 
             </div> 
          </div> 
           
           
       </body> 
    </html> 

    <?php ?>
    Then, to Sadiq's suggestion where there seems to be another problem: http://private.scanactive.com/experi...lecontact2.php. The code:

    PHP Code:
    <?php 

    function populate_recipient_select() { 
       
    $to_send ""
    $recipients_array = array(); 

    $recipients_array[0] = array(); 
    $recipients_array[0]['name'] = "Recipient 1"
    $recipients_array[0]['email'] = "recip1@example.com"

    $recipients_array[1] = array(); 
    $recipients_array[1]['name'] = "Recipient 2"
    $recipients_array[1]['email'] = "recip2@example.com"

    $recipients_array[2] = array(); 
    $recipients_array[2]['name'] = "Recipient 3"
    $recipients_array[2]['email'] = "recip3@example.com"

    $recipients_array[3] = array(); 
    $recipients_array[3]['name'] = "Recipient 4"
    $recipients_array[3]['email'] = "recip4@example.com"

    function 
    populate_recipient_select() {  
     global 
    $recipients_array//allow use of the recipients array... 

     
    $to_send ""

     foreach (
    $recipients_array as $idx => $info_array) {  
      
    $to_send .= "<option value=\"" $idx "\" >" $recipients_array[$idx]['name'] . "</option>\n";  
     }  

     return 
    $to_send;  
    }  

    if (isset(
    $_POST["sender"])) {  

       
    $recipient $recipients_array[$_POST["recipient"]]['email'];  
       
    $sender    $_POST["sender"];  
       
    $subject   $_POST["subject"];  
       
    $message   $_POST["message"];  
       
    $reply_to  $_POST["sender_address"];  

       @
    mail($recipient$subject$message"From: "$sender "Reply-To: " $reply_address);  
       print 
    "<html><body>Thank you for your submission!</body></html>";  
           
    } else {  

    ?>  

    <html> 
       <head> 
          <title>Contact Form</title> 
          <style type="text/css"> 
          <!-- 
           
             html body { text-align: center; background-color:#666666} 
             h1 { font-size: 2em; } 
              
             .bold { font-weight: bold; } 
              
             #container { 
                text-align: left; 
                width: 450px; 
                margin: 0 auto; 
                border: solid #000000; 
                background-color:#ffffff; 
             } 
              
              
             #head { font-family: Garamond; text-align:center;} 
             #form { margin: 20px 20px 20px 20px; } 
              
             .lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;} 
             .frmElement { width: 300px; text-align:left;} 
             .frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
             .frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
             .frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;} 
              
             #sub_container { width: 450px; margin: 0 -45px auto; text-align:right;} 
             .frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;} 
              
          //--> 
          </style> 
       <body> 
          <div id="container"> 
             <div id="head"><h1>Contact Us</h1></head></div> 
             <div id="form"> 
                <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST"> 
                   <div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div> 
                   <div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div> 
                   <div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div> 
                   <div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select();?></select></div> 
                   <div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div> 
                    
                   <div id="sub_container"> 
                      <input type="submit" name="submit" value="Submit" class="frmSubmit bold" /> 
                   </div> 

                </form> 
             </div> 
          </div> 
           
           
       </body> 
    </html> 

    <?php ?>
    Have I done this the right way, or what?


    ~Jan

  • #9
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Sorry about that. I shouldn't have used $name, and the recipient's list needs to be global. Try the following:

    try using this code:

    PHP Code:
    <?php 

       $recipients_array 
    = array( 
           
    "Recipient 1" => "recip1@example.com"
           
    "Recipient 2" => "recip2@example.com"
           
    "Recipient 3" => "recip3@example.com"
           
    "Recipient 4" => "recip4@example.com" 
       
    );

    function 
    populate_recipient_select($recipients_array) { 

        
       
    $to_send ""
        
        
       foreach (
    $recipients_array as $recip_name=>$value) { 
           
    $to_send .= "<option value=\"" $recip_name "\" >" $recip_name "</option>\n"
       } 
        
       return 
    $to_send


    if (isset(
    $_POST["sender"])) { 

       
    $recipient $_POST["recipient"]; 
       
    $sender    $_POST["sender"]; 
       
    $subject   $_POST["subject"]; 
       
    $message   $_POST["message"]; 
       
    $reply_to  $_POST["sender_address"]; 
       
       @
    mail($recipients_array[$recipient], $subject$message"From: "$sender "\r\nReply-To: " $reply_address); 
       print 
    "<html><body>Thank you for your submission!</body></html>"
          
    } else { 

    ?> 

    <html>
       <head>
          <title>Contact Form</title>
          <style type="text/css">
          <!--
           
             html body { text-align: center; background-color:#666666}
             h1 { font-size: 2em; }
              
             .bold { font-weight: bold; }
              
             #container {
                text-align: left;
                width: 450px;
                margin: 0 auto;
                border: solid #000000;
                background-color:#ffffff; 
             }
              
              
             #head { font-family: Garamond; text-align:center;}
             #form { margin: 20px 20px 20px 20px; }
              
             .lbl { font-family: Garamond; font-size: 1em; float:left; width: 150px; text-align:left; font-weight: bold; clear:leftt;}
             .frmElement { width: 300px; text-align:left;}
             .frmText { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
             .frmSelect { font-family: Garamond; color: #888888; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0;}
             .frmTextArea { font-family: Garamond; color: #888888 ; font-size: 1em; background-color: #EEEEEE; margin: 0 0 15px 0; text-align:top;}
              
             #sub_container { width: 450px; margin: 0 -45px auto; text-align:right;}
             .frmSubmit { font-family: Garamond; color: #000000; font-size: 1.2em; background-color: #EEEEEE; margin: 0 0 15px 0;}
              
          //-->
          </style>
       <body>
          <div id="container">
             <div id="head"><h1>Contact Us</h1></head></div>
             <div id="form">
                <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
                   <div class="lbl">Your Name:</div><div class="frmElement"><input type="text" name="sender" class="frmText" /></div>
                   <div class="lbl">Your Email:</div><div class="frmElement"><input type="text" name="sender_address" class="frmText" /></div>
                   <div class="lbl">Subject:</div><div class="frmElement"><input type="text" name="subject" class="frmText" /></div>
                   <div class="lbl">Select a Recipient:</div><div class="frmElement"><select name="recipient" class="frmSelect"><?php print populate_recipient_select($recipients_array);?></select></div>
                   <div class="lbl">Your Message:</div><div class="frmElement"><textarea class="frmTextArea bold" name="message" rows="15" cols="53"></textarea></div>
                    
                   <div id="sub_container">
                      <input type="submit" name="submit" value="Submit" class="frmSubmit bold" />
                   </div>

                </form>
             </div>
          </div>
           
           
       </body>
    </html>

    <?php ?>
    -Celt
    [/php]

  • #10
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, it works great now! Thanks, Celtboy!


    ~Jan

  • #11
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi again! I'd have one more question if you guys don't mind. Is there any way to have the message previewed before sending it? I mean so that the user can check that everything is correct? No biggie, but would be a cool extra


    ~Jan


  •  

    Posting Permissions

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