dtjzpg5313 2015-10-01 08:50
浏览 111

如何使用ajax将javascript变量传递给php

I want to pass javascript variable to php file. Here I attached my code

function Calculate(val)
    {

        var h = document.getElementById('xyz').textContent ;
        var result = h * val;
        result = result.toFixed(2);
        document.getElementById('lblRes').innerHTML ='$'+ result;
        $('#ori_price').hide();

        $.ajax ({
            url: 'nonmembersdetail1.php',
            type : 'POST',
            data : {val : h},
            success: function( result ) {
                alert( result );
            }
        });
   }

<?php
echo "<td ><label id='xyz' name='xyz'>".  $row->nonmember_price  ."</label></td>";
echo "<td ><input type='text' style='width:40px' id='words' name='qty' value='1' onchange='Calculate(this.value);'  /></td>";
?>

In my nonmembersdetail1.php code

echo ( $_POST['val'] );

I didn't get value in php file. Please anyone help me.Thanks in advance...

  • 写回答

2条回答 默认 最新

  • dongzhong5967 2015-10-01 08:59
    关注

    Check in you browser console, if the AJAX call is actually getting fired or you are getting any error before that. Because there is no error in the AJAX call and you should get the val value in POST array of PHP.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?