Hi this is scorpionz here

I am working on some exams stuff...
Now the thing i want to mention here is that: a text boxes created by php code, means a text boxes shaped in this way:

<input type="text" name="total_points<?=$i?>" style="width:50px;" >

Now these are created severals. I have a button here which i created in this way:

<input type="submit" name="calculate_points" value="Submit" onclick="return totalpoints_calculation()" />

when i press button i need to calculate the points that i input in the text boxes. and mention that result in alert box.
here is the function i started

<script type="text/javascript">

function totalpoints_calculation()
	//var	tot_points	=	document.questioncreationform.total_points<?=$i?>.value;
	return false;


I am unable to create a javascipt to calculate the total points in boxes.

Any Response will be highly appreciated with great interest
Thanks in Advance....


10 Years
Discussion Span
Last Post by langsor

There's dozens of ways to get the text value from the form field to the javascript ... this is one simple way.

$i = '_foo';
<script type="text/javascript">

function totalpoints_calculation () {
  var tot_points = document.getElementById('<?=$i?>').value;

  <input id="<?=$i?>" type="text" name="total_points<?=$i?>" style="width:50px;" />
  <input type="button" name="calculate_points" value="Submit" onclick="totalpoints_calculation()" />

The Submit button can or not be a 'submit' type button or just a regular button.

You could also parse through the entire form, but this way you don't really need a <form> wrapper.

This method assumes each php variable is unique thus using an 'id' to get the html element.

Let me know if your situation needs a different approach.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.