How do I change the url parameter of a form to submit?

I have this form to process.

<form action="driver.php" method="GET">
<input type="text" placeholder="Search" id="search" name="n">
<button type="submit">Submit</button>
</form>

      


default url http://sites.com/driver.php?n=typedname

so I made changes to .htaccess and converted it to http://sites.com/driver/typedname

for friendly urls modrewrite issue - 404 error thanks to anubhava

everything is fine, but the problem now occurs when I type and press the submit button, it goes to http://sites.com/driver.php?n=typedname

so how can i do this to go to this url http://sites.com/driver/typedname

instead of clicking submit?

I think javascript can do this, so I tagged it, hope I am not mistaken.

thank.

+3


source to share


2 answers


JQuery

$('button[type=submit]').click(function(e){
    e.preventDefault();
    e.stopPropagation();

    window.location = "http://sites.com/driver/" + $('#search').val();

    return false;
});

      

or



$('form').submit(function(e) { // you really need to class/ID your form
    // all that code
});

      

Now it's quick and dirty to give you this idea. Of course you want to misinform your input (it's a good idea to do this both on the frontend and in the background).

+2


source


You will need to handle the form submit event yourself. Something like that:



document.querySelector('form').addEventListener('submit', function(e) {
    e.preventDefault();
    location.href = '/driver/' + this.elements.n.value;
}, false);

      

+2


source







All Articles