Solved

Setting up form with Multiple drop down options

Posted on 2013-06-19
7
290 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 109

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 109

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 109

Accepted Solution

by:
Ray Paseur earned 500 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 109

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

860 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