Asif,
The clean way to do this is to attach, in javascript, an onsubmit handler to the form element. Be sure to return false to suppress normal form submission and associated page refresh.
onload = function(){
var sf = document.getElementById("searchForm");
if(sf){
sf.onsubmit = function(){
var searchTerm = this.query.value;
alert(searchTerm);//debug statement
...
...
...
return false; //Important! Suppresses default HTML form submission behaviour.
};
}
}
<form id="searchForm">
<input type="text" name="query" />
<input type="button" value="Submit"/>
</form>
Airshow
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
couldn't that also be done by something like:
<input type="text" name="query" id="queryId" />
<input type="button" value="submit" onClick="runMe()" />
function runMe(){
var queryVar = document.getElementById("queryId").getValue();
}
or something similar? I don't really use Javascript that much, so might be completely wrong :)
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
You can do that. It's maybe more intuitive but generally considered clumsy.
The biggest issue is that runMe() would need to be in the global namespace, which should be avoided. In my code above, you will see that everything is contained in a window.onload handler. The global namespace is not used. Another advantage is that all your js functionality is in js; you don't need to hunt through the HTML to discover what event handlers are in place. This is a real issue in with large and/or templated code-bases and multi-programmer teams.Airshow
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
There's an error in my HTML:
<!--change-->
<input type="button" value="Submit"/>
<!--to-->
<input type="submit" value="Submit"/>
Airshow
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Sorry, I'll take the blame on this [rare?] occasion.
Airshow
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Suggest a single form with a div containing all the advanced elements.
When the "advanced" div is hidden its form elements will not be submitted. When it's not hidden, its form elements will be submitted.
By implementing defaults server-side for any setting that is not included in the POST/GET, both standard and advanced search will behave correctly.
Airshow
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Airshow
WiFi Lounge Lizard
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372