PDA

View Full Version : javascript write hidden form element



abskure
05-17-2006, 03:15 AM
Ive created a form with one text field that will be used to search an engine for properties. I've set up javascript so that depending on what is entered, the form then knows what the user is searching by (zip, city, or ID) and then the corresponding value.
The search engine takes strict parameters of (example) KeyField="City" and City="Atlanta"

Here is what I have so far:


<form id="propSearch" name="propSearch" method="post" action="http://atlantapremiererealestate.listingware.com/search/searchresults.cfm">


<input name="shortCut" type="text" id="shortCut"/>

<script type="text/javascript">
<!--
var textValue = document.propSearch.shortCut.value;
var Keyfield = "";
if (isNaN(textValue)) {
KeyField = "City";
}
else {
if(textValue.length == 5)
{
KeyField = "PostalCode";
}
else
{
KeyField = "ListingID";
}
}

var theField = "<input type=\"hidden\" name=\"KeyField\" value=\"" + KeyField + "\"/>";
document.write(theField);
var theValue = "<input type=\"hidden\" name=\"" + KeyField + "\" value=\"" + textValue + "\"/>";
document.write(theValue);

//-->
</script>
<input type="submit" name="Submit" value="Go!"/>


</form><p>Enter City, 5 digit Zip, or MLS ID#</p>

I've tested bits of the code, and the problem lies in the part where I write the hidden field. If I dont have the brackets <>, the script seems to write fine.

Does anyone know what I'm doing wrong and how I can change this?

It's important that I use the post form method with hidden fields rather than create a strURL.

mwinter
05-17-2006, 12:01 PM
Does anyone know what I'm doing wrong and how I can change this?Your general approach is wrong.

If you're going to guess what the user has entered, that guess should be made server-side after the form has been submitted. Nowhere else. If not, use separate fields.

Mike

abskure
05-31-2006, 01:59 AM
OK, that makes sense then. But, I don't have access to the server side script, it's a search engine that lets me pass it parameters. So, if I can't adjust the server-side script that interprets the info being passed to it, then I have to use separate boxes...bummer. I'll work it out though.

Thanks for your reply