Nida Zubair
Nida Zubair

Reputation: 23

Getting Values From Form And Using It In JavaScript Function

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...

Upvotes: 0

Views: 6755

Answers (3)

ajax333221
ajax333221

Reputation: 11754

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);

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

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.

Upvotes: 0

Marc
Marc

Reputation: 11613

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'));

Upvotes: 0

Related Questions