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)

 {

   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/samplejavascripttoinsertfo.html

Campground Master Home