...

View Full Version : Add search term to form



uzziwozzi
08-01-2011, 01:43 PM
Hello,

I have been working with different codes to get a search form working with a database.

I wonder if someone with better expertise can assist.

I have a search form based on drop down option values, what I would like to do is select a "company" and append the search term to the company's value.

E.G the form would be something like:

<option value="http://url?term=valueisfromsearch&folder=Company Name">Company</option>

There will be a free text area which will allow users to input their search term but need this go feed into the above value of "valueisfromsearch".

Upon submit, the user will be presented with their results in their browser.

Is this possible mid-URL? I've seen info on Javascript Query String Parameters but don't really understand it that well.

Thanks,

Uzzi

Old Pedant
08-02-2011, 05:48 AM
You need to show your entire <form> and especially the existing JS code that utilizes that <option> value.

*Probably* you don't need to work this hard, at all. But we can't tell unless we see what is being done with that <option>.

uzziwozzi
08-02-2011, 10:47 AM
Thank you for your response Old Pendant. I'll provide you with the code I'm using:

<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
var submitto = sf.sengines.options[sf.sengines.selectedIndex].value + escape(sf.searchterms.value);
window.location.href = submitto;
return false;
}
</script>
</head>

<body>

<form name="searchform" onSubmit="return dosearch();">
Search:
<select name="sengines">
<option value="http://url?term=admin&folder=ACME">Search ACME</option>
</select>
For:
<input type="text" name="searchterms">
<input type="submit" name="SearchSubmit" value="Search">
</form>

When I hit submit, I can get to the ACME folder and search for the term "admin". But I want to change it so that when users search in the box "search terms", this value is replaced by the value after term?=

I've used a sample code that I *thought* would work since the option values can be hard coded, but need the search term replaced which I think would requite scripting editing..

Many thanks.

Old Pedant
08-03-2011, 04:03 AM
How about this:


<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
var submitto = sf.sengines.value.replace( "$$$", escape(sf.searchterms.value) );
window.location.href = submitto;
return false;
}
</script>
</head>

<body>

<form name="searchform" onSubmit="return dosearch();">
Search:
<select name="sengines">
<option value="http://url?term=$$$&folder=ACME">Search ACME</option>
<option value="http://www.google.com/search?q=$$$">Search Google</option>
</select>
For: <input type="text" name="searchterms">

<input type="submit" name="SearchSubmit" value="Search">
</form>

encodeURIComponent() is better than escape(), but for most purposes either one works.

uzziwozzi
08-03-2011, 01:24 PM
Fantastic - that works perfectly :)

uzziwozzi
08-09-2011, 11:07 AM
I have an additional requirement. Is it possible to append an additional URL to the completed submitto if a tick box is checked?

I need to add &searchsubfolders=true.

I'm confusing my SQL codes and Javascript codes and I supect I would need a new variable calling on if the check box is ticked and somehow add &searchsubfolders=true to submitto... little help?

Just a thought, I tried creating a new function that can be called on if the check box is enabled, but again, having troubles having this appended to sf.sengines.value or perhaps I can add it to submitto?..

Old Pedant
08-10-2011, 12:34 AM
Easy peasy


<script type="text/javascript">
function dosearch() {
var sf=document.searchform;
var submitto = sf.sengines.value.replace( "$$$", escape(sf.searchterms.value) );
if ( sf.searchsubfolders.checked ) submitto += "&searchsubfolders=true"
window.location.href = submitto;
return false;
}
</script>
</head>

<body>

<form name="searchform" onSubmit="return dosearch();">
Search:
<select name="sengines">
<option value="http://url?term=$$$&folder=ACME">Search ACME</option>
<option value="http://www.google.com/search?q=$$$">Search Google</option>
</select>
For: <input type="text" name="searchterms">

<label><input type="checkbox" name="searchsubfolders"/> Search sub-folders?</label>
<input type="submit" name="SearchSubmit" value="Search">
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum