skip to Main Content

I need an advice regarding URL parameters. Sometimes when I click a button which switches the page from page 1 to page 2, it removes some GET parameters values.

Here is an example for an <a> tag:
<a href="/forms/page_name/dashboard.php?page=page1&pg=<?=$_GET['pg']?>&type=fa&emp=<?=$_GET['emp']"> Name of Link </a>

when I click that link, &pg and other parameters are now empty except the dashboard.php. So I manually code it through javascript just to do it easier like this:

$('.leave_info').each(function(index, element){
      $(element).dblclick(function(e){
          let pg = $(this).val()
          let page = $(this).data('page')
          let type = $(this).data('type')
          let emp = $(this).data('emp')
          let super_id = $(this).data('id')

          window.location.href ="/forms/leaver/dashboard.php?page=page1&type="+type+"&pg="+pg+"&emp="+emp
     })
})

Thanks for the help…

2

Answers


  1. If you are asking how to construct a new URL keeping (most of) the GET parameters in a better way, you can use URL and URLSearchParams objects.

    // parse the current GET parameters
    const params = new URLSearchParams(window.location.search)
    // modify the parameters, if needed, using `.append`, `.delete` and `.set`
    params.set("newkey", "newval")
    // construct the new URL
    const newurl = new URL("/forms/leaver/dashboard.php?" + params, window.location)
    // if you need it as string, just toString it, or use `.href`:
    console.log(newurl.href)

    (Obviously, the stack snippet won’t originally have any GET arguments, so you can’t see how they are preserved by running it in here.)

    If you just want to redirect to the same page by modifying some of the params, this is even easier (don’t actually run this here, as it will try to redirect the snippet):

    // parse the current GET parameters
    const params = new URLSearchParams(window.location.search)
    // modify the parameters, if needed, using `.append`, `.delete` and `.set`
    params.set("newkey", "newval")
    // replace the current URL's search params, which triggers reload
    window.location.search = params
    Login or Signup to reply.
  2. It works fine but I think it was the SERVER issue, have you asked your network admin for some rules in the network? Can you check .htaccess ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search