| maeltar |
07-04-2010 07:57 AM |
Ajax + Javascript + DIV problem
Not so much or a problem, just I don't know...
Ok, so ajax select to create a table with a few fields in it is working fine, obviously puts the table in a DIV...
Now the problem I have is I want to do a calculation on a field in the table, the issue I have is I don't know where to do the javascript calc (if that makes sense)...
Code:
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<script type="text/javascript">
function calcMargin(margin)
{
do I do this here ??????
}
function showProduct(str)
{
// use correct code for Browser Type
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// Check for state change and fill the "div" with the sql data and table html bollocks
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getProd.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<?
$con = mysql_connect('localhost', 'user', 'pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
/*
Grab the ID's and the product names from the db, only going to use the id_prd value for the form value
but going to show the ID and product name for ease of use.
*/
$sql="SELECT id_prd, name_prd FROM products_prdV2 order by id_prd asc";
$result = mysql_query($sql);
?>
<body>
Please select a Product from the List
<form>
<select name="id_prd" onchange="showProduct(this.value)">
<?
while($row = mysql_fetch_array($result))
{
echo "<option value=" . $row['id_prd'] .">" . $row['id_prd'] . " " . $row['name_prd'] . "</option>\n";
}
?>
</select>
</form>
<br />
<div id="txtHint"></div>
</body>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD>
</HTML>
This is the form processor (yes it is rough, just throwing it together to get it working then will tidy and sort it properly, just want funtionality first)
Code:
<?php
$q=$_GET["q"];
?>
<script type="text/javascript">
function calcMargin(margin)
{
Or do I do this here ????
}
</script>
<?
$con = mysql_connect('localhost', 'user', 'pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql="SELECT id_prd, sku_prd, price_prd, prd_discounts.discount_sum FROM products_prdV2, prd_discounts WHERE id_prd = $q and discount_prd_id = $q";
$result = mysql_query($sql);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product ID</th>";
echo "<th>Product name</th>";
echo "<th>Price</th>";
echo "<th>Discount Percentage</th>";
echo "<th>Price after discount</th>";
echo "<th>Cost Price</th>";
echo "<th>Margin</th>";
echo "</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id_prd'] . "</td>";
echo "<td>" . $row['sku_prd'] . "</td>";
echo "<td>£" . $row['price_prd'] . "</td>";
echo "<td>" . $row['discount_sum'] . "%</td>";
echo "<td>£" . round(($row['price_prd'] - (($row['price_prd'] / 100) * $row['discount_sum'])), 2) . "</td>";
echo "<td><input type='text' name='cost_price' onChange='calcMargin(this.value)'></td>";
echo "<td><input type='text' name='margin' value='margin' readonly='readonly' /></td>";
echo "</tr>";
}
echo "</table><br><br>";
mysql_close($con);
?>
Thanks :D
|