Link to home
Start Free TrialLog in
Avatar of hhnetworks
hhnetworksFlag for United States of America

asked on

Setting up form with Multiple drop down options

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>
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of hhnetworks

ASKER

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
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.
https://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

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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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)
Understood and thanks for the points!  Best regards, ~Ray