I need a small help on javascript and php

Discussion in 'PHP' started by baris22, Feb 23, 2010.

  1. #1
    hello all,

    This is the code which adds the values together.

    
    
    function amtTot(){
    
    	//fee_tot1
    	//alert(document.getElementById("fee_tot1").value);
    	var tot_amt=parseFloat(document.getElementById("fee_tot1").value)+parseFloat(document.getElementById("fee_tot2").value)+parseFloat(document.getElementById("fee_tot3").value)+parseFloat(document.getElementById("fee_tot4").value)+parseFloat(document.getElementById("fee_tot7").value)+parseFloat(document.getElementById("fee_tot8").value);
    	//alert(tot_amt); var result = num.toFixed(2);
    	document.getElementById("tot_amt").value=tot_amt;
    	addValueText();
    	//tot_amt
    }
    
    <input name="tot_amt" type="text" id="tot_amt" value="" size="10" onClick="amtTot();" />
    
    PHP:
    I want to apply discount on "tot_amt" according to customer_discount field. if customer_discount is "10", i want the "tot_amt" to be "(tot_amt x customer_discount / 100) + tot_amt".

    How can i do this here?

    
    <?
    if (isset($_POST['get_customer_details'])) 
    {
    ?>
    <td><?=$customer_discount;?></td>
    <td><input name="tot_amt" type="text" id="tot_amt" value="" size="10" onClick="amtTot();" /></td>
    <?
    }
    ?>
    
    PHP:
     
    Last edited: Feb 23, 2010
    baris22, Feb 23, 2010 IP
  2. darkblade

    darkblade Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK, I am not sure what you want to do exactly, but you need to know that javascript and php are different. Javascript executes in the brower, while php execute on the server.

    But if you still trying to mix the two, probably best is
    var tot_amt=parseFloat([B][U]<?php echo $customer_discount;?> + [/U][/B]document.getElementById("fee_tot1").value)+parseFloat(document.getElementById("fee_tot2").value)+parseFloat(document.getElementById("fee_tot3").value)+parseFloat(document.getElementById("fee_tot4").value)+parseFloat(document.getElementById("fee_tot7").value)+parseFloat(document.getElementById("fee_tot8").value);
    Code (markup):
    Something link that?
     
    darkblade, Feb 23, 2010 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you very much you made me think another way and it worked. i did this on the javascript

    
    <?
    if (isset($_POST['get_customer_details'])) 
    {
    ?>
    
    function amtTot(){
    
    	//fee_tot1
    	//alert(document.getElementById("fee_tot1").value);
    	var tot_amt=parseFloat(document.getElementById("fee_tot1").value)+parseFloat(document.getElementById("fee_tot2").value)+parseFloat(document.getElementById("fee_tot3").value)+parseFloat(document.getElementById("fee_tot4").value)+parseFloat(document.getElementById("fee_tot7").value)+parseFloat(document.getElementById("fee_tot8").value);
    	//alert(tot_amt); var result = num.toFixed(2);
    	document.getElementById("tot_amt").value=tot_amt - (tot_amt *<?=$customer_discount;?>/100);
    	addValueText();
    	//tot_amt
    }	
    
    <? }else{ ?>
    function amtTot(){
    
    	//fee_tot1
    	//alert(document.getElementById("fee_tot1").value);
    	var tot_amt=parseFloat(document.getElementById("fee_tot1").value)+parseFloat(document.getElementById("fee_tot2").value)+parseFloat(document.getElementById("fee_tot3").value)+parseFloat(document.getElementById("fee_tot4").value)+parseFloat(document.getElementById("fee_tot7").value)+parseFloat(document.getElementById("fee_tot8").value);
    	//alert(tot_amt); var result = num.toFixed(2);
    	document.getElementById("tot_amt").value=tot_amt;
    	addValueText();
    	//tot_amt
    }	
    <? } ?>
    PHP:
     
    baris22, Feb 23, 2010 IP
  4. darkblade

    darkblade Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No problem glad I could help.
    You could probably condense even more. I think you syntax is wrong equal sign on the wrong side and I dont know what the * is for. I am an expert on javascript. But you need to echo the varable.

    Here:
    Should work. They way you dont even need that second function.
    document.getElementById("tot_amt").value=tot_amt <? if($customer_discount > 0){ ?> - (tot_amt *<? echo $customer_discount;?>/100) <? } ?>;
     
    darkblade, Feb 23, 2010 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    i have got no idea on javascript. it just worked by chance i think.

    i changed

    
    document.getElementById("tot_amt").value=tot_amt;
    
    PHP:
    into

    
    document.getElementById("tot_amt").value=tot_amt - (tot_amt *<?=$customer_discount;?>/100);
    
    PHP:
     
    baris22, Feb 23, 2010 IP