PDA

View Full Version : adding two form field number values


gunner
01-30-2003, 04:13 AM
at the bottom the called script is two form fields named wor and wor2, i'm tring to add them together and send that summation to the next page:
document.f6.action="Language.htm?wor="+sum+data ;

<script type="text/javascript">
///this first script checks the radios so only one can be selected////
<!-- Begin
var data = "";
var itemchecked = false;
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el[i].type == "radio") {
var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select where you plan to settle");
if(el[i].focus)
el[i].focus();
return false;
}
}
}
var sStr = document.location.search;
var val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.indexOf(",")));
//???/// I have values from a previous page /form ////and below is where i think i'm having problems with f6.(wor + wor2) //???////
var sum = parseInt(document.f6.wor[j].value)+((!isNaN(val))?val:0);
if(itemchecked){
data = sStr.substring(sStr.indexOf(","),sStr.length)+ ",(wor="+parseInt(document.f6.wor[j].value)+")";
document.f6.action="Language.htm?wor="+sum+data ;
}
//return true;
}

//// below i'm just writing the last summation to the browser //////
window.onload = updateRunningScore;
function updateRunningScore(){
var sStr = document.location.search;
val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.length));
document.getElementById("currentScore").innerHTML = "Your score up to this point is = "+((!isNaN(val))?val:0);
}
// End -->
</script>

<body>
...........

<form name="f6" action="" method="post" onSubmit="checkRadios(this)">
<table cellSpacing="10" cellPadding="0" width="696" border="0">
<tbody>
<tr>
<td align="left" width="666">
<div align="left">
<font face="Times New Roman" size="3">4 or more years related to skilled occupation</font>
</div>
</td>
<td align="left" width="29"><font size="-1"><input type="radio" value="16" name='wor' ></font></td>
</tr>
<tr>
<td align="left" width="666">
<div align="left">
<font face="Times New Roman" size="3">3.5 years related to skilled occupation</font>
</div>
</td>
<td align="left" width="29"><font size="-1"><input type="radio" value="14" name="wor">

blah blah blah more options.......

<input type="radio" value="0" name='wor2' checked>
</font>
</td>

</tr>
</tbody>
</table>
<p><input type="submit" value="Next" name="submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Clear" name="Reset"></p>
</form>

gunner
01-30-2003, 08:49 PM
Please help... any takers.....
I've tried variations of the sort:

var sum = parseInt(document.f6.wor[j].value+document.f6.wor2[j].value)+((!isNaN(val))?val:0);

data = sStr.substring(sStr.indexOf(","),sStr.length)+ ",(wor="+parseInt(document.f6.wor[j].value+document.f6.wor2[j].value)+")";

gunner
01-31-2003, 11:33 PM
ahhh,,, this is so simple yet i can't figure it out..... arrrrrgh....

BrightNail
02-01-2003, 08:05 AM
huh....going thru your code, I'm not sure what you are trying to do.....

use this code to read a name/value pair from the url string.
If you wanted the value for the name "bicycle"...you say

getQry('bicycle')

and you'd get whatever value is associated with bicycle. Now, make sure you turn the strings into numbers if you havne't done so...

function replace(str, from, to) {
var i = str.indexOf(from);
if (!from || !str || i == -1) return str;
var newstr = str.substring(0, i) + to;
if (i+from.length < str.length)
newstr += replace(str.substring(
i+from.length,str.length),from,to);
return newstr;
}
function getQry(nm)
{
if (!location.search) return '';
var i, j, qry;
qry = location.search.substring(1);
i = qry.indexOf(nm)+nm.length+1;
if (i == nm.length) return '';
j = qry.indexOf('&',i);
if (j+1) qry = qry.substring(i,j);
else qry = qry.substring(i);
return unescape(replace(qry, '+', ' '));
}

function substitute(str, from, to) {
var i = str.indexOf(from);
if (!from || !str || i == -1) return str;
var newstr = str.substring(0, i);
if (i+from.length < str.length)
newstr += replace(str.substring(
i+from.length,str.length),from);
return newstr;

}
function convert(nm)
{
if (!location.search) return '';
var i, j, qry;
qry = location.search.substring(1);
i = qry.indexOf(nm)+nm.length+1;
if (i == nm.length) return '';
j = qry.indexOf('&',i);
if (j+1) qry = qry.substring(i,j);
else qry = qry.substring(i);
return unescape(substitute(qry, '+'));
}




if this isn't what you are trying to do...then look at your code here:

function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el[i].type == "radio") {
var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}


if it were me, you are doing the check here to see if the selection is checked...right?..if so, why don't you just funnel the value into a price array...then, add the totals of the price array and pass that?

