2012-11-26 25 views
0

我有這種形式複選框值:

<form action="../index_success.php" method="post" id="sendEmail" class="email"> 
      <h3 class="register2">Newsletter Signup:</h3> 
      <ul class="forms email"> 
       <li class="name"> 
        <label for="yourName">Name: </label> 
        <input type="text" name="yourName" class="info" id="yourName" value="<?php echo $_POST['yourName']; ?>" /><br /> 
       </li> 
       <li class="city"><label for="yourCity">City: </label> 
        <input type="text" name="yourCity" class="info" id="yourCity" value="<?php echo $_POST['yourCity']; ?>" /><br /> 
       </li> 
       //This is where I need help with the check box code 
       <li class="classes"><label for="classInterest">Interested in classes?: </label> 
        <input type="checkbox" name="classInterest" class="info" id="classInterest" value="Yes" /><br /> 
       </li> 
       <li class="email"> 
        <label for="emailFrom">Email: </label> 
        <input type="text" name="emailFrom" class="info" id="emailFrom" value="<?php echo $_POST['emailFrom']; ?>" /> 
        <?php if(isset($emailFromError)) echo '<span class="error">'.$emailFromError.'</span>'; 

        ?> 
       </li> 
       <li class="buttons email"> 
        <button type="submit" id="submit">Send</button> 
        <input type="hidden" name="submitted" id="submitted" value="true" /> 
       </li> 
      </ul> 
     </form> 

這是通過電子郵件發送給用戶。我不知道如何添加上方,用戶將看到一個「是」如果該複選框被選中該複選框:

<?php 
$mailTo = '[email protected]'; // This is the hardcoded Recipient Address 
$mailSubject = 'Subject'; // This is the hardcoded Subject 
$mailFrom = $_POST['emailFrom']; 
$yourName = $_POST['yourName']; 
$yourCity = $_POST['yourCity']; 
$classInterest = $_POST['classInterest']; //This is the code for the checkbox 

$mailHeader = "From: {$mailFrom}"; 
$mailBody = "Name = {$yourName} City = {$yourCity} Class interest = {$classInterest}"; 

mail($mailTo , $mailSubject , $mailBody , $mailHeader); 

基本上,我需要的是(psudocode):

if ($classInterest == "yes") { 
    $classInterest = "Interested in Classes in ".$yourCity; 
} 
else { 
... 
} 

回答

2

試試這個:

$classInterest = (isset($_POST['classInterest']) && $_POST['classInterest'] == 'Yes') ? "Interested in Classes in " . $yourCity : ''; 

編輯

http://blog.gerv.net/2006/10/firefox_reload_behaviour/閱讀文章並通過Jason的第一條評論

+1

+1使用isset。最好記住,當複選框「未設置」時,瀏覽器不會發送複選框字段。 – vstm

+0

嗯 - 即使複選框被選中,我仍然對類興趣= {$ classInterest}留空。 – Joel

+0

我不明白的另一件奇怪的事情 - 當我刷新頁面時,複選框會保持選中狀態。它應該是unchecked ... – Joel