2013-01-19 52 views
1

希望有人能幫我解決這個問題..我有一個表格來爲用戶選擇需要的類別。我使用此表單的複選框讓他們選擇類別。當他們選擇類別時,我在SESSION上保存所選的類別,並在該類別頁面之後重定向到下一頁。防止瀏覽器錯誤再次轉到上一頁

這個過程是這樣的......

if ($totalCategory >= 1 && $totalCategory <= 10) { 

    $_SESSION['category'] = $_POST['category']; 

    $url = BASE_URI . 'select_subject.php'; // Define the URL:  
      header("Location: $url"); 
      exit(); // Quit the script. 
} 

我的問題是,當頁面重定向到下一個頁面(select_subject.php)有時候用戶可能需要返回到分類頁面再次使用後退按鈕在瀏覽器上更改他們選擇的類別。但是當它發生時,它不會再次進入類別頁面,瀏覽器顯示類似這樣的錯誤...

文檔已過期,該文檔已不再可用。

注意:但是當我點擊頁面重新載入按鈕頁面顯示正常。

在這種情況下,我需要從別人幫助從這避免和我需要用正確顯示先前選定的類別再次顯示類別頁面..

UPDETE CODE:

<?php 

if (isset($_GET['submitted_category'])) { 

    if (isset ($_GET['category']) && is_array($_GET['category'])) { 

     $_SESSION['selectedcatcount'] = count($_GET['category']); 
     $totalCategory = count($_GET['category']); 

     if ($totalCategory >= 1 && $totalCategory <= 10) { 

      $_SESSION['category'] = $_GET['category'];    
      $url = BASE_URI . 'select_subject.php'; // Define the URL:  
        header("Location: $url"); 
        exit(); // Quit the script.   
     } else {   
      echo '<div class="error"> 
        <img src="images/error.png" /> 
        <p style="margin:2px 0 0;">Please select atleast 1, not more than 10 categories.</p> 
        </div>';   }   
    } else {     
     echo '<div class="error"> 
       <img src="images/error.png" /> 
       <p style="margin:2px 0 0;">This can not be empty. Please select atleast one category.</p> 
       </div>'; 
    } 
} 

$q = 'SELECT * FROM category ORDER BY category_id'; 
$r = mysqli_query($dbc, $q); 

$c = 0; 
$i = 0; 

echo '<form action="" method="get" accept-charset="utf-8">'; 
echo '<table><tr>'; 

    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC )){ 

     // if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row: 
     if(($c % 2) == 0 && $c != 0){ 
      echo "</tr><tr>"; 
     } 
     echo '<td width="50%"> 
       <input type="checkbox" name="category[]" value="' . $row['category_id'] . '" />' . $row['category_name'] . 
      '</td>'; 
    $c++; 

    } // while.. 
     // in case you need to fill a last empty cell: 
     if (($i % 2) != 0){ 
     // str_repeat() will be handy when you want more than 2 columns 
      echo str_repeat("<td>&nbsp;</td>", (2 - ($i % 2))); 
     } 
    echo "</tr></table>"; 
?> 
         </div>     
         <div class="continue"> 
          <p>Please click 'Continue' button to proceed to next step</p> 
          <input type="submit" value="Continue" class="continue-btn" /> 
          <input type="hidden" name="submitted_category" value="TRUE" /> 
         </div> 
         </form> 

希望有人幫助我.. 謝謝..

+0

任何人都可以幫助我解決這個問題..謝謝。 – TNK

+1

可能解決[這裏](http://stackoverflow.com/questions/660329/prevent-back-button-from-showing-post-confirmation-alert) – Niloct

回答

1

如果您使用$ _GET方法,您將不會面臨此問題,請更改您的窗體方法以獲取並嘗試它。

+0

我使用$ _GET方法..然後頁面類別頁面正在加載,但與一個錯誤消息,我在表單驗證分類頁面中使用.. – TNK

+0

你可以告訴我,你的代碼和錯誤,以便我可以得到更好的主意。對於我來說,對你的問題知之甚少是非常困難的。 – fkabeer

+0

我更新了我的代碼...請檢查 – TNK

1

使用此代碼....

if ($totalCategory >= 1 && $totalCategory <= 10) { 

    //Replace the following line 
    $_SESSION['category'] = $_POST['category']; 

    //with the line below 
    $_SESSION['category'] = $_GET['category']; 

    $url = BASE_URI . 'select_subject.php'; // Define the URL: e fio 

     header("Location: $url"); 
     exit(); // Quit the script. 
} 

感謝分享你的代碼,但你忘了告訴我哪個錯誤觸發。讓我們仔細看看它。你有沒有declaired輸入正確意味着

<input type="checkbox" name="category[]" value="...."> 
<input type="checkbox" name="category[]" value="...."> 
<input type="checkbox" name="category[]" value="...."> 

當然提交

<input type="submit" name="submitted_category" value="Submit Categories"> 

和您的網址應該是這樣的提交按鈕後按下,您重定向之前。

select-category.php?category[]=2&category[]=3&category[]=7.......... 

仍然很多取決於你得到的錯誤。