0

嗨,我在编程和做一些 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。但不知道该怎么做。感谢代码的一些帮助。

4

2 回答 2

1

我认为在您的示例中,您缺少的只是将值传递回输入字段。

   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;

// here! instead of returning the value, set it as the value of your Input
document.getElementById('Kg').value = php_KgIntoRate;}

或者你可以简单地在 php 中进行计算....

$result1 = $Rate * $KgIntoRate;
echo "<tr><td>".$row{'EmployeeName'}."</td>";
echo "<td><input type='text' name='Kg' id='Kg' value='".$result1."'/></td>";
echo "<td>".$Rate."</td>";
于 2013-06-27T08:59:41.277 回答
1

如果您的计算是在 javascript 中处理的,则可以使用 ajax 来完成:

检查它是否对您有帮助。

例如更改在 PHP 代码中:

$i = 0; // define outside the loop

echo "<td><input type='text' name='Kg' id='Kg".$i."' onChange='CalcKgIntoRate(".$i.");'/></td>";
echo "<td id='rate".$i."'>".$KgIntoRate."</td>";
echo "<td id='kg_into_rate".$i."'>".$Amount."</td></tr>";
$i++;

JS代码:

function CalcKgIntoRate(i) {
    var kg = document.getElementById('Kg'+i).value;
    var rate = document.getElementById('rate'+i).innerHTML;
    var kg_into_rate = document.getElementById('kg_into_rate'+i);

    // here is your calculation login
    // suppose you store the calculation in an another variable
    var calVal = parseFloat(kg*eval(rate));
    kg_into_rate.innerHTML = calVal;
}
于 2013-06-27T08:43:07.497 回答