473,326 Members | 2,125 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,326 software developers and data experts.

Submitting Radio button results & getting result displayed on screen.

135 100+
Currently when I select one of the radio buttons and then press submit, the answer which is alerted is always 'false', even if i press 'true', I still get 'false' alerted. What I'm i doing wrong? Please see code below:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4.  
  5. function get_results() {
  6.  
  7.     var answer=document.getElementById("question").value;
  8.     alert(answer);
  9.  
  10.    if (answer==true) {
  11.     alert("the answer was TRUE");
  12.    }
  13.    else {
  14.        alert("answer was false for question one");
  15.    }
  16. }
  17.  
  18. </script>
[HTML]</head>
<body>
<h2 align=center>The Personality Questionnaire</h2><br><br>

1) Do you browse through railway timetables, directories, or dictionaries just for pleasure?
<input type=radio name="question" value="false">False
<input type=radio name="question" value="true">True
<br><br>


<button type="submit" id="submit" value="Submit"onClick="get_results()">Submit</button>

</body>
</html>[/HTML]
Aug 25 '07 #1
4 2649
patelxxx
135 100+
the code resolved the issue:

Expand|Select|Wrap|Line Numbers
  1. var answer=document.getElementById("question").checked;
Aug 25 '07 #2
patelxxx
135 100+
How can I reduce the following code, basically I have 4 questions (questionnaire) on my website and users have to select the correct answer via Radio Buttons and then user presses submit button which invokes the get_results() function.

How can i reduce the following code my using (this.checked) in the submit button?


Expand|Select|Wrap|Line Numbers
  1. function get_results() {
  2.  
  3.     var answer=document.getElementById("question").checked;
  4.     alert("Answer ONE: "+answer);
  5.  
  6.     var answertwo=document.getElementById("questiontwo").checked;
  7.     alert("Answer TWO: "+answertwo);
  8.  
  9.                 var answerthree=document.getElementById("questionthre").checked;
  10.     alert("Answer THREE: "+answerthree);
  11.  
  12.                 var answerfour=document.getElementById("questionfour").checked;
  13.     alert("Answer FOUR: "+answerfour);
  14.  
Aug 25 '07 #3
acoder
16,027 Expert Mod 8TB
One thing that is still incorrect is that you've only specified a name and you're using document.getElementById(id) to access the element. Use document.getElementsByName instead.
Aug 26 '07 #4
acoder
16,027 Expert Mod 8TB
As I mentioned in another thread, if you've specified a name, you can't use it as an id unless you also specify an id. However, in the case of radio buttons and checkboxes, this cannot work because you have more than one checkbox/radiobutton with the same name and an id has to be unique.

Try replacing the lines with another function:
Expand|Select|Wrap|Line Numbers
  1. function displayans(radioName, num) {
  2.   var alertStr = "Answer " + num + ":";
  3.   var radios = document.getElementsByName("question");
  4.   if (radios[0].checked) alertStr += radios[0].value;
  5.   else alertStr += radios[1].value;
  6. }
  7.  
then in get_results():
Expand|Select|Wrap|Line Numbers
  1. function get_results() {
  2.   displayans("question","ONE");
  3.   displayans("questiontwo","TWO");
  4.   ...
  5. }
You could make this even more compact, but this is a start.
Aug 26 '07 #5

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

Similar topics

1
by: David | last post by:
Hi, May I know how to create data dynamically in a combo box after clicking on a radio button ? Data are retrieved using a store procedure from a SQL Server. For example : Click Radio Button...
5
by: Tyler Style | last post by:
Hullo - looking for a little advice here. I have a form on a page in one domain submitting to a cgi in another domain. Weirdly, on some Windows XP systems, a form on the page fails to submit/post...
16
by: browntown | last post by:
so I have this application I'm nearly finished with. The only thing the client has requested is the ability to submit the form by pressing "enter". I didn't think this would be a huge pain in the...
7
by: IchBin | last post by:
I am trying to programmatically set a radio button in a table of radio buttons. I have two problems with the code below: 1 - I can not prepare the <Formstatement to be printed by php. (syntax...
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...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
6
by: dba | last post by:
using the following code with a problem.... echo "<input type='hidden' name='member_id' value=\"{$row}\">{$row}"; echo "<input type='radio' name='member_name' value=\"{$row}\">{$row}<br />"; ...
11
by: Twayne | last post by:
Hi, Newbie to PHP here, no C or other relevant background, so pretty niave w/r to the nuances etc. but I think this is pretty basic. XP Pro, SP2+, PHP 4.4.7, XAMPP Local Apache Server...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.