gunner
02-01-2003, 06:42 PM
thanks for the reply......

this is what i'm trying to do:

I have a multipage form that uses radio buttons, although each form on each page has multiple questions, only one answer/option is allowed to be selected, so there is only one number being passes from form to form. Think of this as a math test with 6 questions, all multiple choice and 1 question per page. So after the first question / form i writing the score at the top of each preceeding page (adding what has been attained from each form as the user goes along) So, on each preceeding form i'm displaying the summation value of the users answers from the past questions. Now, on the final page after the questions i'm displaying what scaore the user attained as well as a points breakdown (displaying what score the user got from each question) so i'm sending each forms number results in an array thru the url along with the number value attaind from the question. So my url on form 3, after 2 submits looks like this:
......mand.htm?edu=26,(age=10),(edu=16)

Now i have this question 5 on form # 5 that has a bonus question. So the user can select from one of the solutions of question 5 as well as select the bonus question, which is worth 2 extra points. Now i assigned the radio values for the bonus question as either a 0 point selection or a 2 point selection. So i would like to add question 5 points attaind by the user and the points the user atained from the bonus question on the same form.

For Example.....
*****This is form 1 JS:
<script type="text/javascript">
var itemchecked = false;
var val;
<!-- Begin
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el[i].type == "radio") {
var radiogroup = el[el[i].name]; // get the whole set of radio buttons.

for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select an Age");
if(el[i].focus)
el[i].focus();
return false;
}
}
}
var sStr = document.location.search;
val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.length));
var sum = parseInt(document.f1.age[j].value)+((!isNaN(val))?val:0);
if(itemchecked){
var data = ",(age="+sum+")";
document.f1.action="Education.htm?age="+sum+data ;

}
return true;
}
end -->

*****And this is form2 JS:

<script type="text/javascript">
<!-- Begin
var data = "";
var itemchecked = false;
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el[i].type == "radio") {
var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Please select an Education Level");
if(el[i].focus)
el[i].focus();
return false;
}
}
}
var sStr = document.location.search;
var val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.indexOf(",")));
var sum = parseInt(document.f2.edu[j].value)+((!isNaN(val))?val:0);
if(itemchecked){
data = sStr.substring(sStr.indexOf(","),sStr.length)+ ",(edu="+parseInt(document.f2.edu[j].value)+")";
document.f2.action="Occupational_Demand.htm?edu="+sum+data ;
}
//return true;
}

window.onload = updateRunningScore;
function updateRunningScore(){
var sStr = document.location.search;
val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.length));
document.getElementById("currentScore").innerHTML = "Your score up to this point is = "+((!isNaN(val))?val:0);
}
// End -->'


so the html with teh JS of the page that has the bonus question which is field name wor2 is in the original post....... maybe this can clearify what i'm doing and would like to acheive.

BTW- this is the last page of the forms and how i'll be calling the array... although this is really not important and probably with most of my jibber jabber, but none the less this is it:

<script type="text/javascript">
var itemchecked = false;
var dataArr = new Array();
var data = "";
<!-- Begin
function checkRadios() {
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if(el[i].type == "radio") {
var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
for(var j = 0 ; j < radiogroup.length ; ++j) {
if(radiogroup[j].checked) {
itemchecked = true;
break;
}
}
if(!itemchecked) {
alert("Your final score is: ");
if(el[i].focus)
el[i].focus();
return false;
}
}
}
var sStr = document.location.search;
var val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.indexOf(",")));
var sum = parseInt(document.f3.occ[j].value)+((!isNaN(val))?val:0);
if(itemchecked){
data = sStr.substring(sStr.indexOf(","),sStr.length)+ ",(occ="+parseInt(document.f3.occ[j].value)+")";
parseAllEntries();//This function where finalize calculations are.
document.f3.action="Guaranteed_Employment.htm?occ="+sum+data;
}
return true;
}

/*
THIS FUNCTION MUST BE USED AT THE VERY LAST POINT WHERE ALL USER INPUT NEEDS TO BE REFERENCED
this function will parse URL data into an array of all the entries and their
respective values selected by user
for example
dataArr[n][0] will be "age"
dataArr[n][1] will be "10"
*/
function parseAllEntries(){
var temp = data.split(",");
var dataArr = new Array();
z=0;
for(n=0;n<temp.length;n++){
var str = temp[n];
var desc = str.substring(1,str.indexOf("="));
var value = str.substring(str.indexOf("=")+1,str.length-1);
if(n>0){
dataArr[z] = new Array(desc,value);
z++
}
}
//Now can access this array with the following pattern and get
//desc = which is the area description such as age,edu and so on
//with value which is 10 or 20
//dataArr[0][0]+", "+dataArr[0][1])
}
// End -->