Jump to content

New to Javascript, please help


spires

Recommended Posts

Hi guys.

 

I've been php programming for some time now, but have just started to learn Javascript. I am trying to create my first javascript form which also pulls data from a mysql table.

 

http://www.businessmobiles.com/comcalc/main_interface.php

 

If the following fields are filled in:

field 1 = 2

dropdown 1 = Nokia

dropdown 2 = Orange

click add

 

The form should output the following formula in the final field:

Orange value - Nokia value * field 1

 

However, if field 2 is filled in, the formula should read:

Orange value - Nokia value + field 2 * field 1

 

If am trying to do this in an if statement, but I can't get the first formula to work, only the second.

 

Can someone please advice where i'm going wrong?

 

<?PHP

// Item
$item_sql = "SELECT * FROM csv_item";
$item_result = mysql_query($item_sql) or die ("query 2 failed".mysql_error());
$item_count = mysql_num_rows($item_result);

// Product
$product_sql = "SELECT * FROM csv_product";
$product_result = mysql_query($product_sql) or die ("query 2 failed".mysql_error());
$product_count = mysql_num_rows($product_result);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<script language="javascript">

	function getTotal()
	{
	var selectedQuantity, selectedProduct, selectedItem, selectedUnitPrice, selectedQtyPrice, finalGP;

		selectedQuantity = document.form1.quantity.value;
		selectedProduct = document.form1.product.value;
		selectedItem = document.form1.itemComp.value;
		selectedUnitPrice = document.form1.unitPrice.value;
		selectedQtyPrice = document.form1.qtyPrice.value;

	if (selectedUnitPrice=="0") {
        finalGP = selectedItem - selectedProduct * selectedQuantity;
	}else{
	finalGP = selectedItem - selectedProduct + selectedUnitPrice * selectedQuantity;
	}
		finalUnit = selectedQuantity * selectedUnitPrice;


	document.form1.qtyPrice.value = finalUnit;
	document.form1.gp.value = finalGP;

	}

</script>

<form name="form1">
<input name="quantity" type="text" size="14" value="1"/>
  
<select name="product" onchange="this.style.backgroundColor = '#FFCCCC'">
<option value=""> </option>
<option value="1">add 1</option>
<option value="2">add 2</option>
<option value="3">add 3</option>
<option value="4">add 4</option>
<option value=""> </option>

<?PHP
while ($product_row = mysql_fetch_assoc($product_result)){
				$product_id = $product_row['product_id'];
				$product_id_code = $product_row['product_id_code'];
				$product_network = $product_row['product_network'];
				$product_manufacturer = $product_row['product_manufacturer'];
				$product_name = $product_row['product_name'];
				$product_trade = $product_row['product_trade'];
				$product_subsidy = $product_row['product_subsidy'];
				$product_bonus = $product_row['product_bonus'];
				$product_img = $product_row['product_img'];

				echo'
				<option value="'.$product_trade.'">'.$product_manufacturer.'</option>';
}

?>

</select>
  
<select name="itemComp" onchange="this.style.backgroundColor = '#FFCCCC'">
<option value=""> </option>
<?PHP
while ($item_row = mysql_fetch_assoc($item_result)){
				$item_id = $item_row['item_id'];
				$item_id_code = $item_row['item_id_code'];
				$item_network = $item_row['item_network'];
				$item_group = $item_row['item_group'];
				$item_internal_name = $item_row['item_internal_name'];
				$item_external_name = $item_row['item_external_name'];
				$item_contract_length = $item_row['item_contract_length'];
				$item_line_rental = $item_row['item_line_rental'];
				$item_commission = $item_row['item_commission'];
				$item_shared_min = $item_row['item_shared_min'];
				$item_shared_text = $item_row['item_shared_text'];
				$item_shared_data = $item_row['item_shared_data'];
				$item_single_min = $item_row['item_single_min'];
				$item_single_text = $item_row['item_single_text'];
				$item_single_data = $item_row['item_single_data'];
				$item_global_group = $item_row['item_global_group'];
				$item_local_group = $item_row['item_local_group'];

				echo'
				<option value="'.$item_commission.'">'.$item_network.'</option>';
}

?>

</select>
  
<input name="unitPrice" type="text" size="14" value="0"/>
  
<input name="qtyPrice" type="text" size="14" value="0"/>
  
<input name="gp" type="text" size="14" />
  
<input type="button" value="add" onclick="getTotal()" /> 
</form>
</body>
</html>

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/155162-new-to-javascript-please-help/
Share on other sites

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.