473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Radio button and textbox

I need to do for a system that calculate the marks. At first, I had created the radio buttons and textboxes for user to insert the marks, but it seems not so user friendly.

So I decide to change them into automatically calculate the marks which means that when the user select the radio button, the system will automatically calculate and display the marks in the textbox.

For example, maximum marks for question 1 is 5 and rating=1,2,3,4,5 (radio button) and user select 1, therefore the textbox will display value 1. But, for example, when the maximum marks for question 1 is 10 and rating=1,2,3,4,5 (radio button) and user select 3, I believe that most of the people will code the system to calculate and display at textbox witht the value 6. But in manual the marks can be 5 or 6. Therefore, is there any appropriate ways to do this?or any kinds of suggestion or comments which is more suitable to solve this problem are appreciated. Thanks in advance.

The maximum marks and most of the data are retrieved from database and display using looping.

If I want the user select the radio button and directly calculate for the value and display in textxbox, then how?Furthermore, How can I save the value of radio button and textbox (the value which calculate automatically) in the database?
May 3 '10 #1
17 3582
acoder
16,027 Expert Mod 8TB
Why can't the rating correspond to the maximum mark, so if it's 10, the rating would be 1 to 10 and not 1 to 5?

Saving the values in the database depends on the language and database you're using. Ask in the appropriate forum.
May 4 '10 #2
@acoder
If the maximum value is more than 30, there will too many radio button. Which forum is appropriate?
May 4 '10 #3
acoder
16,027 Expert Mod 8TB
Then don't use radio buttons. Either use a text field or a drop down (select element).

This forum is appropriate for your original question, but if you want to know how to save the values to the database, it will depend on the language/database you're using.
May 4 '10 #4
@acoder
Ya. I used drop down list now. May I know after I select the marks, then how I can calculate them and show in a textbox? For example, once I select the marks, it will show in the textbox, then when I select the second, the third, and so on, they are going to add one by one and display in the textbox. How I can do for this? Thanks.
May 6 '10 #5
acoder
16,027 Expert Mod 8TB
When a mark has been selected, use onchange to detect that it has. This can call a function to add up the marks. You can set the values of the ones that haven't been selected yet to 0, so the total mark is correct. Use parseInt to convert the string to an integer.
May 7 '10 #6
@acoder
Thanks. Can I do the same for my original question, which is the radio button? I just want to multiply. For example, if user choose (radio button) 5, then times 2 and do the same for the rest of the question with different number to multiply. And I need to display each of the value in a textbox and total up them again in a textbox. So I just need to loop all the questions and multiply one-by-one?
May 8 '10 #7
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_array($result))
  2.                 { 
  3.                     echo "<td width='50' align='center'>$row[no]</td>";
  4.                     echo "<td width='500' align='left'>$row[question]</td>";
  5.                     echo "<td align='center' width='50'>$row[max_mark]</td>";
  6.                     echo "<td align='center' width='500'><input type='radio' name='rating[$i]' id='rating$i' value='1' onClick='calcRadio()'/>1
  7.                                                                <input type='radio' name='rating[$i]' id='rating$i' value='2' onClick='calcRadio()'/>2
  8.                                                             <input type='radio' name='rating[$i]' id='rating$i' value='3' onClick='calcRadio()'/>3
  9.                                                             <input type='radio' name='rating[$i]' id='rating$i' value='4' onClick='calcRadio()'/>4
  10.                                                             <input type='radio' name='rating[$i]' id='rating$i' value='5' onClick='calcRadio()'/>5</td>";
  11.                     echo "<td align='center' width='50'><input name='score[$i]' type='text' id='title'size='5' align='middle'></td>";
  12.                                         echo "</tr>";
  13.  
  14.                     $i++;
  15.  
  16.  
  17.                 }
I know I need to create a function onClick at radio button and immediately show the value in the textbox. And save both the value of radio button and textbox in the database but I don't how to calculate. Can anyone show me? Thanks in advance.
May 8 '10 #8
I had try this. But why it show in all textbox once a radio button was clicked? Moreover, it didn't calculate the value I expected. For example, for question 1, no matter what radio button (1,2,3,4,5) I clicked, it remains show value 1 in all the textbox. Why? Didn't the .value property will read the value for that radio button (for example, value 1,2,3,4,5) when it was checked and multiply with the number I write?
Expand|Select|Wrap|Line Numbers
  1. function calcRadio()
  2. {
  3.     var rating1 = parseInt(document.getElementById("rating[1]").value);
  4.     document.getElementById("score[1]").value = rating1*1;
  5.  
  6.     var rating2 = parseInt(document.getElementById("rating[2]").value);
  7.     document.getElementById("score[2]").value = rating2*5;
  8.  
  9.     var rating3 = parseInt(document.getElementById("rating[3]").value);
  10.     document.getElementById("score[3]").value = rating3*2;
  11.  
  12. //for the rest are same as above except the number to multiply
  13. }
  14.  
