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;
	alert('tot_points');
	return false;
}

</script>

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

Regards
Scorpionz

Member Avatar for langsor

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

<?php
$i = '_foo';
?>
<html>
<head>
<script type="text/javascript">

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

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

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.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.