Jump to content

broke my javascript making it php?


wmguk

Recommended Posts

hey,

 

I've just changed some code from

<input name="unit1" type="text" id="unit1" style="width: 90px" onChange="calc(1)" />

 

to

<? echo "<input type='text' class='admintext' name='net_$i' value='' onChange=" ."calc(".$i".")" ." style='width: 90px'>"; ?>

 

any idea how I can fix it again?

Link to comment
https://forums.phpfreaks.com/topic/153671-broke-my-javascript-making-it-php/
Share on other sites

ok,

 

i tried

 

<?php echo "<input type='text' class='admintext' name='net_$i' value='' onChange=".calc(\"$i")." style='width: 90px'>"; ?> 

<?php echo "<input type='text' class='admintext' name='net_$i' value='' onChange=".calc(\"$i\")." style='width: 90px'>"; ?> - IT WAS  \"$i\" 

<?php echo "<input type='text' class='admintext' name='net_$i' value='' onChange='calc($i)' style='width: 90px'>"; ?> 

 

but none worked :(

thank you for your help, it actually ended up being

 

<?php
echo '<input type="text" class="admintext" name="net_'.$i.'" id="qty_'.$i.'" value="" style="width: 90px" onChange="calc(\''.$i.'\')" />'; 
?>

 

however for some reason now the javascript still doesnt work...

Sorry, I'm aware that this is the php forum area, not the javascript, so didnt want to appear to take the *** out of anyone...

 

// JavaScript Document
var allClients = new Array();
function addClient(name,coname,address,postcode,tel,email){
var curPointer = allClients.length;
allClients[curPointer] = new Object();
allClients[curPointer].name = name;
allClients[curPointer].coname = coname;
allClients[curPointer].address = address;
allClients[curPointer].postcode = postcode;
allClients[curPointer].tel = tel;
allClients[curPointer].email = email;

}
function findClient(){
var selectedClient = document.getElementById('account_number').value;
var clientPos;
for(var i=0;i<allClients.length;i++){
	if(allClients[i].coname == selectedClient){
		clientPos = i;
		break;
	}
}

displayClientInfo(clientPos);
}

function displayClientInfo(pos){
var target = document.getElementById('net6');
var infoStr = "";
infoStr += "Name: " + allClients[pos].name + "\n";
infoStr += "Company Name: " + allClients[pos].coname + "\n";
infoStr += "Address: " + allClients[pos].address + "\n";
infoStr += "Postal Code: " + allClients[pos].postcode + "\n";
infoStr += "Telephone: " + allClients[pos].tel + "\n";
infoStr += "Email: " + allClients[pos].email;
target.value = infoStr;					
}			

function calc(which){

if(document.getElementById("qty" + which).value && document.getElementById("unit" + which).value){
	var qty = document.getElementById("qty" + which).value;
	var uPrice = document.getElementById("unit" + which).value;
	var netPrice = qty * uPrice;
	netPrice = netPrice.toFixed(2);
	document.getElementById("net" + which).value = netPrice;
	calcTotal();
}
}

function calcTotal(){
var depositAmount = 0;
var subTotal = 0;
var netPrices = new Array();;

 netPrices[0] = document.getElementById("net_1").value;
 netPrices[1] = document.getElementById("net_2").value;
 netPrices[2] = document.getElementById("net_3").value;
 netPrices[3] = document.getElementById("net_4").value;
 netPrices[4] = document.getElementById("net_5").value;
for(var i=0;i<netPrices.length;i++){
	if(netPrices[i]){
		subTotal += parseFloat(netPrices[i]);
	}
}


subTotal = subTotal.toFixed(2);
var VAT = subTotal * 0.15;
VAT = VAT.toFixed(2);
var InTotal = parseFloat(subTotal) + parseFloat(VAT);
var InTotal = InTotal.toFixed(2)
depositAmount = document.getElementById("deposit").value;
var balanceDue = InTotal - depositAmount;
balanceDue = balanceDue.toFixed(2);


document.getElementById("total_net").value = subTotal;
document.getElementById("total_vat").value = VAT;
document.getElementById("total").value = InTotal;
document.getElementById("balance").value = balanceDue;
}

 

and the code which generates it is now

 

                       <?PHP for($i = 1;$i <= $count;$i++) { ?>
                          <tr>
<td align="center"><?php echo '<input type="text" class="admintext" name="qty_'.$i.'" id="qty_'.$i.'" value="" style="width: 50px" onChange="calc(\''.$i.'\')" />'; ?></td>
<td align="center"><?php echo '<input type="text" class="admintext" name="details_'.$i.'" id="qty_'.$i.'" value="" style="width: 560px" onChange="calc(\''.$i.'\')" />'; ?></td>
<td align="center"><?php echo '<input type="text" class="admintext" name="unit_'.$i.'" id="qty_'.$i.'" value="" style="width: 90px" onChange="calc(\''.$i.'\')" />'; ?></td>
<td align="center"><?php echo '<input type="text" class="admintext" name="net_'.$i.'" id="qty_'.$i.'" value="" style="width: 90px" onChange="calc(\''.$i.'\')" />'; ?></td>
                          </tr>
                         <? } ?>

ahhh!! ok,

 

so i changed it and now its working, except the subtotal / total vat area isnt working...

 

function calc(which){

if(document.getElementById("qty_" + which).value && document.getElementById("unit_" + which).value){
	var qty = document.getElementById("qty_" + which).value;
	var uPrice = document.getElementById("unit_" + which).value;
	var netPrice = qty * uPrice;
	netPrice = netPrice.toFixed(2);
	document.getElementById("net_" + which).value = netPrice;
	calcTotal();
}
}

 

now works ^^

 

but

function calcTotal(){
var depositAmount = 0;
var subTotal = 0;
var netPrices = new Array();;

 netPrices[0] = document.getElementById("net_1").value;
 netPrices[1] = document.getElementById("net_2").value;
 netPrices[2] = document.getElementById("net_3").value;
 netPrices[3] = document.getElementById("net_4").value;
 netPrices[4] = document.getElementById("net_5").value;
for(var i=0;i<netPrices.length;i++){
	if(netPrices[i]){
		subTotal += parseFloat(netPrices[i]);
	}
}


subTotal = subTotal.toFixed(2);
var VAT = subTotal * 0.15;
VAT = VAT.toFixed(2);
var InTotal = parseFloat(subTotal) + parseFloat(VAT);
var InTotal = InTotal.toFixed(2)
depositAmount = document.getElementById("deposit").value;
var balanceDue = InTotal - depositAmount;
balanceDue = balanceDue.toFixed(2);


document.getElementById("total_net").value = subTotal;
document.getElementById("total_vat").value = VAT;
document.getElementById("total").value = InTotal;
document.getElementById("balance").value = balanceDue;

 

doesnt seem to work - it should auto calculate however its not doing it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.