Web Hosting Talk







View Full Version : how to assign 2 values to a html radio click


mjfroggy
05-20-2009, 07:04 AM
Ok I have a quality assurance page where I ask 10 questions.
Answers to all questions are yes, no or na. All the different questions have different points assigned to them so like question one would have 2 points if the user answers yes or n/a and of course 0 points if answer is no vs question two which may have 5 points if answer is yes or na and 0 points if answer is 0.

So my issue is as someone fills out the form I need the page to tally the points and display it on the page (I know javascript can do this) but my issue is the I also need to be able to display what answers where put in for what questions. Answers are yes, no and na . So since each question has radio clicks and when you click a yes it has a value of how many points are assigned to that answer how do I know which answer was clicked when I display the data back.

I mean if someone answers yes or na they get the same about of points. So then when I pull from a mysql database the answer that was entered to question 2 the var maybe 5 but 5 would not tell me if it was yes that was clicked or na that was clicked for that question.

So in essence I need to have my radio clicks have 2 values one that is used to enter into a mysql database and one that is used by javascript to sum up on the page. Any suggestions on how to do this?

Thanks

Arun - HostLevel3
05-20-2009, 08:22 AM
the name attribute of the radio elements in a question-ans should be kept same (eg. name="Q1") for all the 3 ans radio buttons but their value attribute should be different. (eg. value="yes" for 1st, value="no" for 2nd, value="na" for 3rd) :)

mjfroggy
05-20-2009, 09:46 AM
Well that doesn't really help me unless I am not understanding your answer.

All my radio buttons have a name so like the yes no and na radio clicks for question1 have a name of q1 vs the yes no and na for question two radio clicks are all called q2 they also all have valuyes which is how I am able to sum up each answer and display it to the page. My issue is when I go to put the values of each clicked radio into the database I need to know if the value is representing yes no or na. and since yes and na both have a value that are the same I need some advice on how to maybe have my radio clicks have a 2nd value attribute which could be 1 2 or 3 which then can help me know which answer was clicks (i.e., yes, no, na)

here is one of my radio clicks to one of the questions
which may help

<input type="radio" name='Q1' id='game0' value="5" onClick="UpdateCost(), document.getElementById('color').style.background='#009900'" >
<B>N</B><BR><input type="radio" name='Q1' id='game1' value="0" onClick="UpdateCost(), document.getElementById('color').style.background='#990000'">
<B>N/A</B><BR><input type="radio" name='Q1' id='game2' value="5" onClick="UpdateCost(), document.getElementById('color').style.background='#93DCFF'">


then I have my javascript which sums the value of all clicked radios

function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<29; i++) {
gn = 'game'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost').value = sum.toFixed(0);
}


So know when I go to enter the value of the clicked radio for the first question my $_post['Q1'] is always going to be a value of 0 or 5 and I need to know if the value is 5 if that 5 means yes was clicked or na was clicked. which brings me back to needing my radio click to have a second value='' which I can make be 1 , 2 ,3 to help me know which actual radio was click

hope this makes it clearer what I need to do?
Thanks

Xenatino
05-20-2009, 10:08 AM
I'd prefix the value with a value relating to the answer. ie:


<input type="radio" name='Q1' id='game0' value="Y5" onClick="UpdateCost(), document.getElementById('color').style.background='#009900'" >
<input type="radio" name='Q1' id='game1' value="N0" onClick="UpdateCost(), document.getElementById('color').style.background='#990000'">
<input type="radio" name='Q1' id='game2' value="A5" onClick="UpdateCost(), document.getElementById('color').style.background='#93DCFF'">


And then use substr() to grab the number from the value:


function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<29; i++) {
gn = 'game'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value.substr(1)); }
}
document.getElementById('totalcost').value = sum.toFixed(0);
}