Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Damned Radio buttons...

  • 27-11-2000 1:23pm
    #1
    Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭


    I have a html form and am trying to take in the info through javascript, and the only part of the form I have trouble with is taking info in from the radion button, option thingamajiggies, so does anyone have any experience with taking in info with radio buttons, if so how is it supposed to be done. If you need to see my code to point out my errors let me know.


Comments

  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    It can be a pain in the rear...

    The radio group all need to have the same name but different values. e.g.
    <form name="myform" action="" method="post">
      <input type="option" name="group1" value="1">
      <input type="option" name="group1" value="2">
      ...etc...
    </form>
    

    So, in Java-Script, it'll be referenced with document.myform.group1.value.

    Hope that helps...



    All the best,
    kharn_sig.gif


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Well here are some very small snippets from the code. This is the same as it was before the reply and I think that I have done it exactly the way you said but it is still not working. It is not anything to do with the missing document. part because all other inputs in the form are referenced without the document. part. I am getting ****ed of with the stupid thing now so all help would be greatly appreciated.
    //How I reference it
    sex = parseInt(details.gender.value);
            
    //How I coded it
            <TR>
    
              <TD ROWSPAN="2">Gender:</TD>
              <TD>Male <INPUT TYPE="radio" 
    
    NAME="gender" VALUE="1"></TD>
    
            </TR>
    
            <TR>
    
              <TD>Female <INPUT TYPE="radio" NAME="gender" VALUE="2"></TD>
    
            </TR>
    


    Details being the name of the form of course
    [This message has been edited by Baz_ (edited 27-11-2000).]

    [This message has been edited by Baz_ (edited 27-11-2000).]


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    I am not getting an error that's the most frustrating part, it's just that when the piece of code executes which works on the variable sex, it doesn't seem to do what it is supposed to. I have included the whole code here so that you can get a more complete idea of what I am trying to achieve. Which is basically to print out a message. I just wish to add that such a trivial thing as this is driving me nuts, and I would really really really appreciate help on this.

    ta... especially to kharn and enygma
    <HTML>
      
      <HEAD>
        
       <TITLE>Ex4Q4.html</TITLE>
    
        <SCRIPT LANGUAGE="Javascript">
    
          function printMessage()
          {
    
            var birthyear, birthmonth, birthday, lastname, sex;
    	var secondPart = "", thirdPart = "", fourthPart = "", firstPart = "";
    
            birthyear = parseInt(details.yearob.value);
            birthmonth = parseInt(details.monthob.value);
            birthday = parseInt(details.dayob.value);
            lastname = details.surname.value;
            sex = parseInt(details.gender.value);
    
            switch (sex)
            {
              case 2: 
              {
                firstPart = "Hello Mrs " + lastname;
                break;
              }
              case 1:
              {
                firstPart = "Hello Mr " + lastname;
                break;
              }
            }
    
    	switch (birthday)
            {
              case 1:
              case 21:
              case 31:
              {
                secondPart = "st";
                break;
              }
    
              case 2:
              case 22:
              {
                secondPart = "nd";
                break;
              }
           
              case 3:
              case 23:
              {
                secondPart = "rd";
                break;
              }
    
              default:
              {
                secondPart = "th";
                break;
              }          
            } 
    
            secondPart = "You were born on the " + birthday + secondPart + " day of ";
    
            switch(birthmonth)
            {
              case 1:
              {
                thirdPart = "January";
                break;
              }  
              case 2:
              {
                thirdPart = "February";
                break;
              }   
              case 3:
              {
                thirdPart = "March";
                break;
              }   
              case 4:
              {
                thirdPart = "April";
                break;
              }   
              case 5:
              {
                thirdPart = "May";
                break;
              }  
              case 6:
              {
                thirdPart = "June";
                break;
              }   
              case 7:
              {
                thirdPart = "July";
                break;
              }   
              case 8:
              {
                thirdPart = "August";
                break;
              }   
              case 9:
              {
                thirdPart = "September";
                break;
              }   
              case 10:
              {
                thirdPart = "October";
                break;
              }   
              case 11:
              {
                thirdPart = "November";
                break;
              } 
              case 12:
              {
                thirdPart = "December";
                break;
              }  
            }
     
            fourthPart = " 19" + birthyear;
    
            details.message.value = firstPart + secondPart + thirdPart + fourthPart;   
         
    
    
          }
    
        </SCRIPT>
    
      </HEAD>
    
      <BODY>
    
        <FORM NAME="details">
    
          <TABLE BORDER="1">
    
            <TR>
    
              <TD>Surname</TD>
              <TD><INPUT TYPE = "text" NAME = "surname"></TD>
    
            </TR>
    
            <TR>
    
              <TD>Year of Birth</TD>
              <TD><INPUT TYPE = "text" NAME = "yearob" SIZE="2"></TD>
    
            </TR>
    
            <TR>
    
              <TD>Month of Birth</TD>
              <TD><INPUT TYPE = "text" NAME = "monthob" SIZE="2"></TD>
    
            </TR>
    
            <TR>
    
              <TD>Day of Birth</TD>
              <TD><INPUT TYPE = "text" NAME = "dayob" SIZE="2"></TD>
    
            </TR>
    
            <TR>
    
              <TD ROWSPAN="2">Gender:</TD>
              <TD>Male <INPUT TYPE="radio" NAME="gender" VALUE="1"></TD>
    
            </TR>
    
            <TR>
    
              <TD>Female <INPUT TYPE="radio" NAME="gender" VALUE="2"></TD>
    
            </TR>
    
            <TR>
    
              <TD><INPUT TYPE="button" VALUE="View Message" ONCLICK="printMessage()"></TD>
    
            </TR>
    
            <TR>
    
              <TD COLSPAN="2"><INPUT TYPE="text" NAME="message" SIZE="40"></TD>
    
            </TR>
    
          </TABLE>
        </FORM>
    
    <!--VALUE="1"-->
    <!--sex = parseInt(details.gender.value);-->
    
      </BODY>
    </HTML>
    


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    You need to treat the radio buttons as an array i.e.:
    details.gender[0].value -> the value of the male radio button
    details.gender[1].value -> the value of the female button etc.

    This code might not come out great but it works:
    function printMessage()
    {
       var birthyear, birthmonth, birthday, lastname, sex;
       var secondPart; 
       var thirdPart;
       var fourthPart;
       var firstPart;     
    
       birthyear = parseInt(details.yearob.value);
    
       birthmonth = parseInt(details.monthob.value);        
    
       birthday = parseInt(details.dayob.value);
    
      lastname = document.details.surname.value;        
     
       var male = details.gender[0].value;
       var female = details.gender[1].value;     
    
       if (male == 1) {
          firstPart = "Hello Mr " + lastname;
       } else if (female == 2) {
          firstPart = "Hello Mrs " + lastname;
       }
    
    .
    .
    . 
    etc.
    
    }
    

    Enygma

    =============
    Oh and you'll need to make it Y2K compliant too wink.gif

    [This message has been edited by Enygma (edited 28-11-2000).]


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Try it without the parseInt, or else try Integer.parseInt(document.details.gender.value);
    Although you should get away with keeping it as a String.

    What exactly is the error you're getting?



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Ta enygma worked like a charm, very helpful you've been. Also it was just a class exercise and when they start paying me to do 'em then I will start doing them properly (Y2K compliancy and all that).

    Anyway ta.


  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    DOH! Spotted Enygma - I forgot about the Array elements bit smile.gif



    All the best,
    kharn_sig.gif


Advertisement