Handle path of a form input sent to url

1

I'm testing a search field that sends what the user types directly to the site I've chosen (on a new tab), searching the site itself, and works perfectly. However, I would like to know if I can manipulate this address that is sent to the url.

For example (searching for Youtube): If the user types "best serials" in the form, and press Enter, the form completes the action with the name (q) and search term and sends it to the url. Staying: link ? Q = best + serials

In this way I wanted him to send the url without the "name" and "?", only with "/" and the search term, as if it were a normal link. Example: link / best + serials

Does anyone know if there is any way to handle this with php or javascript?

Follow the form code:

<form name="searchfield" method="get" action="http://www.youtube.com/results" target="_blank">
<input type="text" id="input" name="q" autocomplete="off" maxlength="100" size="36" placeholder="Pesquise no YouTube">
<input type="image" id="search" name="btnG" src="img/search.png">
</form>
    
asked by anonymous 08.07.2015 / 19:25

1 answer

1

You can manipulate the action of your form at the moment it is submitted:

$('form').submit(function(e) {

    var $this = $(this),
        value = $this.find('input[name="q"]').val(),
        url   = $this.attr('action');

    var newTargetUrl = (url+value).replace(" ", "+");

    $this.attr("action", newTargetUrl);

    // Para simular, comentar ou remover para ver a funcionar.
    e.preventDefault();
    alert(newTargetUrl);
});

View in JSFiddle .

But with YouTube, you have a problem, it does not show results with friendly URLs because it does not support them.

    
08.07.2015 / 21:13