i've been scratching my head for an hour and i nailed it down to this code not working because the function call is inside the conditional statements. when i take them out, it works fine. how do i get it to work? all this does is calculate the sum of the first 2 inputs' value via user input using the "onchange" attribute in the input tag, which then calls updatesum() and spits it out to the third input. example of my code... <script type="text/javascript"><!-- function updatesum() { document.form.sum.value = (document.form.qty1$uniq_ID.value -0) + (document.form.qty2$uniq_ID.value -0); } //--></script> </head> <body bgcolor="#ffffff"> <?php $stock1 = 1; $stock2 = 2; echo "<form name='form' > Enter a number: "; if ($stock1 == 0){ echo "<input readonly='readonly' type='text' name='qty1$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } else { echo "<input type='text' name='qty1$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } if ($stock2 == 0){ echo " Another number: <input readonly='readonly' type='text' name='qty2$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } else { echo " Another number: <input type='text' name='qty2$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } echo " Their sum is: <input name='sum' readonly style='border:0px;'> </form>"; ?> Code (markup):
the problem is in $uniq_ID in third line in (document.form.qty1$uniq_ID.value -0) you cant write it like that , it's not a php script , there you go : <script type="text/javascript"><!-- function updatesum() { document.form.sum.value = (document.form.qty1<?=$uniq_ID;?>.value -0) + (document.form.qty2<?=$uniq_ID;?>.value -0); } //--></script> </head> <body bgcolor="#ffffff"> <?php $stock1 = 1; $stock2 = 2; echo "<form name='form' > Enter a number: "; if ($stock1 == 0){ echo "<input readonly='readonly' type='text' name='qty1$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } else { echo "<input type='text' name='qty1$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum();'/>"; } if ($stock2 == 0){ echo " Another number: <input readonly='readonly' type='text' name='qty2$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } else { echo " Another number: <input type='text' name='qty2$uniq_ID' value='0' size='1' maxlength='3' onChange='updatesum()'/>"; } echo " Their sum is: <input name='sum' readonly style='border:0px;' > </form>"; PHP:
<html> <head> <script type="text/javascript"> function updatesum(){document.getElementById('result').value = parseInt(document.getElementById('one').value) + parseInt(document.getElementById('two').value);} </script> </head> <body> <?php $stock1 = 2; $stock2 = 4; $readonly1 = $stock1 == 0 ? "readonly=\"readonly\"" : ""; $readonly2 = $stock2 == 0 ? "readonly=\"readonly\"" : ""; echo "<p><input ".$readonly1." name=\"one\" id=\"one\" value=\"0\" onchange=\"updatesum()\"/></p>"; echo "<p><input ".$readonly2." name=\"two\" id=\"two\" value=\"0\" onchange=\"updatesum()\"/></p>"; echo "<p><input readonly=\"readonly\" name=\"result\" id=\"result\" value=\"0\"/></p>"; ?> </body> </html> PHP:
(document.form.qty1<?=$uniq_ID;?>.value -0) Code (markup): that still does not work, but in your 2nd example, getting the id instead of the stupid variable name worked like a charm. thanks dude!
You can solve that problem using this code:-- <script type="text/javascript"><!-- function updatesum() { document.getElementById("Sum").value = parseInt(document.getElementById("No1").value) + parseInt(document.getElementById("No2").value) } //--></script> </head> <body bgcolor="#ffffff"> <?php $stock1 = 1; $stock2 = 2; watch for more info at cattechnologies.com