Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GPA Calculator returns unwanted info to url bar 2

Status
Not open for further replies.

romeo23

Technical User
May 28, 2004
5
US
Hello all, I found a gpa calculator on the internet which works rather well. I'm having one minor annoyance though. Every time you hit the submit button it attaches a bunch of junk to the existing info in the url bar. Everytime, if it returns an error message and even if it goes through.

example of what it attaches to the url in the url bar:
Code:
 ?GR1=&CR1=&GR2=&CR2=&GR3=&CR3=&GR4=&CR4=&GR5=&CR5=&GR6=&CR6=&cumGPA=&cumCredits=&x=21&y=8

[highlight]Here is the code for the calculator.[/highlight]
Code:
<script language = "JavaScript">

<!-- 

// This gpa calculator script was written by 
// Matt Stueve, mstueve@blue.weeg.uiowa.edu
// Feel free to re-distribute, but please
// leave this notice in place


function round2d(n){return(.01* Math.round(100*n));}

function gpacalc()
{
//define valid grades and their values
var gr = new Array(); 
var cr = new Array();
var ingr = new Array();
var incr = new Array();

// define valid grades and their values
var grcount = 30; 
gr[1] = "A+";
cr[1] = 4;
gr[2] = "A"; 
cr[2] = 4; 
gr[3] = "A-";
cr[3] = 3.70;
gr[4] = "B+";
cr[4] = 3.30;
gr[5] = "B";
cr[5] = 3;
gr[6] = "B-";
cr[6] = 2.70;
gr[7] = "C+";
cr[7] = 2.30;
gr[8] = "C";
cr[8] = 2;
gr[9] = "C-";
cr[9] = 1.7;
gr[10] = "D+"
cr[10] = 1.3
gr[11] = "D";
cr[11] = 1;
gr[12] = "D-";
cr[12] = 0.7
gr[13] = "F";
cr[13] = 0;
gr[14] = "WF";
cr[14] = 0;
gr[15] = "WU";
cr[15] = 0;



gr[16] = "a+";
cr[16] = 4;
gr[17] = "a"; 
cr[17] = 4; 
gr[18] = "a-";
cr[18] = 3.70;
gr[19] = "b+";
cr[19] = 3.30;
gr[20] = "b";
cr[20] = 3;
gr[21] = "b-";
cr[21] = 2.70;
gr[22] = "c+";
cr[22] = 2.30;
gr[23] = "c";
cr[23] = 2;
gr[24] = "c-";
cr[24] = 1.7;
gr[25] = "d+"
cr[25] = 1.3
gr[26] = "d";
cr[26] = 1;
gr[27] = "d-";
cr[27] = 0.7
gr[28] = "f";
cr[28] = 0;
gr[29] = "wf";
cr[29] = 0;
gr[30] = "wu";
cr[30] = 0;



// retrieve user input
ingr[0] = document.GPACalcForm.GR1.value;
ingr[1] = document.GPACalcForm.GR2.value;
ingr[2] = document.GPACalcForm.GR3.value;
ingr[3] = document.GPACalcForm.GR4.value;
ingr[4] = document.GPACalcForm.GR5.value;
ingr[5] = document.GPACalcForm.GR6.value;
incr[0] = document.GPACalcForm.CR1.value;
incr[1] = document.GPACalcForm.CR2.value;
incr[2] = document.GPACalcForm.CR3.value;
incr[3] = document.GPACalcForm.CR4.value;
incr[4] = document.GPACalcForm.CR5.value;
incr[5] = document.GPACalcForm.CR6.value;

// Calculate GPA
var allgr = 0;
var allcr = 0;
var gpa = 0;
for (var x = 0; x < 6; x++)
        {
        if (ingr[x] == "") break;
//      if (isNaN(parseInt(incr[x]))) alert("Error- You did not enter a numeric credits value for Class If the class is worth 0 credits then enter the number 0 in the field."); 
        var validgrcheck = 0;
        for (var xx = 0; xx < grcount; xx++)
                {
                if (ingr[x] == gr[xx])
                        {
                        allgr = allgr + (parseInt(incr[x],10) * cr[xx]);
                        allcr = allcr + parseInt(incr[x],10);
                        validgrcheck = 1;
                        break;
                        }
                }
        if (validgrcheck == 0)
                {
                alert("Error- Could not recognize the grade entered for Class " + eval(x + 1) + ". Please use standard college grades in the form of a a- b+ ...f.");
                return 0;
                }
        }

// this if-check prevents a divide by zero error
if (allcr == 0)
        {
        alert("Error- You did not enter any credit values! GPA = N/A");
        return 0;
        }

gpa = round2d(allgr / allcr);

alert("Your semester GPA =  " + eval(gpa));

return 0;
}

