2015-12-02 34 views
1

我有以下的代碼來構建一個下拉型窗體域:下拉選項沒有被選中

if ($current_user->ID) { 
    $output .= ' 
<form action="" method="POST" class="profileForm" onSubmit="return validateMobile()"> 
<div class="formField"> 
    <label for="tenant_title">Title</label> 
    <select name="tenant_title"> 
    <option value="Mr"' . titleSelected('Mr') . '> 
    Mr 
    </option> 
    <option value="Miss"' . titleSelected('Miss') . '> 
    Miss 
    </option> 
    <option value="Mrs"' . titleSelected('Mrs') . '> 
    Mrs 
    </option> 
    <option value="Ms"' . titleSelected('Ms') . '> 
    Ms 
    </option> 
    </select> 
</div> 

而這個功能,它檢查什麼是已經在數據庫:

function titleSelected($value){ 
    if ($tenant_details->tenant_title == $value) return 'selected'; 
return false; 
} 

問題是,它總是默認爲'Mr',這是第一種選擇,不管數據庫中有什麼。

我錯過了什麼?

我還是很用PHP新手,所以請溫柔... :)

+0

+0

我不能使用這個,因爲我的代碼已經在一個函數中,並以$ output的形式返回......它不是一個HTML表單。 –

+0

那麼這不是你原來的文章,你是否設法解決你的問題? – Naruto

回答

0

您的代碼存在的問題是您無法比較$tenant_details->tenant_title t一個值,因爲$tenant_details被請求在你的函數之外。要了解更多有關變量的作用域退房PHP variable scope

有2種方式解決它:

  1. 你只是傳遞對象到你的函數:

    titleSelected($tenant_details, 'Ms'); 
    
    function titleSelected($tenant_details->tenant_title, $val2){ 
        if ($tenant_details->tenant_title == $val) { 
         return 'selected'; 
        } 
        return ""; 
    } 
    
  2. 只要傳遞價值單獨和比較:

    titleSelected($tenant_details->tenant_title, 'Ms'); 
    
    function titleSelected($val1, $val2){ 
        if ($val1 == $val2) { 
         return 'selected'; 
        } 
        return ""; 
    } 
    
0

而不是selected你應該返回selected="selected",像這樣:

function titleSelected($value){ 
    if ($tenant_details->tenant_title == $value) return 'selected="selected"'; 
    return false; 
} 


<div class="formField"> 
    <label for="tenant_title">Title</label> 
    <select name="tenant_title"> 
    <option value="Mr" <?php echo titleSelected('Mr'); ?>> 
    Mr 
    </option> 
    <option value="Miss" <?php echo titleSelected('Miss'); ?>> 
    Miss 
    </option> 
    <option value="Mrs" <?php echo titleSelected('Mrs'); ?>> 
    Mrs 
    </option> 
    <option value="Ms" <?php echo titleSelected('Ms'); ?>> 
    Ms 
    </option> 
    </select> 
</div> 

編輯:

function titleSelected($value){ 
    if ($tenant_details->tenant_title == $value) return 'selected'; 
    return false; 
} 

function some_function(){ 

    // your code 

    $output = ''; // initialize $output somewhere outside of if block 

    if ($current_user->ID){ 
     $output .= ' 
     <form action="" method="POST" class="profileForm" onSubmit="return validateMobile()"> 
      <div class="formField"> 
       <label for="tenant_title">Title</label> 
       <select name="tenant_title"> 
       <option value="Mr"' . titleSelected('Mr') . '> 
       Mr 
       </option> 
       <option value="Miss"' . titleSelected('Miss') . '> 
       Miss 
       </option> 
       <option value="Mrs"' . titleSelected('Mrs') . '> 
       Mrs 
       </option> 
       <option value="Ms"' . titleSelected('Ms') . '> 
       Ms 
       </option> 
       </select> 
      </div> 
     </form>'; 
    } 
    return $output; 
} 

echo some_function(); 
+0

我個人不會返回false,而是false作爲false有時可以評估爲0 –

+0

@SamSwift true,但是這取決於OP在這種情況下如何處理false。 –

+0

它沒有區別。 HTML5將接受'selected'(它已經在使用不同的功能)。 –