How to add a parameter to the URL?

后端 未结 9 965
遇见更好的自我
遇见更好的自我 2020-11-30 06:37

My current URL is: http://something.com/mobiles.php?brand=samsung

Now when a user clicks on a minimum price filter (say 300), I want my URL to become

相关标签:
9条回答
  • 2020-11-30 07:18

    try something like this, it should consider also cases when you already have that param in url:

    function addOrUpdateUrlParam(name, value)
    {
      var href = window.location.href;
      var regex = new RegExp("[&\\?]" + name + "=");
      if(regex.test(href))
      {
        regex = new RegExp("([&\\?])" + name + "=\\d+");
        window.location.href = href.replace(regex, "$1" + name + "=" + value);
      }
      else
      {
        if(href.indexOf("?") > -1)
          window.location.href = href + "&" + name + "=" + value;
        else
          window.location.href = href + "?" + name + "=" + value;
      }
    }
    

    then you invoke it like in your case:

    addOrUpdateUrlParam('priceMin', 300);
    
    0 讨论(0)
  • 2020-11-30 07:18
    <html>
    <body>
    ..
    ..
    ..
    
    <?php
    $priceMinValue= addslashes ( $_GET['priceMin']);
    if (!empty($priceMin)) {
            $link = "currentpage.php?priceMin=". $priceMinValue;
            die("<script>location.href = '".$link. "'</script>");    
    }
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 07:21
    if(location.search === "") {
        location.href = location.href + "?priceMin=300";
    } else {
        location.href = location.href + "&priceMin=300";
    }
    

    In case location.search === "", then there is no ? part.

    So add ?newpart so that it becomes .php?newpart.

    Otherwise there is a ? part already.

    So add &newpart so that it becomes .php?existingpart&newpart.


    Thanks to hakre, you can also simply set it like:

    location.search += "&newpart";
    

    It will automatically add ? if necessary (if not apparent, it will make it ?&newpart this way, but that should not matter).

    0 讨论(0)
  • 2020-11-30 07:25

    use var urlString = window.location to get the url

    check if the url already contains a '?' with urlString.indexOf('?'), -1 means it doesnt exist.

    set window.location to redirect

    this is like 101 of javascript. try some search engines!

    0 讨论(0)
  • 2020-11-30 07:26

    I rewrite the correct answer in PHP:

    function addOrUpdateUrlParam($name, $value){
    $href = $_SERVER['REQUEST_URI'];
    $regex = '/[&\\?]' . $name . "=/";
    if(preg_match($regex, $href)){
        $regex = '([&\\?])'.$name.'=\\d+';
        $link = preg_replace($regex, "$1" . $name . "=" . $value, $href);
    }else{
        if(strpos($href, '?')!=false){
            $link = $href . "&" . $name . "=" . $value;
        }else{
            $link = $href . "?" . $name . "=" . $value;
        }
    }
    return $link;
    }
    

    I hope that help's someone...

    0 讨论(0)
  • 2020-11-30 07:26
    <FORM action="" method="get">
    <P>
    <LABEL for="brand">Brand: </LABEL>
              <INPUT type="text" id="brand"><BR>
    <LABEL for="priceMin">Minimum Price: </LABEL>
              <INPUT type="text" id="priceMin"><BR>
    </P>
    

    0 讨论(0)
提交回复
热议问题