SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 47
  1. #1
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email form with dropdown boxes

    Hi,

    Been awhile since I've posted here...keeping busy.

    I'm redoing my site a bit, and I want to create just one contact form with dropdown boxes for the different choices for certain options. I wanted dropdown boxes for who the visitor would want to contact and the subject of the email...at least. I kind of wanted a starting point then branch from there if I needed to modify it a bit.

    I'm not efficient with PHP...I just haven't had a chance to learn it thoroughly as I probably should.

    Can anyone help me out with this? If there's one that fulfills this need at a scripts site (i.e. HotScripts.com, Dynamic Drive), my apologies...I just didn't know which one I could go with that was secure and simple.

    Thanks!!!

    Bryan
    Bryan T. Villarin
    All Narfed Up My System Rig

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Here's something that hopefully get you started
    PHP Code:
    <?php 
    error_reporting
    (E_ALL);

    $emails = array('Geroge Bush' => 'bush@us.com'
                    
    'Bill Clinton' => 'clinton@us.com'
                    
    'Ronald Reagan' => 'reagan@us.com'
                   
    );
    $subjects = array(=> 'Help',
                      
    => 'Feedback',
                      
    => 'Tip'
                     
    );


    if(
    count($_POST) > 0) {
        if(
    array_key_exists($_POST['email'], $emails) && array_key_exists($_POST['subject'], $subjects)) {
            
    $email $emails[$_POST['email']];
            
    $subject $subjects[$_POST['subject']];
            
    $message = isset($_POST['message']) ? $_POST['message'] : '';
        
            
    // Send email
            
    echo 'Thank you! Your mail is sent to '$_POST['email'];
        } else {
            echo 
    'Did you try to hack something?';
        }

    } else {
        
    $emailoptions '';
        foreach(
    $emails as $name=>$email) {
            
    $emailoptions .= "<option>$name</option>";
        }

        
    $subjectoptions '';
        foreach(
    $subjects as $id=>$subject) {
            
    $subjectoptions .= "<option value=\"$id\">$subject</option>";
        }
    ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

            To: 
            <select name="email" size="1">
            <?php echo $emailoptions?>
            </select>

            Subject: 
            <select name="subject" size="1">
            <?php echo $subjectoptions?>
            </select>

            Message: 
            <textarea name="message" cols="20" rows="5"></textarea>

            <button type="submit" name="submit">Send email</button>
        </form>
    <?php
    }
    ?>
    -Helge

  3. #3
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Helge,

    I tried that form, changing the emails and subjects arrays, and changing the "Your email is sent to $my_email_address" not to divulge my email address. It didn't work though.

    Would posting my current email form help? I got it from a book.
    Bryan T. Villarin
    All Narfed Up My System Rig

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    What did not work? The form itself or didn't it send an email? I did test the form and it worked for me.
    The code I posted do not actual send an email. I left that out to be an exercise
    You need to use the mail() function. It should be placed in the script where the comment "// Send mail" is.

    -Helge

  5. #5
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not knowledgeable with PHP, so...yeah. I don't know.
    Bryan T. Villarin
    All Narfed Up My System Rig

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Post you current script and I look into. If it's very big, try to strip out some HTML/layout (not the form )

    -Helge
    Last edited by Helge; Oct 21, 2003 at 01:01. Reason: Typo

  7. #7
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?
    $form_block = "
    <form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
    <p><font size=\"2\">Your Name:</font><br>
    <input type=\"text\" name=\"sender_name\" value=\"$_POST[sender_name]\" size=40></p>
    <p><font size=\"2\">Your E-mail Address:</font><br>
    <input type=\"text\" name=\"sender_email\" value=\"$_POST[sender_email]\"size=40></p>
    <p><font size=\"2\">Subject:</font><br>
    <input type=\"text\" name=\"subject\" value=\"$_POST[subject]\" size=40></p>
    <p><font size=\"2\">Message:</font><br>
    <textarea name=\"message\" cols=50 rows=7 wrap=virtual>$_POST[message]
    </textarea></p>
    <input type=\"hidden\" name=\"op\" value=\"ds\">
    <p><input type=\"submit\" name=\"submit\" value=\"Send This Form\"></p>
    </form>";
    
    if ($_POST[op] != "ds") {
    // they need to see the form
    echo "$form_block";
    }
    
    else if ($_POST[op] == "ds") {
    if ($_POST[sender_name] == "") {
    $name_err = "<font color=red>Please enter your name!</font><br>";
    $send = "no";
    }
    
    if ($_POST[sender_email] == "") {
    $email_err = "<font color=red>Please enter your email address!</font><br>";
    $send = "no";
    }
    
    if ($_POST[subject] == "") {
    $subject_err = "<font color=red>Please enter a subject!</font><br>";
    $send = "no";
    }
    
    if ($_POST[message] == "") {
    $message_err = "<font color=red>Please enter a message!</font><br>";
    $send = "no";
    }
    
    if ($send != "no") {
    	// it's ok to send!
    $msg = "Email Sent from btvillarin.com\n";
    $msg .= "Sender's Name:    $_POST[sender_name]\n";
    $msg .= "Sender's E-Mail:  $_POST[sender_email]\n";
    $msg .= "Subject:          $_POST[subject]\n";
    $msg .= "Message:          $_POST[message]\n\n";
    
    $to = "anonworkingemailaddress@btvillarin.com";
    $subject = "Contact Form";
    $mailheaders = "From: btvillarin.com <> \n";
    $mailheaders .= "Reply-To: $_POST[sender_email]\n\n";
    
    
    mail($to, $subject, $msg, $mailheaders);
    
    echo "<p><font size=\"2\"><em>Your email has been sent...thank you!</em></font></p>";
    echo "<p><font size=\"2\"><em>You will be returned to <a href=\"http://www.btvillarin.com\">btvillarin.com</a> in five seconds...</em></font></p>";
    echo "<meta http-equiv=\"refresh\" content=\"5;URL=http://www.btvillarin.com\">";
    
    }
    else if ($send == "no") {
    echo "$name_err";
    echo "$email_err";
    echo "$subject_err";
    echo "$message_err";
    echo "$form_block";
    }
    }
    ?>
    Bryan T. Villarin
    All Narfed Up My System Rig

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where are the drop down boxes you talked about in your first post?

    -Helge

  9. #9
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have them...I used them on a separate page without any of the code above that isn't on the server anymore.

    I made this form months ago, and...it was from the book.
    Bryan T. Villarin
    All Narfed Up My System Rig

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want them?

    -Helge

  11. #11
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes please.
    Bryan T. Villarin
    All Narfed Up My System Rig

  12. #12
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which options do you want drop down boxes for? 'Subject' and 'Send email to'?

    -Helge

  13. #13
    SitePoint Zealot btvillarin's Avatar
    Join Date
    Nov 2002
    Location
    Arcadia, California
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, just to get started. Once I see the code, I'll probably just modify it as my needs increase or decrease.

    Thanks Helge!!!
    Bryan T. Villarin
    All Narfed Up My System Rig

  14. #14
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    I thought I posted the code yesterday, but I can't find it...

    Anyway, you just have to alter the two arrays $sendto and $subjects and the script should be working.
    The $sendto array contains the names and email adresses of people that the user can send an email to.
    The $subject array contains the subjects the user can choose between. Obvious
    PHP Code:
    <?
    error_reporting
    (E_ALL);

    // Here goes the persons (and their emails) that the user can choose between sending an email to
    $sendto = array('Geroge Bush' => 'bush@us.com'
                    
    'Bill Clinton' => 'clinton@us.com'
                    
    'Ronald Reagan' => 'reagan@us.com' 
                   
    ); 
     
    // Here goes the different subject that the user can choose between.
    $subjects = array(=> 'Help'
                      
    => 'Feedback'
                      
    => 'Tip' 
                     
    ); 

    $sendername = empty($_POST['sendername']) ? '' $_POST['sendername'];
    $senderemail = empty($_POST['senderemail']) ? '' $_POST['senderemail'];
    $to = empty($_POST['to']) ? '' $_POST['to'];
    $subject = empty($_POST['subject']) ? '' $_POST['subject'];
    $message = empty($_POST['message']) ? '' $_POST['message'];

    if(
    count($_POST) > 0) { 
        
    $errorMsg = array();
        if(empty(
    $sendername)) $errorMsg[] = 'Your name';
        if(empty(
    $senderemail)) $errorMsg[] = 'Your E-mail address';
        if(empty(
    $to)) $errorMsg[] = 'To';
        if(empty(
    $subject)) $errorMsg[] = 'Subject';
        if(empty(
    $message)) $errorMsg[] = 'Message';
        
        
        if(empty(
    $errorMsg) && array_key_exists($to$sendto) && array_key_exists($subject$subjects)) { 
            
    $mailmsg "Email Sent from btvillarin.com\n";
            
    $mailmsg .= "Sender's Name:    $sendername\n";
            
    $mailmsg .= "Sender's E-Mail:  $senderemail\n";
            
    $mailmsg .= "Subject:          {$subjects[$subject]}\n";
            
    $mailmsg .= "Message:          $message\n\n";
            
            
    $mailrecipient $sendto[$to];
            
            
    $mailsubject "Contact Form";
            
    $mailheaders "From: btvillarin.com <> \n";
            
    $mailheaders .= "Reply-To: $senderemail\n\n";
            
            if(
    mail($mailrecipient$mailsubject$mailmsg$mailheaders)) {
                echo 
    "<p><font size=\"2\"><em>Your email has been sent to $to. Thank you!</em></font></p>";
                echo 
    "<p><font size=\"2\"><em>You will be returned to <a href=\"http://www.btvillarin.com\">btvillarin.com</a> in five seconds...</em></font></p>";
                echo 
    "<meta http-equiv=\"refresh\" content=\"5;URL=http://www.btvillarin.com\">";
                exit;
            } else {
                echo 
    '<p><font color=red>An error occured while trying to send your email</p>';
            }
            
        } else { 
            echo 
    '<p><font color=red>The following field did not get properly filled out:<br />';
            foreach(
    $errorMsg as $msg) {
                echo 
    " - $msg<br />";
            }
            echo 
    '</font></p>';
        } 
    }

    $tooptions ''
    foreach(
    $sendto as $name=>$email) { 
        
    $selected = ($to == $name) ? 'selected="selected" ' '';
        
    $tooptions .= "<option $selected>$name</option>"


    $subjectoptions ''
    foreach(
    $subjects as $id=>$sub) { 
        
    $selected = ($subject == $id) ? 'selected="selected" ' '';
        
    $subjectoptions .= "<option value=\"$id\" $selected>$sub</option>"

    ?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

        <p>
        Your name:
        <input type="text" name="sendername" value="<?php echo $sendername?>" size="40" />
        </p>

        <p>
        Your E-mail Address:
        <input type="text" name="senderemail" value="<?php echo $senderemail?>" size=40>
        </p>

        <p>
        To: 
        <select name="to" size="1"> 
        <?php echo $tooptions?> 
        </select> 
        </p>

        <p>
        Subject: 
        <select name="subject" size="1"> 
        <?php echo $subjectoptions?> 
        </select> 
        </p>

        <p>
        Message: 
        <textarea name="message" cols="50" rows="7" wrap="virtual"><?php echo $message?></textarea>
        </p>

        <p>
        <button type="submit" name="submit" value="Send This Form">Send This Form</button> 
        </p>
    </form>
    Hopefully this will suit your needs

    -Helge

  15. #15
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Helge:

    Just happened to click on this post (I was looking for something similar).

    I must say, you are one of the most helpful people on the planet!!!

    One question RE:[PHP
    $subject = empty($_POST['subject']) ? '' : $_POST['subject'];
    [/PHP]Can you please explain this line???

    Thanks much.

    (EDIT
    P.S. Ohhh... forgot... it's actually "George" Bush

  16. #16
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btvillarin:

    You might also want to consider the following (I found and am planning to use myself)...for a mini-validation on the user's email address:
    PHP Code:
    // Validate E-Mail
    function validate_email($email)
    {
        if (
    preg_match("/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i",$email))
     
            {
                    return 
    1;
            }
            return 
    0;


  17. #17
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also found this, but not sure if malicious input might be a problem with PHP mail() ???
    PHP Code:
    // Deal with Malicious input
    function inputCheck($body)
    {
    $body=preg_replace
    ("/(<\/?)(\w+)([^>]*>)/e""'\\1'.strtoupper('\\2').'\\3'",
     
    $body);
    $disallowedTags = array(
    "APPLET",
    "OBJECT",
    "SCRIPT",
    "EMBED",
    "FORM",
    "?",
    "%"
    );
    $count count($disallowedTags);
    foreach (
    $disallowedTags as $value)
        {
        
    $body=str_replace("<".$value"<b>WARNING: 
        
    $value (tag not allowed.) </b> "$body);             
        }
    return 
    $body;

    Like, for the email and message body input areas. Maybe Helge could help in terms of exactly how to plug it in.

  18. #18
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    Quote Originally Posted by RocketMan
    PHP Code:
    $subject = empty($_POST['subject']) ? '' $_POST['subject']; 
    Can you please explain this line???
    I'll try . It's a short syntax for a control structure.

    This is the basics for this type of syntax:

    PHP Code:
    $var = (expr1) ? (expr2) : (expr3); 
    If expr1 evaluates to TRUE, expr2 will be evaluated. If expr1 on the other hand evaluates to FALSE expr3 will be evaluated. The output from this control structure will be assigned to the $var.
    Instead of assigning it to a variable you can echo the result out aswell: echo (expr1) ? (expr2) : (expr3).
    This structure can be nested aswell (I don't how deep though): $var = (expr1) ? (expr2) : ((expr31) ? (expr32) : (expr33));

    You don't need the parenthesis to get it to work.

    Quote Originally Posted by RocketMan
    (EDIT
    P.S. Ohhh... forgot... it's actually "George" Bush
    Ooops, small typo

    -Helge

  19. #19
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Helge:

    I tried to modify the following to include the $subject along with a (test) Generic 'subject' line (for routing to various mailfolders based upon Subject line contents:
    PHP Code:
    $mailsubject "Generic" "-" "$subject"
    What I had hoped to get was:
    PHP Code:
    Generic Feedback 
    What I got was:
    PHP Code:
    Generic-
    What am I doing wrong???

    Thank you very much.

    (EDIT
    Umh, I figured it out:
    PHP Code:
    $mailsubject "Generic" "-" "{$subjects[$subject]}"
    Last edited by RocketMan; Oct 22, 2003 at 03:19.

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    The value of the subject field returned from the form is the id of a subjects array. So you need to access the associative array $subjects using the $subject variable.

    Try
    PHP Code:
    $mailsubject 'Generic - ' $subjects[$subject]; 
    I change the quotes from double to single. You should use single as long as there is no content in the string that needs to be evaluated by the PHP parser/engine

    HTH

    -Helge

  21. #21
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (hehehe)...our Posts just crossed paths

    But thanks for the info about the quotes.

    BTW, is it important to check for malicious input(s)...in the Name, Email Address & Message areas?

    Tnx.

  22. #22
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helge:

    Ooops...just jumped over your other post...thanks for the nice explanation of that code, etc.

    It is now almost 3:30AM local. Must go ZZZZZZzzzzzzzzz now.

    See you soon...and thanks again!!!

  23. #23
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Quote Originally Posted by RocketMan
    BTW, is it important to check for malicious input(s)...in the Name, Email Address & Message areas?
    Actually I'm not sure. I don't know if the $msg in example get parsed or if it's just handled as a string. If it's just ahndled as a string I don't think it can do any harm.

    Hopfully some of the gurus can shed some light on this.

    The senders email address would be ok to check because then it would be more likely that you can reply. On the other side, if the user want a reply he will give the correct email address.
    Quote Originally Posted by RocketMan
    Must go ZZZZZZzzzzzzzzz now
    Have a good sleep.

    See you around.

    -Helge

  24. #24
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Helge...

    I need to get to bed!!!

    But it just occured to me that btvillarin, myself & maybe others would be interested in a couple of 'tweaks' to the script:

    1. To yield an error message if someone does NOT select the To: or Subj: options (i.e., make it a "Select One" arrangement). Some people need to be 'guided' in online forms

    2. I dunno if this is possible, but instead of re-directing to a main website domain, is there a way to have the redirect go back to the page from wherever the person clicked on the email 'form' ???

    3. If a person has an e-mail link to the form on every page in their website, it would be very cool to include (in the sent email) the actual page from where someone clicked on the e-mail form!!! That's what I was experimenting with for input to a DB with:
    PHP Code:
    $pagename $_SERVER['PHP_SELF'];
    $pagename preg_replace("/\//"""$pagename); 
    I'm thinking this could work for insertion into the email???

  25. #25
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (hehehe)...

    This works (inserted into the $message lines):
    PHP Code:
    $mailmsg .= "From Page: $pagename\n"
    But I get an angle tag in the email output:
    PHP Code:
    >From Page: <snip
    Dunno why

    But I just got another idea...if an option item for the Subject might be "Wish List", etc., the form could be tweaked to also input the user's email address & the text directly into a MySQL DB for later use.

    OK...this time (4AM now), I simply must go to bed since my alarm clock is set for 7:15AM

    ZZZZZZZZzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.................


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
  •