Sachin Goyal Sachin Goyal - 3 months ago 8
Javascript Question

Construct GET url from java script and submit

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>

Answer

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;">
Comments