SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating email form with destination choices

    I want to create a form where people can choose from a dropdown which email address to send to and then use a text box to type the message to be sent. Does anyone know how I would set up the dropdown box with these choices?

    Any help would be greatly appreciated!!

  2. #2
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found one on hotscripts.com! Thanks anyways.

  3. #3
    Object Not Found junjun's Avatar
    Join Date
    Mar 2002
    Location
    northern MI
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lightwithoutheat
    Found one on hotscripts.com! Thanks anyways.
    Would you like to share? :-)

  4. #4
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I thought I had it, but only solved half of my problem. I used radio buttons, but am having a problem...

    In the <head>:

    <script type="text/javascript">
    <!-- Activate Cloaking Device

    var browser=navigator.appName + " " + navigator.appVersion;

    // Used to display and load email address
    var address="";

    // Called by onClick in radio buttons
    function setAddress(addr)
    {
    address=addr;
    document.email.display.value=address;
    }

    // Deactivate Cloaking -->
    </script>


    In the <body>:
    <form action="formmail.php" method="POST">
    <b>Your Name</b><br /> <input type="text" name="realname" size="30"><br />
    <b>Your Email Address</b><br /> <input type="text" name="email" value="example&#058; anybody&#064;yahoo&#046;com" size="30"><br />
    <b>Your Message</b><br /> <textarea cols="30" name="emailmessage" rows="8"></textarea><br />
    <b>Send message to/b>
    <input type="radio" name="radiobutton" onClick="setAddress('whatever1@anywhere.com')">email1</input>
    <input type="radio" name="radiobutton" onClick="setAddress('whatever2@anywhere.com')">email2</input>
    <br />
    <input type="SUBMIT" value=" Submit message ">
    </form>

  5. #5
    Object Not Found junjun's Avatar
    Join Date
    Mar 2002
    Location
    northern MI
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lightwithoutheat
    Actually, I thought I had it, but only solved half of my problem. I used radio buttons, but am having a problem...
    Since you're using PHP already, you could let it handle it instead. Assign a value to each button, and let the php script you are using insert the corresponding address.

  6. #6
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    If I only knew how to do that...!

  7. #7
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    a better approach would be to create a drop-down list using <select> tag. Place the email addresses in there & the user can choose the email address to which it want to send email. Since he can choose only one option, only that option would be submitted to the PHP script.

    Here's a sample code:-

    HTML Code
    HTML Code:
    <form action="formmail.php" method="POST">
    <b>Send message to:</b>
    <select name="sendTo">
    <option value="enquiry" selected>Enquiries</option>
    <option value="sales">Sales</option>
    <option value="support">Support</option>
    </select>
    <br />
    <b>Your Name</b><br /> <input type="text" name="realname" size="30"><br />
    <b>Your Email Address</b><br /> <input type="text" name="email" value="anybody@yahoo.com" size="30"><br />
    <b>Your Message</b><br /><textarea cols="30" name="emailmessage" rows="8"></textarea><br />
    <input type="SUBMIT" value=" Submit message ">
    </form>

    Now the PHP Code of formmail.php
    PHP Code:
    $to_suffix "@example.com"//name of your domain after the @ symbol
    $to_prefix $_POST['sendTo']; //get the value from drop down list
    $to $to_prefix.$to_suffix//join to get complete eMail address
    $from $_POST['email']; //visitor's eMail
    $from_name $_POST['realname']; //visitor's name
    $message $_POST['emailmessage']; //visitor's message
    $subject "Mail from Your Visitor"//subject of the eMail
    $mail_headers "From: $from_name <$from> \r\n";
    $mail_headers .= "MIME-Version: 1.0\n" "Content-type: text/plain;charset=iso-8859-1";
    // the eMail headers being set
    $mail_success mail($to$subject$message$mail_headers); //send eMail
    if ($mail_success)
    {
    echo 
    "Thanks for sending your feedback.";
    // display message if the eMail is sent
    }
    else 
    {
    echo 
    "eMail cannot be sent.";
    // display message if the eMail is not sent


    Try this code. It'll work & there's no JavaScript involved. Plain HTML with plain PHP. However you can use JavaScript in the HTML code to validate the form.


    Edit:


    PHP Code fixed.
    Last edited by asp_funda; Mar 20, 2004 at 17:07.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  8. #8
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by junjun
    Since you're using PHP already, you could let it handle it instead. Assign a value to each
    button, and let the php script you are using insert the corresponding
    address.
    This can be handled using the switch(). I mean that if you select a radio
    button/checkbox & a value corresponding to that is entered in a variable
    on the server using PHP. switch() is actually like multiple if/else statements.

    But I've posted a better solution in my previous post, using the drop-down list.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  9. #9
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks asp_funda! I will give that a go and will try both solutions to see which one is best suited for my situation. I appreciate it.

  10. #10
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I can tell you beforehand, unless you like things the
    difficult way, you'll be pleased with the 1st solution that
    I gave you, which uses the <select> tag. Its the
    simplest way with least maintenance headache.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  11. #11
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used the first method that you gave me and I receive the error that the email cannot be sent. This is in the email:

    @mydomain.com: no local part

  12. #12
    Object Not Found junjun's Avatar
    Join Date
    Mar 2002
    Location
    northern MI
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aspfunda,
    providing sample code, gets nice reps :-)

  13. #13
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lightwithoutheat
    I used the first method that you gave me and I receive the
    error that the email cannot be sent. This is in the email:

    @mydomain.com: no local part
    Oh! Gosh!! That was a silly mistake of mine. Fixed the PHP Code.
    Just copy & paste the PHP Code again.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  14. #14
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by junjun
    aspfunda,
    providing sample code, gets nice reps :-)
    Thanks
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  15. #15
    SitePoint Zealot lightwithoutheat's Avatar
    Join Date
    Feb 2002
    Location
    Washington DC
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much. I got it to work, and it works well. My problem was with the option: <option value="enquiry" selected>Enquiries</option>

    I was putting an entire email address in the value field, when it called for the first part of the address and then parsed the rest on send. My misunderstanding. But thanks anyway. I'm sure I will be using that code on many of the sites I do. Sitepointforums has been good to me...

  16. #16
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Glad that worked for you.
    But you see, there was a problem with the PHP code
    that I posted 1st. It was retrieving a variable
    "myMail" instead of "sendTo". So, even if you hadn't
    made a mistake with the option value, though the
    script would've worked, it wouldn't have sent the
    eMail to you.

    When you said that script isn't working, I checked the
    variables first, as usual, & spotted the problem & fixed
    it.

    Use it where ever you want.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


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
  •