Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting up form with Multiple drop down options

Posted on 2013-06-19
7
Medium Priority
?
303 Views
Last Modified: 2013-08-02
Hello,

I'm trying to set up an HTML form with Multiple values in a drop down. The intent is for the output to be emailed to a Branch Mgr and the actual location to be part of the body.

Example:
Email to jdoe@gmail.com

Body of email
Requesting Branch: Boston

I'm able to get the attached code to work with an array as an alert but not to output to email. I have my output to email attempt <!-- in a comment --> Also, I don't necessarily require Value 1 to show on my code as long as I can see Requested Branch.

Exact code is much more lengthier with more output than what is attached. Any help would be gratefully appreciated.

Thanks

<html>
<head>
<title>Multiple Option Attributes</title>
</head>
<body>

<form name="transferform" id = "transferform">


<p>
Requested Branch: <br>
<select name="requestbranch" id="requestbranch" onchange="rem1(this)">
   <option selected value=""> Choose One</option>
   <option value="Boston"> Boston </option>
   <option value="Raleigh"> Raleigh </option>
   <option value="Orlando"> Orlando </option>
   <option value="Topeka">Topeka</option>
   
</select>

</form>

<script type = "text/javascript">
// Branch Array

var branch = [];
    branch['Boston'] = ["Boston","jdoe@gmail.com"];
    branch['Raleigh'] = ["Raleigh","bsmith@gmail.com"];
    branch['Orlando'] = ["Orlando","jjones@gmail.com"]
      branch['Topeka'] = ["Topeka","lbaker@gmail.com"]
     
function rem1(which) {
  if (which.value !="") {
      var ndx = document.transferform.requestbranch.selectedIndex;
    var val = which.value;                        // .branch
    var loc = branch[which.value][0];      // .location;
    var email = branch[which.value][1];      // .email;
   
    alert("Value "+ndx+": "+val+"\nRequesting Branch: "+loc+"\nEmail: "+email);
  } else { alert('Requires selection'); }
}

</script>


<!--Code above works as an alert, now I need to get it to output to email showing Requesting Branch: Boston, etc. and to email properly to Branch Manager

<input LANGUAGE="JavaScript" TYPE="button" class="button" VALUE="Submit"
ONCLICK="location.href = &quot;mailto:&quot; +
document.transferForm.requestbranch.options[document.transferForm.requestbranch.selectedIndex].value + &quot;; &quot; +
"+ndx+": "+val+"\nRequesting Branch: "+loc+"\nEmail: "+email: NAME="Send email">-->
</body>
</html>
0
Comment
Question by:hhnetworks
  • 4
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39262616
Do you have a server-side scripting language available, such as PHP?  This form-to-email application is easy if you submit the form to the server.
0
 

Author Comment

by:hhnetworks
ID: 39262907
yes I do but I'm not real familiar with PHP so not quite sure how to write the code. I believe I write separate php code and then call it out in my HTML submit action.

Is an array required for this to function properly? If so, does the array go into HTML or PHP code?

Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39262972
I can show you the framework of a PHP form-to-email script (see snippet).  I expect an array or object would be required, but I'm not sure exactly how to build your application.

This article has some good learning resources for getting a foundation in PHP.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

Author Comment

by:hhnetworks
ID: 39263605
sorry but this is new to me, I hope this is not to confusing. I have the following added to my HTML Form code, is it correct? Whenever I fill the form and hit submit, the actual php code opens up.

<form action="requestform.php" method="post" enctype="text/plain" name="transferForm" class="form" id="transferForm">

Also, should the array be written like so in the php code ? I need the output to get emailed to manager in Boston but also need to know which is the requesting branch and shipping branch. Hence the multiple values in array.

    $to[Boston] = "jdoe@gmail.com";
    $to[Raleigh] = "bsmith@gmail.com";
    $to[Orlando] = "jjones@gmail.com";
    $to['Topeka'] = "lbaker@gmail.com";

Thanks
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39264070
The HTML form statement will cause a POST-method request to be made when the form is submitted.  The request will be made to the relative URL requestform.php.  The contents of the form will be interpreted by the browser and submitted in the request.

In the PHP action script at requestform.php, the PHP program will find the request data in the $_POST array.

This is all pretty basic stuff, and if you're new to PHP you might want to consider hiring a professional developer to help you work through this.  In respect of your time, a pro will get you a quick fix at very modest cost - this is perhaps no more than 1/2 day's work.  If you're just doing this to learn, then the resources in the article I linked above will be helpful.

Best regards, ~Ray
0
 

Author Closing Comment

by:hhnetworks
ID: 39376967
(Ray, my web developer who submitted this question is no longer employed here. I am closing the question and awarding full points for your help)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39377140
Understood and thanks for the points!  Best regards, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You finally migrated Public Folders to Office 365, decommissioned the Public Folder mailbox database and since then, when you send an email from on-premise to mail-enabled Public Folders, you get the following error: "Misconfigured public folder mai…
In this post, we will learn to set up the Group Naming policy and will see how it is going to impact the Display Name and the Email addresses of the Group.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question