Can i post data from a select drop down to email u

2019-08-27 22:13发布

问题:

I'm currently building an html form with an action to a php-file. In the form i have some drop down menus in which the user needs to select one of the values. I need to retrieve that value and post it to an email. I know almost nothing of php and cannot find anywhere how this should be done. All i find online is doing this in combination with mysql or some database but i'm not working with databases here.

Is this possible?

thank you for the replies, but honestly i'm having too much problems getting it to work. I've been looking around for tutorials and demos but i just can't seem to get it to work properly. After so many hours i really need some help please

my html is

<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>

my php reads like this

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;
  }
}

I'm sure i'm doing a million things wrong but i have too little knowledge of programming to realize what it is. I do want to learn however but i just need help to get me on the right track.

By the way, there is some javascript involved too, when a user selects one of the two options in the first select box, the input fields (div2 and div3) appear or disappear. I assume that wouldn't have to be a problem for php?

回答1:

You can use PHP's built-in mail function to accomplish this.

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

You do not need to first write the information to a database or do something else with it. You can pass the variables directly to PHP's mail function.

<?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);


回答2:

Your are not on proper way, you have to set names of all the elements, I have implemented your code, and will work fine

<?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>


标签: php forms