0

So i'm making a search section for my website but i noticed that my server sees + as a space.

When i search 2 + 2 its /s/2+%2B+2 in url and 2+++2 in server (2 2 when urldecoded) but i want it to be 2 + 2, how can i achieve that?

// js
$("#searchForm").submit(function(e){
    window.location.href = "{{ route('search', ['']) }}/"+encodeURIComponent($("#search").val()).replace(/%20/g, "+");
    return false;
});

// web.php
Route::get('/s/{q}', 'Search')->name('search');

// Search controller
public function __invoke(SearchRepository $repo, $query)
{
    echo $query;
}
party34
  • 99
  • 3
  • 14
  • see https://stackoverflow.com/questions/2671840/php-plus-sign-with-get-query – Jinal Somaiya Aug 27 '18 at 11:11
  • Remove the spaces beforehand? – brombeer Aug 27 '18 at 11:11
  • Use POST instead –  Aug 27 '18 at 11:13
  • Simply stop messing with the result of `encodeURIComponent` …? That you are replacing `%20` with `+` after that, is what “destroys” your data here. – CBroe Aug 27 '18 at 11:17
  • @JinalSomaiya I already send + as an encoded value, the problem is php doesn't get other + signs as spaces before i use urldecode. kerbholz I need spaces. Arvind I can't use POST for this, users should be able to share the url with others, just like in Google. – party34 Aug 27 '18 at 11:18
  • @CBroe it looks so ugly that way, i would like spaces as plus signs in the URL. – party34 Aug 27 '18 at 11:18
  • It works perfectly if i use it like /s?q={q} and not /s/{q}, why doesn't it work the other way? – party34 Aug 27 '18 at 11:30
  • 2
    Most likely because in the one case it is part of the query string, whereas in the other you are making it part of the path. https://stackoverflow.com/q/5366007/1427878 Not sure if Laravel’s routing has an additional influence here, but in “pure” PHP using path info, this problem would also occur already (`index.php/2+%2B+2` would only get you `/2+++2` in PATH_INFO.) – CBroe Aug 27 '18 at 11:34
  • I believe, since it is a path and not a query string, the correct encoding for `2 + 2` would be `2%20%2B%202`. Using `+` for space is for query string/form data encoding. It is defined in the standard, ugly or otherwise. – Salman A Aug 27 '18 at 12:04

0 Answers0