2013-12-09 25 views
1

我想弄清楚爲什麼當我提交這個表單過濾器時,我的if/else if語句不起作用。的print_r($後);返回正確的過濾器值,但是$ printmain只顯示'rehab',不管選擇了哪個過濾器選項 - 這很奇怪,因爲$ post變量明顯在變化。所以我不明白爲什麼它不通過if/else if語句?如果/ else if語句不適用於PHP窗體過濾器

if(isset($_POST['filter'])) { 

    $post = $_POST['filter']; 

     print_r($post); 

     if ($post = 'teamrehab') {$printmain = 'rehab';} 
     else if ($post = 'heights') {$printmain = 'heights';} 
     else if ($post = '1225') {$printmain = '1225';} 

    } 

<html> 
    <form method="post" id="filter" action="<?= $_SERVER['PHP_SELF'];?>"> 
    <select name="filter" onchange="document.getElementById('filter').submit();"> 
    <option value="choose">Choose Client</option> 
    <option value="teamrehab">teamrehab</option> 
    <option value="heights">heights</option> 
    <option value="1225">1225 Old Town</option> 
    </select> 
    </form> 

<?php if(!empty($_SESSION['username'])){ echo $printmain;} 
else echo "Please login with your Twitter account.";?> 


</html> 
+1

你比較運算'= '是分配一個不比較'=='的值 – hammus

回答

3

您在if語句中使用的是=而不是==

變化如下:

if ($post = 'teamrehab') {$printmain = 'rehab';} 
     else if ($post = 'heights') {$printmain = 'heights';} 
     else if ($post = '1225') {$printmain = '1225';} 

到:

if ($post == 'teamrehab') {$printmain = 'rehab';} 
     else if ($post == 'heights') {$printmain = 'heights';} 
     else if ($post == '1225') {$printmain = '1225';} 

你需要知道這是=之間的差異的assignment operator==這是一個comparison operator