联系表单在电子邮件中缺少字段

时间:2014-08-07 10:20:08

标签: php html forms

我在从联系表单中发送两个变量时遇到问题。 当表单提交时,我得到除了这些空的所有字段 搜索类型: 日期。  我想我可能错过了一些东西,却看不出有什么人有什么想法?我也使用javascript创建级联下拉效果,以便日期取决于所选的课程。

<label for="training">What kind of training?</label>
<select id="searchType" class="fixed-size-drop">
    <option value="sessions">Introduction to Marketing</option>
    <option value="files">Social Media Workshop</option>
    <option value="clients">Advanced Marketing Workshop</option>
    <option value="firstaid">First Aid Training</option>
</select>

<label for="date">Choose your Date:</label>
<select id="sessions" class="fixed-size-drop2">
    <option value="020914">2nd sept 2014</option>
    <option value="18_sept_14">18th sept 2014</option>
</select>

<select id="files" class="fixed-size-drop2">
    <option value="11_sept_14">11th sept 2014</option>
    <option value="18_sept_14">18th sept 2014</option>
</select>

<select id="clients" class="fixed-size-drop2">
    <option value="9_sept_14">9th sept 2014</option>
    <option value="25_sept_14">25th sept 2014</option>
</select>

<select id="firstaid" class="fixed-size-drop2">
    <option value="03_sept_14">3rd September 2014</option>
    <option value="16_sept_14">16th September 2014</option>
</select>
</div>

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){

    // all your form processing code goes in here, including the mail() statement.
    $EmailFrom = "$name";
    $EmailTo = "email@email.co.uk";
    $Subject = "Online contact form";
    $name = trim(stripslashes($_POST['name'])); 
    $companyname = trim(stripslashes($_POST['companyname'])); 
    $position = trim(stripslashes($_POST['position'])); 
    $email = trim(stripslashes($_POST['email']));
    $phone = trim(stripslashes($_POST['phone']));
$searchType = trim(stripslashes($_POST['searchType']));
    $sessions = trim(stripslashes($_POST['sessions'])); 
    $newsletter = trim(stripslashes($_POST['newsletter'])); 

    // prepare email body text
    $Body = "";
    $Body .= "name: ";
    $Body .= $name;
    $Body .= "\n";
    $Body .= "companyname: ";
    $Body .= $companyname;
    $Body .= "\n";
    $Body .= "position: ";
    $Body .= $position;
    $Body .= "\n";
    $Body .= "email: ";
    $Body .= $email;
    $Body .= "\n";
    $Body .= "phone: ";
    $Body .= $phone;
    $Body .= "\n";
$Body .= "searchType: ";
    $Body .= $searchType;
    $Body .= "\n";
$Body .= "sessions: ";
    $Body .= $sessions;
    $Body .= "\n";
    $Body .= "newsletter: ";
    $Body .= $newsletter;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.domain.co.uk/thankyou.html\">";
    }

    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.domain.co.uk/error.html\">";
    }
}
?>

2 个答案:

答案 0 :(得分:3)

您需要在name元素上添加select属性。这是在$_POST数组中定义名称的属性。

e.g。

<select id="sessions" class="fixed-size-drop2" name="sessions">

答案 1 :(得分:1)

您的字段应具有PHP的名称属性以接收它们您的select元素缺少name属性:

<select id="searchType" class="fixed-size-drop">

应该是

<select id="searchType" name="searchType" class="fixed-size-drop">