2017-04-15 59 views
0

我正在創建一個php窗體,我按照正常的方式處理複選框 但是,但在此代碼它總是打印不,最新的問題是什麼?處理checbox在聯繫表格

<form method="post" action="php/form.php"> 
    <div class="form-check pull-left"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value="yes" name="check1"> 
    Option one is this and that&mdash;be sure to include why it's great 
    </label> 
</div> 
</form> 

if (isset($_POST['check1'])) { 
    $check1 = "yes"; 
} else { 
    $check1 = "no"; 
} 
$body="check1: $check1"; 
mail('[email protected]', $subject, $body) 

我總是收到郵件沒有

+0

請試試看:if(isset($ _ POST ['check1'])&& $ _POST ['check1'] =='yes') – Scaffold

+0

發表完整的表單代碼。 –

+0

@Scaffold的第一個條件是已經返回false,那麼條件如何可以通過添加一個AND條件來實現? –

回答

0

當第一次加載頁面,還沒有公佈,因此$_POST是空的,$_POST['check1'] == NULL

根據您的邏輯,您在此情況下將check1設置爲no,併發送電子郵件(即使沒有發佈任何內容)。

你應該保證的東西已經發布,它可以通過添加submit按鈕將表格

<button type="submit" name="submit">Submit POST</button> 

併爲$_POST['submit']

if (isset($_POST['submit'])) 

的生存確認所有一起做:

<form method="post"> 
    <div class="form-check pull-left"> 
     <label class="form-check-label"> 
      <input class="form-check-input" type="checkbox" value="yes" name="check1"> 
      Option one is this and that&mdash;be sure to include why it's great 
     </label> 
     <button type="submit" name="submit">Submit POST</button> 
    </div> 
</form> 

<?php 

if (isset($_POST['submit'])) { 
    if (isset($_POST['check1'])) { 
     $check1 = "yes"; 
    } else { 
     $check1 = "no"; 
    } 
    $body = "check1: $check1"; 
    mail('[email protected]', $subject, $body) 
} 

這樣,一個ema il只會在用戶實際提交表單時發送,而不是每次頁面加載時發送。

0

這是正常的,如果你沒有提交輸入做。 你用你的郵件函數來檢查$ _POST是否爲空。

試試這個:

<form method="post"> 
    <div class="form-check pull-left"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value="yes" name="check1" checked> 
    Option one is this and that&mdash;be sure to include why it's great 
    </label> 
</div> 
<input type="submit" value="ok"> 
</form> 
<?php 

if (isset ($_POST) && !empty ($_POST)) 
{ 
    if (isset($_POST['check1'])) { 
    $check1 = "yes"; 
    } else { 
    $check1 = "no"; 
    } 
    $body="check1: $check1"; 
    mail('[email protected]', $subject, $body) 
}