2011-09-04 54 views
0

我有一個非常簡單的形式,我希望用戶選擇是否升序或降序排序。從選擇表單中,我將使用答案按照所需順序提供搜索結果。我的問題是表單沒有給出頁面的結果,並且'if'語句都滿足。我完全難倒了。任何一個人都可以點亮?謝謝PHP表格結果到同一頁

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> 
<label for="sort">Sort by:</label> 
<select name="thesort"> 
<option value="Lowest">Lowest first</option> 
<option value="Highest">Highest first</option> 
</select> 
</form> 


<?php 
if(isset($_POST["thesort"])){ 
echo "selection has been made"; 
} 
?> 

<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';} 
{ 

    echo "<p> choice is DESC </p>"; 


} 
?> 


<?php if($_POST["thesort"]=="Lowest"){ echo 'selected="selected"';} 
{ 
    echo "<p> choice is ASC </p>"; 


?> 
+0

您是否啓用了錯誤報告?也可以通過簡化邏輯來嘗試簡化問題,直到獲得預期的輸出,然後逐個添加代碼。 –

+2

請勿使用POST進行排序。改用GET。 –

回答

2

爲什麼雙曲花括號?在這種情況下,PHP將執行第二個。

if($_POST["thesort"]=="Highest") 
{ echo 'selected="selected"';} 
{echo "<p> choice is DESC </p>";} 

您的代碼修改:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> 
<label for="sort">Sort by:</label> 
<select name="thesort"> 
<option value="Lowest">Lowest first</option> 
<option value="Highest">Highest first</option> 
</select> 
</form> 

<?php 
if(isset($_POST["thesort"])){ 
echo "selection has been made"; 
} 

if($_POST["thesort"]=="Highest"){ 
echo 'selected="selected"'; 
echo "<p> choice is DESC </p>"; 
} 

if($_POST["thesort"]=="Lowest"){ 
echo 'selected="selected"'; 
echo "<p> choice is ASC </p>"; 
} 
?> 
+0

'echo'selected =「selected」';'可能放錯了位置,但這不是這個答案的領域。 –

0

這是一個問題:

<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';} 
{ 
    echo "<p> choice is DESC </p>"; 
} 
?> 

一)這些括號沒有做什麼,我想你想他們在做。特別是第二套是無關緊要的;該代碼將總是執行

b)爲什麼你在'這裏'選擇'=''?這不是在打開<option標籤的情況下。例如,你可能想是這樣的:

echo '<option value="Highest'; 

if ($_POST["thesort"]=="Highest") 
{ 
    echo ' selected="selected"'; 
} 

echo '">Highest first</option>'; 
0
<?php 
$sort = 'Lowest'; // define the default 
if (isset($_POST['thesort']) && $_POST['thesort'] == 'Highest') { 
    $sort = 'Highest'; 
} 
?> 

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> 
    <label for="sort">Sort by:</label> 
    <select name="thesort"> 
    <option value="Lowest"<?php if ($sort == 'Lowest') print(' selected="selected"'); ?>>Lowest first</option> 
    <option value="Highest"<?php if ($sort == 'Highest') print(' selected="selected"'); ?>>Highest first</option> 
    </select> 
</form> 
+1

爲什麼在地球上你都使用POST進行排序? –

+0

@Col。彈片:因爲它可能在OP的問題中? – PeeHaa

+0

難道你沒有自己的想法嗎? –