嗨,我在编程和做一些 PHP 开发以支持个人承诺方面真的是新手。
我写了一个 javascript 来做一些计算并返回答案。我想从我的 php 访问这个值并打印它。
以下是代码组件。
<?php include 'dbconnection.php'; ?>
<?php
$Kg=0.00;
$Rate=0.00; $MinusBags=0.00; $Amount = 0.00;
$KgIntoRate=0.00;
//$Amount = $KgIntoRate - $MinusBags;
//execute the SQL query and return records
$result = mysql_query("SELECT EmployeeName FROM employees");
$result2 = mysql_query("SELECT SRate FROM scraprates where SYear=YEAR(CURRENT_DATE()) AND SMonth=MONTHNAME(CURRENT_DATE())");
$temp1 = mysql_fetch_array($result2);
$Rate = $temp1['SRate'];
//$Rate=mysql_fetch_array($result2);
//fetch the data from the database
try{
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>".$row{'EmployeeName'}."</td>";
echo "<td><input type='text' name='Kg' id='Kg' onChange='CalcKgIntoRate();'/></td>";
echo "<td>".$Rate."</td>";
// $KgIntoRate = $_GET['php_KgIntoRate'];
//echo "<td>".$_GET['php_KgIntoRate']."</td>";
echo "<td>".$KgIntoRate."</td>";
echo "<td><input type='text' name='MinusBags'/></td>";
echo "<td>".$Amount."</td></tr>";
}
}
catch(Exception $e)
{echo "error".$e;}
?>
<script type="text/javascript">
//$Kg= $('#Kg').val();
function CalcKgIntoRate(){
var Kg=document.getElementById('Kg').value;
var php_rate = "<?php echo $Rate; ?>";
var php_KgIntoRate = "<?php echo $KgIntoRate; ?>";
//document.write(Kg);
php_KgIntoRate=Kg*php_rate;
return php_KgIntoRate;
}
</script>
<?php
//function CalcKgIntoRate(){
//$KgIntoRate = $Kg * $Rate;
//echo "<td>".$KgIntoRate."</td>";
//}
//close the connection
mysql_close($dbhandle);
?>
我想做的是这个。Names 和 Rate 来自两个数据库表。我想根据输入的 Kg(在文本字段的更改事件中)计算 KgIntoRate 并在 Kg * Rate 字段中显示值。
我读到我需要使用 Ajax。但不知道该怎么做。感谢代码的一些帮助。