0

I am having difficulty in simple program...

I have a simple html form from which i get 3 values as integers and then they are submitted to a JavaScript function using this line This is the function that i am using

<script language="javascript">
function Secured_Rescaled_Marks(form)
{
    var address = form.total_marks.value;
    window.alert(address);  
    //var secured = (rescale_marks/total_marks)*secured_marks;
    //alert('secured');
}
</script>

and this is the form

<form>
<table cellpadding="3">
<tbody>
<tr>
<td bgcolor="#6699CC">
<b>Total Marks:</b>
<input name="text" name="total_marks" />
</td>
</tr>
</tbody>
</table>
</br>
<table cellpadding="3">
<tbody>
<tr>
<td bgcolor="#6699CC">
<b>Secured Marks:</b>
<input name="text" name="secured_marks">
</td>
</tr>
</tbody>
</table>
</br>
<table cellpadding="3">
<tbody>
<tr>
<td bgcolor="#6699CC">
<b> Rescale Marks:</b>
<input name="textarea" name="rescale_marks">
</td>
</tr>
</tbody>
</table>
</br>
    <input type="button" onsubmit="return Secured_Rescaled_Marks(this);" name="order" value="Secured_Rescaled_Marks??"/>

<input type="reset" value="Reset">
</form>

I need to calculate such formula on form submission and alert the result....

var secured = (rescale_marks/total_marks)*secured_marks;

Any help will be appreciated...

2
  • 2
    You're not showing us your function or all pertinent HTML, so it's a little tough to find your issue. Show us what you're trying! Commented Apr 28, 2012 at 15:03
  • 1
    And show us THAT you are trying: make an effort to understand the question guidelines: stackoverflow.com/questions/how-to-ask Commented Apr 28, 2012 at 15:05

3 Answers 3

1

Firstly, you should give your form a name, then you can use one of these common methods:

document.FormName.elements["element_name"].value;//select by name
//or
document.FormName.elements[index].value;//select by index position

Note: FormName is the form name in the example

source and demo

And then to use this value in your function, you need to transform it from string to number like this:

var val = document.FormName.elements["element_name"].value;
val = parseFloat(val);
Sign up to request clarification or add additional context in comments.

Comments

0

Your input tags have two names, i.e.:

<input name="text" name="total_marks" />

Should be:

<input type="text" name="total_marks" id="total_marks"/>

I added an ID for you, because with an ID, you can get your values using document.getElementById('total_marks');

And when you do your math after retrieving your values, you should make sure they're numbers (in your case, integers), i.e.:

var tmarks = parseInt(document.getElementById('total_marks'));

Comments

0

You have some errors on your form (fixed)

<form name="myform" onsubmit="return Secured_Rescaled_Marks(this);">
<table cellpadding="3">
   <tbody>
    <tr>
     <td bgcolor="#6699CC">
       <b>Total Marks:</b>
       <input type="text" name="total_marks" />
     </td>
    </tr>
   </tbody>
</table>
</br>
<table cellpadding="3">
    <tbody>
     <tr>
      <td bgcolor="#6699CC">
       <b>Secured Marks:</b>
       <input type="text" name="secured_marks">
      </td>
     </tr>
    </tbody>
 </table>
 </br>
 <table cellpadding="3">
  <tbody>
   <tr> 
    <td bgcolor="#6699CC">
     <b> Rescale Marks:</b>
      <input type="text" name="rescale_marks">
    </td>
   </tr>
  </tbody>
 </table>
 </br>
 <input type="submit" name="order" value="Secured_Rescaled_Marks??"/>
 <input type="reset" value="Reset">
</form>​

JS <script language="javascript"> should be <script type="text/javascript">

function Secured_Rescaled_Marks(frm)
{
    var rescale_marks=parseFloat(frm.rescale_marks.value);
    var total_marks=parseFloat(frm.total_marks.value);
    var secured_marks=parseFloat(frm.secured_marks.value);
    var secured = (rescale_marks/total_marks)*secured_marks;
    if(isNaN(secured)) return false;
    if(confirm(secured)) 
    {
        this.submit();
        return true;
    }
    return false; 
}​

DEMO.

3 Comments

Things your are missing: the script tag is bogus (language attribute, wHAT?)
The computation needs to be done in floating point arithmetic.
The form is passed as a parameter to the event handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.