我可以使用PHP将数据从选择下拉列表发送到电子邮件吗?

时间:2014-02-17 19:13:02

标签: php forms

我正在构建一个带有动作到php文件的html表单。 在表单中,我有一些下拉菜单,用户需要在其中选择其中一个值。我需要检索该值并将其发布到电子邮件中。我几乎不知道什么是PHP,无法在任何地方找到应该如何做到这一点。我在网上找到的所有内容都与mysql或某些数据库结合使用,但我不在这里使用数据库。

这可能吗?

谢谢你们的回复,但说实话,我遇到了太多问题。我一直在寻找教程和演示,但我似乎无法让它正常工作。经过这么多个小时,我真的需要一些帮助

我的HTML是

<form method="post" name="contact_form" action="contact.php">                       
<div class="form-div1">
                        <label for="profile" class="label1">Select your profile</label>
                        <br>
                        <select id="user-type" class="selectlist">
                            <option value="option1">I'm a first-time user</option>
                            <option value="option2">I would like to renew or upgrade</option>
                        </select>
                    </div>

                    <div class="form-div2">
                        <label for="SEN" class="label2">SEN</label>
                        <br>
                        <input type "text" name="input1" class="input1">
                    </div>

                    <div class="form-div3">
                        <label for="email" class="label3">Email Address</label>
                        <br>
                        <input type "text" name="input2" class="input2">
                    </div>

                    <div class="form-div4">
                        <label for="product_choice" class="label4" name="select_menu">Select your product</label>
                        <br>
                        <select id="product" class="selectlist">
                            <option value="option1">JIRA</option>
                            <option value="option2">Confluence</option>
                            <option value="option3">JIRA Service Desk</option>
                            <option value="option15">Other</option>
                        </select>
                    </div>

                    <div class="form-div42">
                        <label for="product" class="label42">Specify your product</label>
                        <br>
                        <input type "text" name="input2" class="input2">
                    </div>

                    <div class="form-div5">
                        <label for="license_choice" class="label5">Select your license</label>
                        <br>
                         <select id="select" class="selectlist">
                            <option value="option1">25 users</option>
                            <option value="option2">50 users</option>
                            <option value="option3">100 users</option>

                        </select>
                    </div>

                    <div class="input_box_atlassian">
                        <input type="submit" name="submit" value="Submit" class="submit-button-atl" />
                    </div>
                </form>

我的php读起来像这样

if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}
$profile = $_POST['profile'];
$SEN = $_POST['SEN'];
$email = $_POST['email'];
$product_choice = $_POST['product_choice'];
$product = $_POST['product'];
$license_choice = $_POST['license_choice'];

//Validate first
if(empty($SEN)||empty($email)) 
{
    echo "SEN and email are mandatory!";
    exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'test@gmail.com';//<== update the email address
$email_subject = "Atlassian";
$email_body = "You have received a new message from the user $email.\n".
    "Here is the message:\n $message".

$to = "test@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

我确信我做了一百万件错事,但我对编程知之甚少,才意识到它是什么。我确实想学习,但我需要帮助才能让我走上正确的轨道。

顺便说一下,也有一些javascript,当用户选择第一个选择框中的两个选项之一时,输入字段(div2和div3)出现或消失。我认为这不一定是php的问题吗?

2 个答案:

答案 0 :(得分:0)

您可以使用PHP的内置mail功能来完成此任务。

http://us1.php.net/manual/en/function.mail.php

您无需先将信息写入数据库或使用它执行其他操作。您可以将变量直接传递给PHP的mail函数。

<?php

// IF YOU HAVE A SELECT MENU NAMED select_menu, 
// YOU CAN PULL THE VALUE OUT AND STORE IT INTO A VARIABLE
$select_menu_val = $_POST['select_menu'];


// THEN YOU CAN PASS THAT ALONG TO THE MAIL FUNCTION
$to = 'rick@rick.com';
$subject = 'This is a test';
$message = 'My select menu value is '.$select_menu_val;

mail($to, $subject, $message);

答案 1 :(得分:0)

你的方法不正确,你必须设置所有元素的名称,我已经实现了你的代码,并且可以正常工作

<?php
if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}
$profile = $_POST['profile'];
$SEN = $_POST['SEN'];
$email = $_POST['email'];
$product = $_POST['product'];
$product_choice = $_POST['product_choice'];
$product = $_POST['product'];
$license_choice = $_POST['license_choice'];

//Validate first
if(empty($SEN)||empty($email)) 
{
    echo "SEN and email are mandatory!";
    exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'test@gmail.com';//<== update the email address
$email_subject = "Atlassian";
$email_body = "You have received a new message from the user $email.\n".
    "Here is the message:\n $message".

$to = "test@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<form method="post" name="contact_form" action="contact.php">                       
<div class="form-div1">
                        <label for="profile" class="label1">Select your profile</label>
                        <br>
                        <select id="user-type" name="profile" class="selectlist">
                            <option value="option1">I'm a first-time user</option>
                            <option value="option2">I would like to renew or upgrade</option>
                        </select>
                    </div>

                    <div class="form-div2">
                        <label for="SEN" class="label2">SEN</label>
                        <br>
                        <input type "text" name="sen">
                    </div>

                    <div class="form-div3">
                        <label for="email" class="label3">Email Address</label>
                        <br>
                        <input type "text" name="input2" class="input2">
                    </div>

                    <div class="form-div4">
                        <label for="product_choice" class="label4" name="select_menu">Select your product</label>
                        <br>
                        <select id="product" name="product"class="selectlist">
                            <option value="option1">JIRA</option>
                            <option value="option2">Confluence</option>
                            <option value="option3">JIRA Service Desk</option>
                            <option value="option15">Other</option>
                        </select>
                    </div>

                    <div class="form-div42">
                        <label for="product" class="label42">Specify your product</label>
                        <br>
                        <input type "text" name="input2" class="input2">
                    </div>

                    <div class="form-div5">
                        <label for="license_choice" class="label5">Select your license</label>
                        <br>
                         <select id="select" name="license_choice" class="selectlist">
                            <option value="option1">25 users</option>
                            <option value="option2">50 users</option>
                            <option value="option3">100 users</option>

                        </select>
                    </div>

                    <div class="input_box_atlassian">
                        <input type="submit" name="submit" value="Submit" class="submit-button-atl" />
                    </div>
                </form>