May 9 '10 #9
acoder
16,027 Expert Mod 8TB
With radio buttons you need to get all of them and loop over them to check which has been checked. You can use document.getElementsByName to get all radio buttons with the same name.
May 9 '10 #10
Now I can choose my radio button. So now where do I need to place the statement for textbox to display the value of radio button. Because I put as below, the number display at the different textbox, not the textbox that corresponding with the radio button when I select the radio button. Thanks in advance.
Expand|Select|Wrap|Line Numbers
  1. function calcRadio(radio, val)
  2. {    
  3.         var arr = new Array();
  4.         arr = document.getElementsByName(radio);
  5.  
  6.         for(var i = 0; i < arr.length; i++)
  7.         {
  8.             if(document.getElementsByName(radio).item(i).checked)
  9.             {
  10.                 var obj = document.getElementsByName(radio).item(i);    
  11.                 document.getElementById("score"+i).value = val / 5 * obj.value;
  12.             }        
  13.         }
  14. }
  15.  
May 9 '10 #11
acoder
16,027 Expert Mod 8TB
You'll probably be better off passing the ID of the text box to the function so you can use that instead of score+i
May 9 '10 #12
Sorry. I am not very clear. Can you show how I can do that? Thanks.
May 9 '10 #13
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. function calcRadio(radio, val, textID) {
where textID would be the ID of the text box, then you can use that in place of "score"+i on this line
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("score"+i).value = val / 5 * obj.value;
May 9 '10 #14
yes. I get it already. Now I want to add up all the value in the textbox. But nothing display in the new textbox.
Expand|Select|Wrap|Line Numbers
  1. var sum = 0; 
  2. for(var i=1;i<=7;i++) 
  3. sum += parseInt(document.getElementById("score"+i).value);
  4. document.getElementById(sum).value=sum;
  5.  
May 9 '10 #15
acoder
16,027 Expert Mod 8TB
You've used sum for the ID and the sum. Use a different variable name for one of the two.
May 9 '10 #16
The textbox remain display NaN until all the radio button were checked, then display the correct value. Why?
Expand|Select|Wrap|Line Numbers
  1. var sum = 0;  
  2. for(var i=1;i<=7;i++)  
  3. {  
  4. sum += parseInt(document.getElementById("score"+i).value); 
  5. }  
  6. document.getElementById('total').value=sum; 
  7.  
May 9 '10 #17
acoder
16,027 Expert Mod 8TB
Because the textboxes don't have a value. Either set them to 0 initially, or use isNaN (with parseInt) to avoid NaN* results.

*Not a Number
May 10 '10 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michael Albanese | last post by:
I am developing an application to handle my compay's OSHA reporting requirements. Some of the input criteria are technical and narowly defined, so I was trying to prvide what i call "Context...
2
by: Bart Verbeke | last post by:
Hi, I have a form with 2 radio buttons (ACCEPT/REJECT). When the page is initially loaded, no textbox should be visible. When a user clicks the REJECT radio button, a textbox should...
0
by: ana_gil | last post by:
Hello I need help whit a validation control. I have a radio button list which i made required: <asp:radiobuttonlist id="_AlergiasSN" runat="server"> <asp:listitem id="_AlergiasS"...
1
by: jw01 | last post by:
Im trying to write a java script in HTML in which when a particular radio button is selected, a textbox would appear write next to the selected item. e.g (RADIO BUTTON) fhd: (RADIO BUTTON) fdj:...
2
by: Eric Layman | last post by:
Hi, I have a radio button and a combo box. Upon changing a value for radio button, the combo box values will be dynamically updated. I have an idea on how to go abt doing it but Im stuck...
2
by: jw01 | last post by:
I changed my code...Now everytime i click a radio button, a text box appears...What i want is when i hit a particular radio button, the textbox should only appear next to that radio button. And if...
2
by: Peted | last post by:
I have 6 radio buttons in a column with a single line text box next to each rb. is there a general straightforward way to link a radio button to the textbox next to it. I want to be able to...
0
by: dancer | last post by:
Can somebody tell me why my RequiredFieldValidator works for the TEXT BOX, (location) but does NOT work for the RADIO BUTTON (EmpTrain)? <%@ Page Language="VB" ClientTarget="downlevel" %> ...
0
by: Gary W. Smith | last post by:
Hello, I have a simple form with a radio button group on it and I need to do some basic validation based upon which button is checked. Here is a little more detail. if button one is...
2
by: cpup22 | last post by:
I'm new to visual basic but have been playing around with extending the treenode functionalities. I found out how to make it so that after a user clicks on a node in a tree a windows object such as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.