function cumCalc()
{
//define valid grades and their values
var gr = new Array(); 
var cr = new Array();
var ingr = new Array();
var incr = new Array();

// define valid grades and their values
var grcount = 30; 
gr[1] = "A+";
cr[1] = 4;
gr[2] = "A"; 
cr[2] = 4; 
gr[3] = "A-";
cr[3] = 3.70;
gr[4] = "B+";
cr[4] = 3.30;
gr[5] = "B";
cr[5] = 3;
gr[6] = "B-";
cr[6] = 2.70;
gr[7] = "C+";
cr[7] = 2.30;
gr[8] = "C";
cr[8] = 2;
gr[9] = "C-";
cr[9] = 1.7;
gr[10] = "D+"
cr[10] = 1.3
gr[11] = "D";
cr[11] = 1;
gr[12] = "D-";
cr[12] = 0.7
gr[13] = "F";
cr[13] = 0;
gr[14] = "WF";
cr[14] = 0;
gr[15] = "WU";
cr[15] = 0;



gr[16] = "a+";
cr[16] = 4;
gr[17] = "a"; 
cr[17] = 4; 
gr[18] = "a-";
cr[18] = 3.70;
gr[19] = "b+";
cr[19] = 3.30;
gr[20] = "b";
cr[20] = 3;
gr[21] = "b-";
cr[21] = 2.70;
gr[22] = "c+";
cr[22] = 2.30;
gr[23] = "c";
cr[23] = 2;
gr[24] = "c-";
cr[24] = 1.7;
gr[25] = "d+"
cr[25] = 1.3
gr[26] = "d";
cr[26] = 1;
gr[27] = "d-";
cr[27] = 0.7
gr[28] = "f";
cr[28] = 0;
gr[29] = "wf";
cr[29] = 0;
gr[30] = "wu";
cr[30] = 0;


// retrieve user input
ingr[0] = document.GPACalcForm.GR1.value;
ingr[1] = document.GPACalcForm.GR2.value;
ingr[2] = document.GPACalcForm.GR3.value;
ingr[3] = document.GPACalcForm.GR4.value;
ingr[4] = document.GPACalcForm.GR5.value;
ingr[5] = document.GPACalcForm.GR6.value;
incr[0] = document.GPACalcForm.CR1.value;
incr[1] = document.GPACalcForm.CR2.value;
incr[2] = document.GPACalcForm.CR3.value;
incr[3] = document.GPACalcForm.CR4.value;
incr[4] = document.GPACalcForm.CR5.value;
incr[5] = document.GPACalcForm.CR6.value;

// Calculate GPA
var allgr = 0;
var allcr = 0;
var gpa = 0;
for (var x = 0; x < 6; x++)
        {
        if (ingr[x] == "") break;
//      if (isNaN(parseInt(incr[x]))) alert("Error- You did not enter a numeric credits value for Class If the class is worth 0 credits then enter the number 0 in the field."); 
        var validgrcheck = 0;
        for (var xx = 0; xx < grcount; xx++)
                {
                if (ingr[x] == gr[xx])
                        {
                        allgr = allgr + (parseInt(incr[x],10) * cr[xx]);
                        allcr = allcr + parseInt(incr[x],10);
                        validgrcheck = 1;
                        break;
                        }
                }
        if (validgrcheck == 0)
                {
                alert("Error- Could not recognize the grade entered for Class " + eval(x + 1) + ". Please use standard college grades in the form of a a- b+ ...f.");
                return 0;
                }
        }

// this if-check prevents a divide by zero error
if (allcr == 0)
        {
        alert("Error- You did not enter any credit values! GPA = N/A");
        return 0;
        }

gpa = allgr / allcr;


// set variables
var cumGPAnum = 0;
var cumCredTotal = 0;

// retrieve user input
cumGPAnum = parseFloat(document.GPACalcForm.cumGPA.value);
cumCredTotal = parseFloat(document.GPACalcForm.cumCredits.value);

if (document.GPACalcForm.cumGPA.value == "") 
   {  alert("You need to enter your previous cumulative gpa in order to calculate your new one.");
            return 0;
   }
   if (document.GPACalcForm.cumCredits.value == "") 
   {  alert("You need to enter the total number of credits you earned prior to this semester.");
            return 0;
   }
   if (document.GPACalcForm.cumGPA.value == gpa) 
   {  if (confirm("Are you sure you are entering your previous cumulative GPA, and not your new semester GPA?" +
                  " Hit OK if your sure, otherwise hit Cancel to change your values."))
      {}
      else
            return 0;
   }

// find total points
var points = 0;
points = (cumGPAnum * cumCredTotal);

// Add semester values to cumulative
points = points + allgr;
var credits = 0;
credits = cumCredTotal + allcr;

// Calculate new cumulative gpa
var newCum = 0;
newCum = round2d(points / credits);

alert("Your new cumulative GPA is " + eval(newCum));


return 0;
}

