2015-04-26 65 views
0

登錄後,將數據傳遞我想在頁面上對項目進行排序,併爲我使用這種形式:形式已被處理窗體停止用戶已經

<form method="get" action=""> 
      <label for="sort">Sort items by</label> 
      <select id="sort" name="sort"> 
       <option value="">-</option> 
       <option value="plth">Price (Low to high)</option> 
       <option value="phtl">Price (High to low)</option> 
       <option value="naz">Name (A-Z)</option> 
       <option value="nza">Name (Z-A)</option> 
      </select> 
      <input type="submit" name="sortSubmit" value="GO >"> 
     </form> 

後,我會用一些SQL語句從數據庫中檢索所需順序中的項目:

if(isset($_GET["sortSubmit"]) && $_GET["sort"] == "plth") { 
        $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY PRICE ASC"; 
       } 
       elseif (isset($_GET["sortSubmit"]) && $_GET["sort"] == "phtl") { 
        $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY PRICE DESC"; 
       } 
       elseif (isset($_GET["sortSubmit"]) && $_GET["sort"] == "naz") { 
        $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY NAME ASC"; 
       } 
       elseif (isset($_GET["sortSubmit"]) && $_GET["sort"] == "nza") { 
        $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY NAME DESC"; 
       } 

的問題是,一切工作正常,直到一個用戶登錄的用戶登錄後,它停止工作完全和我想不通爲什麼。通過一些測試回聲聲明,我發現表單不再傳遞任何數據。我無法想象這是如何做會議或cookie的東西。 感謝您的任何想法。

+0

我不確定我是否理解這個想法,但不是簡單的,如果你設置表單動作到index.php和index.php的邏輯來查找你是否選擇了排序動作然後按要求的順序再次生成頁面? –

+0

看看我的回答,可能會明確我的想法 –

回答

0
<form method="get" action=""> 
    <label for="sort">Sort items by</label> 
    <select id="sort" name="sort"> 
     <option value="">-</option> 
     <option value="plth">Price (Low to high)</option> 
     <option value="phtl">Price (High to low)</option> 
     <option value="naz">Name (A-Z)</option> 
     <option value="nza">Name (Z-A)</option> 
    </select> 
    <input type="submit" value="go"> 
    </form> 

現在,如果這個頁面被稱爲sort.php te表單也會提交給sort.php。

這樣,在標題中你可以讀取像這樣的值。

$sort = $_GET['sort']; 
switch ($sort){ 
case "plth": 
$retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY PRICE ASC"; 
break; 
// Other possible value andling here 
0
... 
The ordered data 
... 
<form method="get" action="index.php"> 
    <input type hidden name="manager_action" value="sort"> 
      <label for="sort">Sort items by</label> 
      <select id="sort" name="sort"> 
       <option value="">-</option> 
       <option value="plth">Price (Low to high)</option> 
       <option value="phtl">Price (High to low)</option> 
       <option value="naz">Name (A-Z)</option> 
       <option value="nza">Name (Z-A)</option> 
      </select> 
</form> 

<?php // index.php 
... 
if($_POST['manager_action']=='sort'){ 
    // regenerate data in right ordered 
    // and again generate the html page 
} 
... 

?> 
0
<form method="get" action=""> 
    <label for="sort">Sort items by</label> 
    <select id="sort" name="sort"> 
     <option value="">-</option> 
     <option value="plth">Price (Low to high)</option> 
     <option value="phtl">Price (High to low)</option> 
     <option value="naz">Name (A-Z)</option> 
     <option value="nza">Name (Z-A)</option> 
    </select> 

    <input type="submit" name="sortSubmit" value="go"> 
</form> 

注意被交換出去。這將提交表單並告訴它添加www.yourwebsite.com?sort=nza(例如)

然後在您的PHP中,您可以正確使用if/else或創建switch語句。我將使用switch語句,因爲它比一堆if/else更快一些。

<?php 
    if(isset($_GET["sortSubmit"])) { // Ask whether the form was submitted or not. Not technically required though. 
     switch($_GET["sort"]) { 
      case "plth": 
       $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY PRICE ASC"; 
       break; 
      case "phtl": 
       $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY PRICE DESC"; 
       break; 
      case "naz": 
       $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY NAME ASC"; 
       break; 
      case "nza": 
       $retrieve = "SELECT * FROM products WHERE ID <= '9' ORDER BY NAME DESC"; 
       break; 
      default: 
       // Catch anything else 
       break; 
     } 
    } 
?>