1,105,271 Community Members

Syntax Errors With My Javascript Function

Member Avatar
domh87
Newbie Poster
3 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

I am building a site and i need to move the contents from one form to another on a different page. The code i have used worked fine when i was testing, but since i put it on my live site i get syntax errors which i think is because of the '<' sign. The problem is, i havent got a clue what is wrong so your help is appreciated. Below is my code with the form and javascript to get the values from the URL.

<a name="details"></a>
<div class="box">
<div class="header">
<div class="h1">
<h1>Your Details</h1>
</div>
</div>
<div class="content">
<div id="webform-client-form">
<form method="POST" name="contactform" action="contact-form-handler.php">
<table>
<tr>
<td><label for="typecover">Type of Cover:</label></td>
<td colspan="3"><div class="select"><select id="typecover" name="typecover">
<option value="PersonalLifeInsurance">Personal Life Insurance</option>
<option value="IncomeProtection">Income Protection</option>
<option value="FamilyProtection">Family Protection</option>
</select></div></td>
</tr>
<tr>
<td><label for="policy">Policy is for:</label></td>
<td colspan="3"><div class="select"><select id="policy" name="policy">
<option value="Joint">Joint</option>
<option value="Individual">Individual</option>
</select></div></td>
</tr>
<tr>
<td><label for="smoker">Smoker:</label></td>
<td><div class="small-select"><select id="smoker" name="smoker">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select></div></td>
<td><div class="label"><label for="year">Birth Year:</label></div></td>
<td><div class="small-select"><select id="year" name="year">
<option value="1986">1986</option>
<option value="1987">1987</option>
</select></div></td>
</tr>
</table>
<div class="input"><input type="submit" value="CONTACT US"></div>
</form>
</div>

<div class="waxseal">
</div>
</div>
</div>

<script type="text/javascript">

function replace(string,text,by) {
    // Replaces text with by in string
    var i = string.indexOf(text), newstr = '';
    if ((!i) || (i == -1))
        return string;
    newstr += string.substring(0,i) + by;
    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);
    return newstr;
}

var passed = replace(replace(location.search.substring(1),"+"," "),"=","&");

function split(string,text) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return;
    if (i == -1) {
        splitArray[splitIndex++] = string;
        return;
    }
    splitArray[splitIndex++] = string.substring(0,i);
    if (i+txtLength < strLength)
        split(string.substring(i+txtLength,strLength),text);
    return;
}

</script>

<script type="text/javascript">

function split(string,text) {
    splitArray = string.split(text);
    splitIndex = splitArray.length;
}

</script>

<script type="text/javascript">

var splitIndex = 0, splitArray = new Object();

split(passed,'&');

for (var i=0; i < splitIndex; i=i+2){
    if (splitArray[i] == 'typecover')
        document.contactform.typecover.value = unescape(splitArray[i+1]);
    if (splitArray[i] == 'policy')
        document.contactform.policy.value = unescape(splitArray[i+1]);
    if (splitArray[i] == 'smoker')
        document.contactform.smoker.value = unescape(splitArray[i+1]);
    if (splitArray[i] == 'year')
        document.contactform.year.value = unescape(splitArray[i+1]);
}

</script>
Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Parsing out a URL's query string is quite simple if you approach it th right way.

My QueryParser() will save you a lot of heartache.

Not only does it parse out the query string but also includes a bunch of useful methods, and it only puts a maximum of two members in the global namespace (none if you install it in a closure), and it's fully documented.

Airshow

Member Avatar
domh87
Newbie Poster
3 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for getting back to me, its not getting the fields into the URL its getting them back out into a series of select boxes that i cannot get to grips with.

Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Yes, I understand that.

Use QueryParser parses the query sting and makes its name|value pairs available as properties of an object.

That is one half of your problem solved.

The other half of the problem, setting the values of select menus, is most simply solved by using jQuery, which provides you with a command of the form:

$("#xxx").val($q.xxx);

A series of such statements will set each select menu in turn.

With jQuery and QueryParser installed on the page, you need just four javascript statements to make the whole thing work.

Airshow

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: