Skip to content

Commit

Permalink
Updating form-to-email PHP script to handle selects
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Shea committed Dec 4, 2011
1 parent 1de61f9 commit 6861ada
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions PHP/form-to-email/includes/fields.php
Expand Up @@ -20,6 +20,24 @@
if ($data["type"] == "textarea") {
echo ' <textarea id="form-' . $field . '" name="' . $field . '" >' . ($_POST[$field] ? $_POST[$field] : $data["value"]) . "</textarea>\n";
}


// selects
if (($data["type"] == "select") || ($data["type"] == "dropdown")) {
echo ' <select id="form-' . $field . '" name="' . $field . '">\n';
if (isset($data["options"])) {
foreach ($data["options"] as $optionId => $optionValue) {
if (isset($data["defaultValue"]) && ($data["defaultValue"] == $optionId)) {
$selected = " selected=\"selected\"";
} else {
$selected = "";
}
echo "\t\t\t\t\t\t<option value=\"$optionId\"$selected>$optionValue</option>\n";
}
}
echo ' </select>';
}


echo ' </div>' . "\n\n";
}
Expand Down
2 changes: 1 addition & 1 deletion PHP/form-to-email/includes/handler.php
Expand Up @@ -85,7 +85,7 @@ function formCheckSelected($postName, $valueName) {
foreach($_POST as $key => $value) {
// remove form handling fields
if (($key != "sendmail") && ($key != "subject")) {
$message .= ucwords($key) . ": " . $value . "\n\n";
$message .= str_replace("-", " ", ucwords($key)) . ": " . $value . "\n\n";
}
}

Expand Down
21 changes: 21 additions & 0 deletions PHP/form-to-email/index.php
Expand Up @@ -33,10 +33,13 @@
// (can add or remove as many as needed below)
// currently only supports:
// input type=text
// select
// textarea
// shouldn't be hard to repurpose to include any type of form element
// just modify includes/fields.php
$formFields = array(

// examples of input type=text
"name" => array(
"type" => "input",
"label" => "Your name:",
Expand All @@ -58,13 +61,31 @@
"defaultValue" => $emailDefaults["subject"],
"required" => false,
),

// example of a textarea
"message" => array(
"type" => "textarea",
"label" => "Message:",
"class" => "",
"defaultValue" => "",
"required" => true,
),

// example of a select
"how-many-fingers" => array(
"type" => "select",
"label" => "How many fingers am I holding up:",
"class" => "",
"options" => array(
1 => "1",
"2",
"3",
"4",
"5",
),
"defaultValue" => 2,
"required" => true,
),
);


Expand Down

0 comments on commit 6861ada

Please sign in to comment.