Hello! There is a search form:

form id="search" name="search" method="get" onsubmit='document.location.href= "/search/"+q.value; return false;'> 

When searching for the address is obtained:

 site.ru/search/юрий%20шатунов 

but you need:

 site.ru/search/юрий+шатунов 

how to do it?

  • in q.value replace all spaces with pluses - Marsel Arduanov
  • one
    I understand oftop but why? - Naumov

2 answers 2

Simply replace all spaces with pluses with the replace function.

 form id="search" name="search" method="get" onsubmit='document.location.href= "/search/"+q.value.replace(/\s/g, '+'); return false;'> 
  • I tried this: onsubmit = 'document. location href = "/ search /" + q .value. replace (/ \ s / g, '+'); but location does not work like this - Identicon
  • Not sure of course, but it looks like it’s because of extra spaces, if you don’t add them yourself in the comments, but you can shorten the record: onsubmit='location = "/search/"+q.value.replace(/\s/g, '+'); - WanSpi
  • The quotes inside replace should be double! Thank you for help! - Ididon
  • For sure! This is due to the fact that onsubmit taken as a single, I missed this moment :) - WanSpi

Use the decodeURI(str) function in JS.
Use the urldecode($str) function in PHP.