Sample Javascript to insert form values |
Top Previous Next |
You can use the code bits below to extract the <site> and <date> parameters from vacancy grid links as default values in your form. This assumes that the "Link Format" in your Export to Web Vacancy Grid setup is in this format:
http://www.mywebsite.com/reservationform.html?<site>&<date>
Basically there's a big block of script to extract the variables, then one line of script for each form input field. The latter script writes out the HTML code for the form input field with the value inserted as the default. There's also a <NOSCRIPT> section to write it without the variable if the user doesn't have Javascript enabled in their browser, which should be the simple <input> field for your form.
Note: Make sure that your site names don't have the "&" character in them, or else they will confuse the code.
Insert this code before the form to extract the variables:
<SCRIPT TYPE="text/javascript">
//
// This block of code extracts the <site> and <date> variables
//
var var1 = location.search.substring(1, location.search.length)
// replace %20's with spaces so they're readable
while (var1.indexOf("%20") != -1)
{
var index = var1.indexOf("%20")
var first = var1.substring(0,index)
var last = var1.substring(index+3,var1.length)
var1 = first + " " + last
}
// replace %26 with the & so we can find it
while (var1.indexOf("%26") != -1)
{
var index = var1.indexOf("%26")
var first = var1.substring(0,index)
var last = var1.substring(index+3,var1.length)
var1 = first + "&" + last
}
// extract the two parameters
myparams = var1.split("&")
var param1 = ""
var param2 = ""
if (myparams.length > 0) { var param1 = myparams[0] }
if (myparams.length > 1) { var param2 = myparams[1] }
// see if it's in "May 23, 2005" format
dateparts2 = param2.split(",")
if (dateparts2.length == 2)
{
dateparts3 = dateparts2[0].split(" ")
if (dateparts2.length == 2 && dateparts3.length == 2)
{
month = dateparts3[0]
day = dateparts3[1]
year = dateparts2[1]
if (year.length == 3 || year.length == 5)
{ year = year.substring (1, year.length) }
if (year.length == 4 && year.substring(0,2) == "20")
{ year = year.substring(2,4) }
if (month.substring(0,3) == "Jan") { month = "1"}
if (month.substring(0,3) == "Feb") { month = "2"}
if (month.substring(0,3) == "Mar") { month = "3" }
if (month.substring(0,3) == "Apr") { month = "4" }
if (month.substring(0,3) == "May") { month = "5" }
if (month.substring(0,3) == "Jun") { month = "6" }
if (month.substring(0,3) == "Jul") { month = "7" }
if (month.substring(0,3) == "Aug") { month = "8" }
if (month.substring(0,3) == "Sep") { month = "9" }
if (month.substring(0,3) == "Oct") { month = "10" }
if (month.substring(0,3) == "Nov") { month = "11" }
if (month.substring(0,3) == "Dec") { month = "12" }
param2 = month + "/" + day + "/" + year
}
}
</SCRIPT>
Insert this code in your form to insert the default value for the site requested (<site> parameter). (You may need to modify the actual <input...> code to match your form's requirements.)
Site Requested:
<SCRIPT>
document.write('<input type="text" name="Site_Name" value ="' + param1 + '">')
</SCRIPT>
<NOSCRIPT>
<input type="text" name="Site_Name">
</NOSCRIPT>
Insert this code in your form to insert the default value for the arrival date (<date> parameter). (You may need to modify the actual <input...> code to match your form's requirements.)
Arrival Date:
<SCRIPT TYPE="text/javascript">
document.write('<input type="text" name="First_Night" value ="' + param2 + '">')
</SCRIPT>
<NOSCRIPT>
<input type="text" name="First_Night">
</NOSCRIPT>
(MM/DD/YYYY)