Sample Javascript to Insert Form Values

Top  Previous  Next

 

 

 

If you already have a reservation form on your web site (using the standard HTML <form> tags), you can use the Javascript code below to extract the <site> and <date> parameters from vacancy grid "V" links and use them as default values in your form.  If you don't yet have a reservation form, or if you want an example form that will work well with the Online Reservation functionality, see the next section.

 

You should have your web site designer do this if you're not familiar with editing HTML pages in text form.  The Javascript code 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>

 

The first big block of script extracts the site and date from the variables in the link, then the other blocks show how to use those as default values in your form's <input> fields.

 

Important: Make sure that none of your site names 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)

 

 


Page URL https://CampgroundMaster.com/help/samplejavascripttoinsertfo.html

Campground Master Home