2017-01-05 112 views
0

如何在GET表單上更改我的URL。我的形式看起來像這樣更改GET表單上的URL參數

<form method="GET"> 
    <input type="text" name="brand"> 
    <input type="text" name="model"> 
    <input type="text" name="condition"> 
    <input type="text" name="transmission"> 
    <input type="submit" name="search_car" value="SEARCH> 
</form> 

my url would look like this 

http://example.com/car-listings.php?make=Toyota&model=Vios&condition=New&transmission=Manual&search_car=SEARCH 

我想縮短我的網址參數,並改變它像這樣

http://example.com/car-listings.php?make=Toyota&model=Vios 

有反正我可以修改GET方法我paraments?

+0

使用POST而不是拿! –

+0

這是一頁上的搜索頁面sir POST不會工作它不會更改URL參數 –

+0

爲什麼要縮短它?你還期望得到其他參數,還是你想完全刪除它們? – Sean

回答

0

我建議你採取下列措施:

  1. 讓不加修飾正常提交表單。
  2. 在您的php代碼中,過濾_GET數組以獲取非空參數。
  3. 基於所得到的陣列上的分頁網址,從第2步

例子:

<form method="GET"> 
    <input type="text" name="brand"> 
    <input type="text" name="model"> 
    <input type="text" name="condition"> 
    <input type="text" name="transmission"> 
    <input type="submit" value="SEARCH"> 
</form> 

<?php 

// Function to check if the $var value is empty or not. 
function not_empty($var) 
{ 
    return !empty($var) && !is_null($var) && isset($var); 
} 

// Step 1 
if(!empty($_GET) && isset($_GET)){ 
    // Step 2, apply not_empty function with array_filter for each GET parameter, to get the non-empty list. 
    $parameters = array_filter($_GET, 'not_empty'); 

    if(!empty($parameters) && isset($parameters)){ 
     // Step 3, Generate the pagination URLs 
     $pagination_query = ''; 
     $i = 0; 
     foreach ($parameters as $key => $value) { 
      if($i == 0){ 
       $pagination_query .= "$key=$value"; 
      }else{ 
       $pagination_query .= "&$key=$value"; 
      } 
      $i++; 

     } 

     echo "example.com/car-listings.php?".$pagination_query."&page=2"; 
    } 
}