如何使用PHP联系表格格式化多个电子邮件选择[关闭]

问题描述:

I have a question regarding the PHP code i'm using for my contact form.

I currently have this HTML code for my form:

      <div id="contact-form">

    <form method="post" action="sendmail.php">
    <div>
    Name * <input name="name" type="text" placeholder="John Doe" />
    </div>

    <div>
    Email * <input name="email" type="email" placeholder="johndoe@example.com" />
    </div>

    <div>
    Tel * <input name="tel" type="tel" />
    </div>

    <div>
    Organisation <input name="org" type="text" />
    </div>

    <div class="select">
    Requested Date<br>
      <select name="day">
                <option value="01">01</option>
                <option value="02">02</option>
                <option value="03">03</option>
                <option value="04">04</option>
                <option value="05">05</option>
                <option value="06">06</option>
                <option value="07">07</option>
                <option value="08">08</option>
                <option value="09">09</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
         </select>
         <span>/</span>
         <select name="month">
                <option value="JAN">JAN</option>
                <option value="FEB">FEB</option>
                <option value="MAR">MAR</option>
                <option value="APR">APR</option>
                <option value="MAY">MAY</option>
                <option value="JUN">JUN</option>
                <option value="JUL">JUL</option>
                <option value="AUG">AUG</option>
                <option value="SEPT">SEPT</option>
                <option value="OCT">OCT</option>
                <option value="NOV">NOV</option>
                <option value="DEC">DEC</option>
         </select>
         <span>/</span>
         <select name="year">
                <option value="2015">2015</option>
                <option value="2016">2016</option>
                <option value="2017">2017</option>
                <option value="2018">2018</option>
                <option value="2019">2019</option>
                <option value="2020">2020</option>
            </select>
    </div>

    <div>
    Duration of Day/s<br>
         <select id="time" name="time">
                <option value="1-2 Hours">1-2 Hours</option>
                <option value="2-4 Hours">2-4 Hours</option>
                <option value="5-7 Hours">5-7 Hours</option>
                <option value="7+ Hours" >7+ Hours</option>
         </select>
    </div>

    <div>
    Message *
    <textarea name="message" rows="15" cols="40"></textarea>
    </div>

    <input type="submit" value="SEND" class="button submit-button" />
    </form>

And here is the PHP code (which I got from my hosting company):

<?php
/* 
=============================================
Sendmail.php - send an email from a web form. Make sure this file is called sendmail.php
when you upload it, otherwise the example form won't find the script and will error.

NOTE: This script is heavily commented. Text after double slashes // is ignored by PHP
=============================================
*/

// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = "info@urbanedge-promotions.com";            // Specify the email address you want to send the mail to.
$email_subject = "Urban Edge Enquiry";  // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "http://www.info@urbanedge-promotions.com/thankyou.html";

// Get the details the user entered into the form
$name = $_POST["name"];
$email_from = $_POST["email"];
$tel = $_POST["tel"];
$org = $_POST["org"];
$time = $_POST["time"];
$message = $_POST["message"];

//Validate name is not empty
if(empty($name)) {
    die("<p>Please go back and enter your full name.</p>");
}

//Validate email is not empty
if(empty($email_from)) {
    die("<p>Please go back and enter your email address.</p>");
}
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    // Invalid email address
    die("<p>The email address entered is invalid. Please go back and correct it.</p>");
}

//Validate tel is not empty
if(empty($tel)) {
    die("<p>Please go back and enter your telephone number.</p>");
}

//validate message is not empty
if(empty($message)){
    die("<p>Please go back and complete the message field.</p>");
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    die("<p>Your message must be greater than 20 characters. Please go back and provide as much detail as possible regarding your enquiry.</p>");
}

// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The 
 is the code to use a new line.
$headers  = "From: " . $email_from . "
";
$headers .= "Reply-To: " . $email_from . "
";    // (You can change the reply email address here if you want to.)

// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "
Email: ". $email_from . "
Tel: ". $tel . "
Organisation: ". $org . "
Date: ". $day . "
Duration of Day: ". $time . "
Message: " . $message;

// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("sendmail_from", $email_from);

// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);

// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
    header("Location: " . $thankyou_url);   // Redirect customer to thankyou page
} else {
    // The mail didn't send, display an error.
    echo "There has been an error sending your message. Please try later.";
}

?>

I would like the date selected to appear in my email like '29 / 11 / 2014' but it is currently blank... i.e :

Name: Someones Name

Email: example@example.com

Tel: 07183737373

Organisation: Something

Date:

Duration of Day: 1-2 Hours

Message: Hi all, I'm looking for someone to help me out in my time of need! Your wisdom and knowledge is appreciated.

Thanks

You will have to access $_POST array to get values of day, month and year drop-downs and have to do following things to convert selected options to your desired date.

$date = $_POST['day']."/".date('m', strtotime($_POST['month']))."/".$_POST['year'];

Don't you know how to get the value of 'select' elements? Doesn't this work?

$_POST["day"]
$_POST["month"]
$_POST["year"]
$_POST["time"]