SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new to javascript, just want to take a value from a form and save it or print it on t

    Just want to take information a user selects in a form and either save it of print it on the client side. any help would be greatly appreciated. This is what I have pieced together so far...


    HTML Code:
    <html>
    <head>
    
    <Script language= "Javascript">
    <!--
    var anxiety= new Array()
    anxiety[0]="No";
    anxiety[1]="mild";
    anxiety[2]="Moderate";
    anxiety[3]="High";
    anxiety[4]="affected by substance use or abuse";
    //-->
    var anxietyprint="not assessed";
    
    
    function set_anxiety()
    {
    
    }
    </script>
    
    <script language=text/javascript">
    
    function assign_value()
    {
    if (anxiety.value=="no")
    	{
    	anxietyprint='Client presented with no anxiety';
    	}
    else
    	if (anxiety.value=="mild")
    	{
    	anxietyprint='Client presented with mild anxiety';
    	}
    else
    	if (anxiety.value=='moderate')
    	{
    	anxietyprint="Client presented with moderate anxiety";
    	}
    else
    	if (anxiety.value=='high')
    	{
    	anxietyprint="Client presented with high anxiety";
    	}
    else
    	if (anxiety.value=='affected')
    	{
    	anxietyprint="Client presented with anxiety affected by substance use or abuse";
    	}
    { 
    document.write(anxietyprint);
    }
    
    }
    
    </script>
    
    <P>
    
    </head>
    <body>
    
    <FORM name="anxiety">
    Level of anxiety noted:<br>
    <input type="radio" name="anx" value="no" /> NO<br />
    <input type="radio" name="anx" value="mild" />MILD<br />
    <input type="radio" name="anx" value="moderate" />MODERATE<br />
    <input type="radio" name="anx" value="high" />HIGH<br />
    <input type="radio" name="anx" value="affected" />AFFECTED BY SUBSTANCE USE<br />
    </form> 
    <button type="button" onClick="assign_value()">Assign Anxiety</button>
    
    </body>
    Last edited by Mittineague; Mar 16, 2011 at 15:55. Reason: reformatting bbcode tags

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Location
    Bs. As. - Argentina
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if i understand the basis of what are you trying to do, the code should be something like that

    Code HTML4Strict:
    <html>
    <head>
     
    <Script language= "Javascript">
    var anxiety= new Array()
    anxiety[0]="No";
    anxiety[1]="mild";
    anxiety[2]="Moderate";
    anxiety[3]="High";
    anxiety[4]="affected by substance use or abuse";
     
    var anxietyprint="not assessed";
     
    function set_anxiety()
    {  
       //use?
    }
     
    function assign_value()
    {
        var anxCol = document.getElementsByName("anx");
        var i = 0;
        var o = null;
        for (i=0;i<(anxCol.length-1);i++){
              //looking for the radio button checked
             var o = anxCol[i];
             if (o.checked){
                 //ok, here it is
                 break;
             }
             o = null;
        }
     
        if (o == null){
          //so, none checked ?
    window.alert ("no one selected");
        }else{
            switch(o.value){
                case ("no"):
                     anxietyprint='Client presented with no anxiety';
                break;
     
                case ("mild"):
                     anxietyprint='Client presented with mild anxiety';
                break;
     
                case ("moderate"):
                     anxietyprint="Client presented with moderate anxiety";
                break;
     
                case ("high"):
                     anxietyprint="Client presented with high anxiety";
                break;
     
                case ("affected"):
                      anxietyprint="Client presented with anxiety affected by aubstance use or abuse";
                break;
     
                default:
                    //something else
                     anxietyprint=" something else?";
                break;
            }
            document.write(anxietyprint);
        }
    }    
    </script>
    <P>
    </head>
    <body>
    <FORM name="anxiety">
    Level of anxiety noted:<br>
    <input type="radio" name="anx" value="no" /> NO<br />
    <input type="radio" name="anx" value="mild" />MILD<br />
    <input type="radio" name="anx" value="moderate" />MODERATE<br />
    <input type="radio" name="anx" value="high" />HIGH<br />
    <input type="radio" name="anx" value="affected" />AFFECTED BY SUBSTANCE USE<br />
    </form> 
    <input type="button" onclick="assign_value()" value="Assign Anxiety">
    </body>
    </html>

    Note the "input" tag instead of the "button" tag and the "value" of it.

    Things i do not get understood are the use of the array "anxiety[]" and the function "set_anxiety()" guess you are still codding the page.

    See you

  3. #3
    SitePoint Addict AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did something similar, but the output is to a textbox.

    Code :
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     
    <head>
    <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>Radio Buttons</title>
    <script type="text/javascript">
    <!--
    function assign_value()
    { var i;
      var anxIndex=null, anxietyprint;
      for(i=0; i<dfa.anx.length; i++)
       { if(dfa.anx[i].checked==true){ anxIndex=document.anxiety.anx[i].value; }       
       }
    //   
    switch(anxIndex){
     case "no": anxietyprint='Client presented with no anxiety'; break;
     case "mild": anxietyprint='Client presented with mild anxiety'; break;
     case "moderate": anxietyprint="Client presented with moderate anxiety"; break;
     case "high": anxietyprint="Client presented with high anxiety"; break;
     case "affected": anxietyprint="Client presented with anxiety affected by substance use or abuse"; break;
     default: anxietyprint="No entry was selected";
    } 
    //
    document.anxiety.T1.value=anxietyprint;
    }
    // --------
    var dfa; // global shortcut to form
     window.onload=function(){ dfa=document.getElementById("myForm"); }
    //-->
    </script>
    <style type="text/css">
    <!--
    body, form, input { font-family:arial, helvetica, sans-serif; color:#000; font-size:13px; font-weight:bold; } 
    input { color:#000080; font-size:14px; }
    p { margin-top:0px; Margin-bottom:3px; }
    -->
    </style>
    </head>
     
    <body>
     
    <form id="myForm" name="anxiety" method="get" action>
      <p>Level of anxiety noted:</p>
      <p><input type="radio" name="anx" value="no">NONE</p>
      <p><input type="radio" name="anx" value="mild">MILD</p>
      <p><input type="radio" name="anx" value="moderate">MODERATE</p>
      <p><input type="radio" name="anx" value="high">HIGH</p>
      <p><input type="radio" name="anx" value="affected">AFFECTED BY SUBSTANCE USE</p>
      <p><br>
      <input type="text" name="T1" value size="70"></p>
    </form>
    <p><button type="button" onClick="assign_value()">Assign Anxiety</button></p>
     
    </body>
     
    </html>

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thank you Pablogo and AllanP. What you whipped out in 2 minutes I have been fighting with all day. Kindest Regards...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •