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)

 {

   mon = 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 (mon.substring(0,3) == "Jan") { mon = "1"}

   if (mon.substring(0,3) == "Feb") { mon = "2"}

   if (mon.substring(0,3) == "Mar") { mon = "3" }

   if (mon.substring(0,3) == "Apr") { mon = "4" }

   if (mon.substring(0,3) == "May") { mon = "5" }

   if (mon.substring(0,3) == "Jun") { mon = "6" }

   if (mon.substring(0,3) == "Jul") { mon = "7" }

   if (mon.substring(0,3) == "Aug") { mon = "8" }

   if (mon.substring(0,3) == "Sep") { mon = "9" }

   if (mon.substring(0,3) == "Oct") { mon = "10" }

   if (mon.substring(0,3) == "Nov") { mon = "11" }

   if (mon.substring(0,3) == "Dec") { mon = "12" }

 

   param2 = mon + "/" + 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)

 

 


Page URL http://CampgroundMaster.com/help/samplejavascripttoinsert_2.html

Campground Master Home