By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Javascript combined with php. (works in explorer but not in firefox) getElementById

P: 3
Dear community,


I created a small form witch is supposed to automatically calculate the price via onKeyUp=()

and in explorer all is working fine but in explorer the script goes exactly untill the following line.
document.getElementById(naam+"txt").value='';
after that the script quits no error it just stops.

I'll try to make the code as short as possible
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Index pagina</title>
  4. <script type="text/javascript">
  5. //small script to see if it is a number or not
  6. function is_int(value){
  7.   if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
  8.       return true;
  9.   } else {
  10.       return false;
  11.   }
  12. }
  13. //calculate the total price
  14. function totaal()
  15.   var prijs= 0;
  16.   <?php 
  17.  $X= array("spagGR","* Spaghetti","-groot:",7.5);
  18.   ?>
  19.       prijs = prijs+(<?php echo $X[3];?>* document.getElementById("<?php echo $X[0]; ?>txt").value);
  20.  
  21.  
  22.   document.getElementById("totaal").innerHTML=prijs;
  23.   return;
  24. }
  25. function berekenen(str, prijs, naam)
  26. {
  27.   //alert(str+prijs+naam);
  28.   //check if it is filled in
  29.   if (str.length==0)
  30.     { 
  31.     document.getElementById(naam).innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  32.     totaal()
  33.     return;
  34.     }
  35.   //check if it is a number
  36.   if (is_int(str)==false)
  37.     {
  38.     alert("Gelieve een cijfer in te vullen.");
  39.     document.getElementById(naam).innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  40.     document.getElementById(naam+"txt").value='';
  41.     totaal()
  42.     return;
  43.     }
  44.     //calculate the price
  45.     document.getElementById(naam).innerHTML=str*prijs;
  46.     totaal()
  47.     return;
  48. }
  49. </script>
  50. </head>
  51. <body>
  52. <FORM METHOD="post" TARGET="_self" ACTION="pastabasta.php?p=3">
  53.   <table border=0 cellpadding=0 cellspacing=0>
  54.     <?php
  55.       $X= array("spagGR","* Spaghetti","-groot:",7.5);
  56.     ?>
  57.     <tr>
  58.       <td><?php echo $X[1];?></td>
  59.       <td><?php echo $X[2];?></td>
  60.       <td><?php echo "&euro; ".$X[3]; ?></td>
  61.       <td class="puntjesTD">-><INPUT class="puntjes" maxlength="2" NAME="<?php echo $X[0];?>txt" SIZE="1" onKeyUp="berekenen(this.value,<?php echo $X[3]; ?>,'<?php echo $X[0];?>')" ></td>
  62.       <td>X<?php echo $X[3]."=";?></td><td><em id="<?php echo $X[0];?>" name="<?php echo $X[0];?>" class="puntjes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</em></td></tr><tr>
  63.       <td colspan="5" align="right">Totaal bedrag: </td><td><em id="totaal" name="totaal" class="puntjes" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</em></td>
  64.     </tr> 
  65.   </table>
  66. </FORM>
  67. </body>
  68. </html>

I have also tried replacing getElementById with document.forms['name_of_form'].name_of_input.value

but I still get the same issue...

any idea is welcome!

normally you can check the result on http://joop.vazant.be/www/chiro/pastabasta.php
and the send option isn't written yet so feel free to test what you like :)

the best place to change something for testing is right after the second alert.
Expand|Select|Wrap|Line Numbers
  1. if (is_int(str)==false)
  2.     {
  3.     alert("Gelieve een cijfer in te vullen.");
  4.     document.getElementById(naam).innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  5.     document.getElementById(naam+"txt").value='';
  6.  
(the innerhtml still gets executed and the elementbyid stops the script)
Nov 17 '11 #1

✓ answered by acoder

It would go around the value from the text box.

Looking at your code again, I notice that you've only set a name to the input box and no ID whereas you attempted to get the element by its id.

Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
Don't forget to put parse the value using parseInt/Float, e.g. on line 20.
Nov 17 '11 #2

P: 3
what should I parseInt/Float afterwards?
srr I got the function for checking if it is an integer from a differend site I'm still very new at this :)

I have been trying a differend approach via the
Expand|Select|Wrap|Line Numbers
  1. eval("document.forms['formulier']."+naam+"txt.value")="";
but this seems to be giving even more issues :)
(solved that issue via:
Expand|Select|Wrap|Line Numbers
  1. var formObj = eval("document.formulier."+naam+"txt");
  2. formObj.value = tes;

switching to
Expand|Select|Wrap|Line Numbers
  1. document.formulier.elements[naam+"txt"].value = "";
works again in explorer but not in firefox :s


but well can you please give an extra note on the parseInt/Float as I'm affraid I dont understand what you mean.
Nov 17 '11 #3

acoder
Expert Mod 15k+
P: 16,027
It would go around the value from the text box.

Looking at your code again, I notice that you've only set a name to the input box and no ID whereas you attempted to get the element by its id.
Nov 18 '11 #4

P: 3
omg
I have rewritten that part like 10 times and thats the problem. I forgot the ID tag in the inputfield :s

thanks acoder
for the value from the textbox it gets tested
if (is_int(str)==false) the is_int checks if it is a number or not


1000* thnx acoder
I have been looking at that code for houres and houres :)
Nov 18 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.