3

I have this form for processing..

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


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

so i did modifications in .htaccess and convert it to http://sites.com/driver/typedname for SEO friendly urls modrewrite issue - 404 error thanks to anubhava

everything is well, but the problem now comes when I type and click the submit button, it will go to http://sites.com/driver.php?n=typedname

so how can I make it to go this url http://sites.com/driver/typedname instead after clicking submit?

I think javascript can do this, so i tagged it, hope im not wrong.

thanks.

4
  • 1
    I am confused with what you are trying to do exactly? You're trying to go this new page after submit? Change your action URL?
    – cport1
    Commented Dec 10, 2014 at 20:14
  • so now whether you type it sites.com/driver/typedname or sites.com/driver.php?n=typedname it works ?
    – Yehia Awad
    Commented Dec 10, 2014 at 20:15
  • No, this is nothing that javascript should do. Instead, make your webserver transparently redirect clients to the url with the slash.
    – Bergi
    Commented Dec 10, 2014 at 20:15
  • cport I don't mean exactly to go to that url, I have just used it as an example.. it all depends what the user inputs. so whatever the user inputs and he/she clicks submit, I want it to go sites.com/driver/userinput
    – Overflow
    Commented Dec 10, 2014 at 20:20

2 Answers 2

5

You will have to handle form submit event yourself. Something like this:

document.querySelector('form').addEventListener('submit', function(e) {
    e.preventDefault();
    location.href = '/driver/' + this.elements.n.value;
}, false);
1
  • dfsq that worked perfectly. i just specified the form with an id so it doesn't send every form on the page to the same place. & changed the location and value and it worked beautifully. now i don't have to use a slow & sloppy redirect on my ajax live search just to keep my website uniform with all mod_rewrite pretty link locations. awesome.
    – dynastoned
    Commented Jul 8, 2021 at 6:32
4

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 this is quick and dirty to give you the idea. You'd of course want to sanitize your input (good idea to do that on the front-end as well as the back-end).

1
  • 2
    What if the form is submitted by pressing enter in the input?
    – Bergi
    Commented Dec 10, 2014 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.