2016-01-07 92 views
1

我是新來的PHP,我試圖讓我的聯繫表格與<select><option>下拉菜單一起工作。與選擇選項字段不起作用的聯繫表格

接觸形式發送的所有信息的電子郵件,除了<select><option>領域

這裏是我的代碼有:

PHP代碼:

<?php 
    if (isset($_POST["submit"])) { 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $number = $_POST['number']; 
     $from = 'Ny melding sendt fra kontaktskjema'; 
     $to = '[email protected]'; 
     $subject = 'CompanyName | Dere har motatt en ny melding'; 
     $headers = array(); 
     $headers[] = "MIME-Version: 1.0"; 
     $headers[] = "Content-type: text/plain; charset=UTF-8"; 
     $headers[] = "From: Ny melding sendt fra kontaktskjema på FantasyLab.no"; 
     $headers[] = "X-Mailer: PHP/".phpversion(); 
     $headers[] = "Reply-To: $name <$email>"; 

     $body  = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n"; 

     // Check if name has been entered 
     if (!$_POST['name']) { 
      $errName = 'Vennligst skriv inn ditt navn'; 
     } 

     // Check if email has been entered and is valid 
     if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
      $errEmail = 'Vennligst skriv inn din e-post'; 
     } 

     //Check if message has been entered 
     if (!$_POST['number']) { 
      $errNumber = 'Vennligst skriv inn ditt telefonnummer'; 
     } 

     //Check if message has been entered 
     if (!$_POST['budget']) { 
      $errBudget = 'Vennligst skriv en melding'; 
     } 

     //Check if message has been entered 
     if (!$_POST['message']) { 
      $errMessage = 'Vennligst skriv en melding'; 
     } 

     (isset($_POST["budget"])) ? $budget = $_POST["budget"] : $budget=1; 

// If there are no errors, send the email 
if (!$errName && !$errEmail && !$errNumber && !$errMessage && !$errBudget) { 
    if (mail ($to, $subject, $body,implode("\r\n", $headers))) { 
     $result='Thank you for your enquiry. We will get in touch within 24 hours.'; 
     header('Location:index.php?success='.$result); 
    } else { 
     $result='We apologize, something went wrong. Let us talk over phone; +47 45 46 46 07'; 
     header('Location:index.php?fail='.$result); 
    } 
} 
    } 
?> 

HTML代碼:

<div class="form-group col-md-6 col-sm-12 col-xs-12"> 
      <div class="col-xs-12"> 
      <div class="option-styling"> 
      <label>Budget*</label>         
      <select id="budget"class="col-xs-12"> 
       <option <?php if ($budget == 1) echo 'selected' ; ?> value="1">10.000 kr - 25.000 kr</option> 
       <option <?php if ($budget == 2) echo 'selected' ; ?> value="2">25.000 kr - 50.000 kr</option> 
       <option <?php if ($budget == 3) echo 'selected' ; ?> value="3">50.000 kr +</option> 
       <option <?php if ($budget == 4) echo 'selected' ; ?> value="4">Not sure</option> 
      </select> 
      <?php echo "<p class='text-danger'>$errBudget</p>";?> 

     </div>  
      </div> 
    </div> 

任何人都可以看到我在這裏做錯了嗎?任何幫助表示讚賞,謝謝!

+0

什麼問題? – momouu

+0

用戶選擇的選定選項字段不會顯示在電子郵件中。它顯示用戶填寫的所有其他字段,除了選項字段 – NohmanJ

+0

將選擇放在表單中併爲選項添加名稱 – momouu

回答

3

請將名稱添加到下拉列表中,以獲得值。

你想要服務器端腳本的所有元素必須有名稱屬性在裏面。

在您的代碼中。

<div class="form-group col-md-6 col-sm-12 col-xs-12"> 
      <div class="col-xs-12"> 
      <div class="option-styling"> 
      <label>Budget*</label>         
      <select name="budget" id="budget"class="col-xs-12"> 
       <option <?php if ($budget == 1) echo 'selected' ; ?> value="1">10.000 kr - 25.000 kr</option> 
       <option <?php if ($budget == 2) echo 'selected' ; ?> value="2">25.000 kr - 50.000 kr</option> 
       <option <?php if ($budget == 3) echo 'selected' ; ?> value="3">50.000 kr +</option> 
       <option <?php if ($budget == 4) echo 'selected' ; ?> value="4">Not sure</option> 
      </select> 
      <?php echo "<p class='text-danger'>$errBudget</p>";?> 

     </div>  
      </div> 
    </div> 

感謝 阿米特

+0

我添加了名稱屬性,但仍然沒有在電子郵件中顯示選定的選項 – NohmanJ

+1

請添加$ budget = $ _POST ['budget'];在你的PHP腳本,然後你會得到它。不要忘記將它添加到電子郵件正文。 –

1

你的選擇沒有名稱屬性