logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 11-23-2011, 03:33 AM   #1
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default Conditional Contact Form

I have always used something like this as an example to work off of:

http://www.freecontactform.com/email_form.php

What I want is a topic selection of option1, option2, other:

the option selected would determine the email address the form is submitted to.

I've looked for examples of this unsuccessfully. Is this something fairly basic to set up? I am as green as you get when it comes to PHP.
BullDog is offline   Reply With Quote
Old 11-23-2011, 03:45 AM   #2
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

would something like this work or is there a better method?

Code:
  1. <form name='mailForm' method='post' action='mailsend.php' enctype='text/plain'>
  2. <select name='me' size='1'>
  3. <option value='webmaster'>Web Master</option>
  4. <option value='admin'>Admin</option>
  5. </select>
  6. <input type='submit'>
  7. </form>
Code:
  1. $mailto = $_POST['me'];
  2. if($mailto == 'webmaster')
  3. {
  4. $email = "one@one.com";
  5. }elseif($mailto == 'admin'){
  6. $email = "two@two.com";
  7. };
BullDog is offline   Reply With Quote
Old 11-23-2011, 07:04 AM   #3
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

I started the form layout based on an example in css (no tables).

I have run into a couple issues right off the bat. the radio button alignment you'll notice. And how would you handle the message area? the css wont allow for scrolling.

Test Form
BullDog is offline   Reply With Quote
Old 11-23-2011, 12:08 PM   #4
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

well after hours of trying this over and over with different methods. I decided to switch the radio buttons for a combo box, making for easier alignment. And finally realized the "overflow: hidden;" is what killed the scrollbar.

My original question is still valid tho... regarding a conditional mailto.
BullDog is offline   Reply With Quote
Old 11-23-2011, 12:21 PM   #5
Corrosive
Corrosive's Avatar
 
Join Date: Feb 2008
Location: Bristol, England
Posts: 7,103
Default

So is the condition for who to send the e-mail to driven by the end user? I.e. they select sales, support or customer care (as an example) in the form?
Corrosive is offline   Reply With Quote
Old 11-23-2011, 01:10 PM   #6
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

correct. the category selected will determine the address the processed form is sent to.

http://bugorama.com/test/contact.php
BullDog is offline   Reply With Quote
Old 11-23-2011, 03:26 PM   #7
Corrosive
Corrosive's Avatar
 
Join Date: Feb 2008
Location: Bristol, England
Posts: 7,103
Default

OK, I'd take the 'if' statements out if the equation and and feed in the correct e-mail address using the 'select' in the form. Set it up like;

Code:
<select>
<option value="webmaster@example.com">Webmaster</option>
<option value="sales@example.com">Sales</option>
<option value="care@example.com">Customer Care</option>
</select>
That way the customer sees the options but the value passed to the $mail variable will be the full e-mail address. Then you can use it with the mailto function.

Make sense?
Corrosive is offline   Reply With Quote
Old 11-23-2011, 03:36 PM   #8
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

yes I think so... thank you. looks like my form needs some changes before I can start to figure out the php.
BullDog is offline   Reply With Quote
Old 11-23-2011, 11:18 PM   #9
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

based on a few examples this is as far as I got. I'm not sure how to pass the combo box value into the process file. nor can I figure out how to make sure the combo box is validated. something must be chosen.

http://bugorama.com/test/contact.php

Code:
<?php

if(isset($_POST['email'])){
    
    // Here is the email to information
    $email_to = "your email address goes here";
    $email_subject = "From Website 2012 Contact Form";
    $email_from = "Bugorama Promotions 2012";
    
    // Error code
    function died($error){
        echo "We are sorry, but there were error(s) found with the form you submitted.";
        echo "These errors appear below.<br /><br />";
        echo $error. "<br /><br />";
        echo "Please go back and fix these errors. <br />";
        die();
        }

    // Validation
        if(!isset($_POST['category']) ||
        !isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])){
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }
        
        $category = $_POST['category'];
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.+[A-Za-z] (2,4)$/';
        if(!preg_match($email_exp, $email)) {
            $error_message .= 'The email address you entered does not appear to be valid.<br />';    
            }
        $string_exp = "    /^[A-Za-z.'-]+$/";
        if(!preg_match($string_exp, $name)){
            $error_message .= 'The name you entered does not appear to be valid.<br />';
            }
        if(strlen($message) < 2){
            $error_message .= 'The message you entered do not appear to be valid.<br />';
            }
        if(strlen($error_message) > 0) {
            died($error_message);
            }
            $email_message = "Form details below. \n\n";
            
        function clean_string($string){
            $bad = array("content-type", "bcc:", "to:", "cc:", "href");
            return str_replace(bad, "", $string);
            }    
        $email_message .= "Name:" . clean_string($name) . "\n";
        $email_message .= "Email:" . clean_string($email) . "\n";
        $email_message .= "Message:" . clean_string($message) . "\n";
        
    //    Create email headers
    $headers = 'From: ' .$email_From . "\r\n". 'Reply-To:' . $email. "\r\n" . 
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- Success message goes here -->
<strong>
<font size="+1">
Thank you for contacting us. We will be in touch with you shortly. 
<p>Please click <a href="index.php">HERE</a> to return to the home page.</p>
</font>
</strong>
<?php
    }
?>
BullDog is offline   Reply With Quote
Old 11-23-2011, 11:49 PM   #10
BullDog
 
Join Date: Nov 2011
Location: Alameda, CA
Posts: 156
Default

will this work for validation, to make sure something is selected? What about the default selection with a value of "0"?

Code:
if(!isset($_POST['category'])) {
          $errorMessage .= "Please select a category.";
        }
BullDog is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:48 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com