Basically, I'm new to JavaScript, but I have a strong background in PHP. I’ve developed this survey application to pull questions out of a db. Well I've come down to a problem where my PHP application might build a complex survey question that has checkboxes and then the very last checkbox is for a text box to enter their own answer if they can’t find one that applies to them. The problem is that if people just start typing in the box, and not select the checkbox, then I'm not going to get their data.
Thus JavaScript had to come into play, but I came across a problem. My PHP dynamically builds the form input names using a numerical counter, then a ~ followed by an alpha counter (depending on the type of question....radio, checkbox, etc). So when I try and call a function to check the box for me I come across an issue...
Apparently using numbers as the first character is an illegal operation (This didn’t surprise me as most languages have this rule). So I figured big deal.. I’ll just put a "Q" in front of the number and split it out later... Well then the ~ symbol came across as an illegal character in my name string. Anyways, you get the idea... I’m struggling with using illegal characters in a JavaScript function.
I need to find a way to make the script work given the html form below, I really can't modify the naming system without rebuilding the entire PHP application. Is there a way I can force JavaScript to use illegal characters? If it absolutely can't use a number as the first value.. I can work around that.. but the ~ system needs to stay.
Here is the sample code I'm having problems with along with my simple JavaScript function.
Thus JavaScript had to come into play, but I came across a problem. My PHP dynamically builds the form input names using a numerical counter, then a ~ followed by an alpha counter (depending on the type of question....radio, checkbox, etc). So when I try and call a function to check the box for me I come across an issue...
Apparently using numbers as the first character is an illegal operation (This didn’t surprise me as most languages have this rule). So I figured big deal.. I’ll just put a "Q" in front of the number and split it out later... Well then the ~ symbol came across as an illegal character in my name string. Anyways, you get the idea... I’m struggling with using illegal characters in a JavaScript function.
I need to find a way to make the script work given the html form below, I really can't modify the naming system without rebuilding the entire PHP application. Is there a way I can force JavaScript to use illegal characters? If it absolutely can't use a number as the first value.. I can work around that.. but the ~ system needs to stay.
Here is the sample code I'm having problems with along with my simple JavaScript function.
Code:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function f(x){eval("document.test." + x + ".checked=true");}
//-->
</SCRIPT>
</head>
<body>
<form action="submit.php" method="POST" name="test">
<b>What do you plan on majoring in?</b>
<table>
<tr><td><input type="checkbox" name="7~A" value="7~Behavioral Sciences~A~1"></td><td>Behavioral Sciences</tr>
<tr><td><input type="checkbox" name="7~B" value="7~Business~B~1"></td><td>Business</tr>
<tr><td><input type="checkbox" name="7~C" value="7~Communications~C~1"></td><td>Communications</tr>
<tr><td><input type="checkbox" name="7~D" value="7~Computer Science~D~1"></td><td>Computer Science</tr>
<tr><td><input type="checkbox" name="7~E" value="7~Education~E~1"></td><td>Education</tr>
<tr><td><input type="checkbox" name="7~F" value="7~English~F~1"></td><td>English</tr>
<tr><td><input type="checkbox" name="7~G" value="7~Fine Arts~G~1"></td><td>Fine Arts</tr>
<tr><td><input type="checkbox" name="7~H" value="7~Graphic Arts~H~1"></td><td>Graphic Arts</tr>
<tr><td><input type="checkbox" name="7~I" value="7~History~I~1"></td><td>History</tr>
<tr><td><input type="checkbox" name="7~J" value="7~Mathematics~J~1"></td><td>Mathematics</tr>
<tr><td><input type="checkbox" name="7~K" value="7~Nursing/Pre-Med~K~1"></td><td>Nursing/Pre-Med</tr>
<tr><td><input type="checkbox" name="7~L" value="7~Religion/Ministry~L~1"></td><td>Religion/Ministry</tr>
<tr><td><input type="checkbox" name="7~M" value="7~Science~M~1"></td><td>Science</tr>
<tr><td><input type="checkbox" name="7~N" value="7~Undeclared~N~1"></td><td>Undeclared</tr>
<tr><td><input type="checkbox" name="7~O" value="OTHER"></td>
<td><input type="text" size="35" maxlength="256" name="7~TEXT~O" onfocus="f('7~0')">(other)</td></tr>
</table>
</form>