dpe77294 2013-06-27 08:29
浏览 32

将从javascript返回的值传递给php

hi I am really novice in programming and doing some PHP development to support a personal commitment.

I have written a javascript to do some calculations and return the answer. I want to access this value from my php and print it.

following are the code components.

<?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);
?>

what i want to do is this. Names and Rate are coming from two database tables. I want to calculate KgIntoRate based on the Kg's entered (on change event of text field) and show the value in the Kg * Rate field.

I read that I need to use Ajax. but don't know how to do it. Appreciate some help with the code.

  • 写回答

2条回答 默认 最新

  • dorkahemp972157683 2013-06-27 08:43
    关注

    You can do it wihtout ajax if your calculation is handled in javascript:

    Check if it helps you.

    e.g. Changes are in PHP code:

    $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 Code:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示