function validateForm(form) 
{
   if (form.cumGPA.value == "") 
   {  alert("You need to enter your previous cumulative gpa in order to calculate your new one.");
            return false;
   }
   if (form.cumCredits.value == "") 
   {  alert("You need to enter the total number of credits you earned prior to this semester.");
            return false;
   }
   if (form.cumGPA.value == form.gpa.value) 
   {  confirm("Are you sure you are entering your cumulative gpa, and not your new semester gpa?");
   }
   
  else 
   {  return true;
   }
}


//-->
    
    
</script>
</p>
                    <form name = "GPACalcForm">
                      <table width = "100%" cellspacing = "1" cellpadding = "5" border = "0" bgcolor = "#727272">
	<tr bgcolor = "#fffff">
                          <td valign = top align = middle bgcolor = #a6a6a6><b>Current<br>
                            Semester</b></font></td>
                          <td valign = top align = middle bgcolor = #a6a6a6><b>Letter<br>
                            Grade</b></font></td>
                          <td valign = top align = middle bgcolor = #a6a6a6><b># Credits<br>
                            for Class</b></font></td>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>1st 
                            Class</font></td>
                          <td align = middle bgcolor = "#ffffff"> 
                            <input maxlength = 5 align = top size = 5 name = GR1>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR1>
                          </td>
                        </tr>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>2nd 
                            Class</font></td>
                          <td align = middle bgcolor = "#ffffff"> 
                            <input maxlength = 5 align = top size = 5 name = GR2>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR2>
                          </td>
                        </tr>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>3rd 
                            Class</font></td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = GR3>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR3>
                          </td>
                        </tr>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>4th 
                            Class</font></td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = GR4>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR4>
                          </td>
                        </tr>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>5th 
                            Class</font></td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = GR5>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR5>
                          </td>
                        </tr>
                        <tr bgcolor = "#ffffff"> 
                          <td align = middle>6th 
                            Class</font></td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = GR6>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = CR6>
                          </td>
                        </tr>
                        <tr align = middle bgcolor = "ffffff"> 
                          <td colspan = "3" align = "right"> 
                            <input type = "image" src = "/images/calculate.gif" width = "50" height = "19" border = "0" alt = "Calculate" onClick = "gpacalc()">
						</td>
                        </tr>
                        <tr bgcolor = "ffffff"> 
                          <td align = middle rowspan = 2><b>Prior 
                            to<br>
                            Current Semester:</b></font></td>
                          <td align = middle><b>Cum. 
                            GPA</b></font></td>
                          <td align = middle><b>Cum. 
                            Credits</b></font></td>
                        </tr>
                        <tr bgcolor = "ffffff"> 
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 name = cumGPA>
                          </td>
                          <td align = middle> 
                            <input maxlength = 5 align = top size = 5 
name = cumCredits>
                          </td>
                        </tr>
                        <tr align = "middle" bgcolor = "ffffff"> 
                          <td colspan = 3 align = "right"> 
                            <input type = "image" src = "/images/calculate.gif" width = "50" height = "19" border = "0" alt = "Calculate" onClick = "cumCalc()">
							
                          </td>
                        </tr>
                        <tr align = middle bgcolor = "#ffffff"> 
                          <td colspan = 3 align = "right"> 
                            <a onclick = "document.GPACalcForm.reset();return false;"
							href = "#"><img alt = "Clear" src = "/images/reset.gif" border = "0"></a>
                          </td>
                        </tr>
                        </tbody> 
                      </table>
                    </form>

How can I get the calculator to work the same way but not return all that stuff to the url bar.
 
Try changing

Code:
<a onclick = "document.GPACalcForm.reset();return false;"
href = "#">

to

Code:
<a onclick = "document.GPACalcForm.reset();return false;"
href = "[b]javascript:void(0)[/b]">

The '#' can screw things up.

'hope that helps.

--Dave
 
thanks for the post dave but it didn't seem to help. thanks
 

You need to add the following to your form tag:

Code:
action="post"

As far as I know, the default action is "get", which does what you see - attaching the form elements and values to the URL. Using the post method does not do this, but you will need server-side code to pick up on the form values.

Hope this helps,
Dan
 

You need to add the following to your form tag:

Code:
method="post"

As far as I know, the default method is "get", which does what you see - attaching the form elements and values to the URL. Using the post method does not do this, but you will need server-side code to pick up on the form values.

Hope this helps,
Dan
 

Aah rats - ignore my first post - I didn't hit Esc quick enough... "method" is the correct attribute to use ;o)

Dan
 
That did it Dan, thanks for lending your eyes to look through that code. I gave you a star. :)
 
You still might want to consider changing the anchor-href I pointed out. Notice what happens to your URL when you click the link with href='#' in it.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top