0

On click of search button in my form, i want to set some param to url and submit it.

Below is my javascript

function validateSubmitSearch(form) {   
  if(form.elements["iAccept"].checked == true) {
    form.query.value=form.search_query.value;
    form.action = "vendor/search";
    form.method = "GET";
    form.submit();
  }
}

This javascript is returning url as http://localhost:8080/Project/vendor/search?query=xxx&search_query=xxx&search=Search

Instead i need it as http://localhost:8080/Project/vendor/search?query=xxx

How this can be done ?

EDIT:

I cannot remove form elements search_query and search

Here is HTML code

<form class="searchform" onSubmit="validateSubmitSearch(this)">
  <input name="query" type="hidden" />
  <input name="search_query" class="textbox" type="text" />
  <input type="checkbox" id="iAccept" name="iAccept" value="I ACCEPT">
  <input name="search" class="button" value="Search" type="submit" /></td>
</form>
4
  • please add complate of your html codes . Commented Aug 26, 2016 at 22:26
  • what is the value of form.search_query.value;? Commented Aug 26, 2016 at 22:31
  • @IvanBarayev : Added HTML code Commented Aug 26, 2016 at 22:32
  • @SamuelToh : form.search_query.value = input text in textbox Commented Aug 26, 2016 at 22:32

1 Answer 1

1

When you submit a form with `method="GET", all the input fields in the form will be included as URL parameters. If you want some of them to be left out, you need to remove those inputs from the form.

function validateSubmitSearch(form){

    if(form.elements["iAccept"].checked == true)
        {
            form.query.value=form.search_query.value;
            form.action = "vendor/search";
            form.method = "GET";
            form.seach_query.parentNode.removeChild(form.search_query);
            form.search.parentNode.removeChild(form.search);
            form.submit();
        }
    }
}

Also, you should disable the default form submission by returning false from the onsubmit code.

<form class="searchform" onsubmit="validateSubmitSearch(this); return false;">
Sign up to request clarification or add additional context in comments.

3 Comments

This is not working either, URL still has the unwanted params
How are you calling the function? Maybe you're not disabling the default submission.
Thanks mate, You hit the bulls eye.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.