Calling javascript func Inside PHP Conditioal Statement

Discussion in 'PHP' started by vassili, Feb 19, 2008.

  1. #1
    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):

     
    vassili, Feb 19, 2008 IP
  2. Ares

    Ares Member

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    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 [​IMG] ,
    there you go [​IMG]:
    
    <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:
     
    Ares, Feb 19, 2008 IP
  3. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <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:
     
    fairuz.ismail, Feb 19, 2008 IP
  4. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    (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! :)
     
    vassili, Feb 19, 2008 IP
  5. CATTechnologies

    CATTechnologies Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    CATTechnologies, Feb 21, 2008 IP
  6. CATTechnologies

    CATTechnologies Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
     
    CATTechnologies, Feb 21, 2008 IP