dpu66046 2015-10-21 16:44 采纳率: 0%
浏览 57
已采纳

PHP值不使用AJAX Post更新

I have the need to receive javascript variables in PHP so I set up my Ajax call and script, I have verified that AJAX is sending back the proper response it just seems like server side is not wanting to actually pull the information. Here is my javascript

$("#payments2").on("change", function() {

    $("#p1").val($(this).find("option:selected").text());

    var Id = $(this).find("option:selected").attr('value');
    var Ids = $("#payments").find("option:selected").attr('value');

    $.post("postit.php", { 
        Id: Id,
        Ids: Ids
    }, function(data){

    var theResult = data;
    }, 'json' );
});
$("#exchange").on("change", function() {
    var am = $(this).attr('value');
    var fee = $("#fee").attr('value');
    var cost = $("#cost").attr('value');
    var perc = fee / 100;
    var tot = perc * am;
    var total = am - tot - cost;

    $("#return").val(total);
});

and here is my PHP variable call

<li>
    <label for="exchange">Amount to Exchange</label>
    <input type="text" id="exchange" name="exchange" maxlength="100">
    <span>Amount to Exchange</span>
</li>
<?php
$amounts = $db->query("SELECT * FROM `exchange` WHERE `fromp` = '".$_POST['fromp']."' AND `top` = '".$_POST['top']."'");
$amo = $amounts->fetch_assoc();
?>
<input type="hidden" id="fee" name="fee" value="<?php echo $amo['fee'] ?>" />
<input type="hidden" id="cost" name="pay" value="<?php echo $amo['cost'] ?>" />
<li>
    <label for="return">Amount to Receive</label>
    <input type="text" id="return" name="return" maxlength="100" value="">
    <span>Amount you will receive</span>
</li>

Since I can clearly tell that my Ajax call is sending back the proper information and I am not getting any errors in my query then I am unsure as to why my values for fees and cost aren't changing causing me to receive NaN in my return text box.

Here is also the contents of my postit.php file

  $Id= isset($_POST['Id']) ? $_POST['Id'] : '';
  $Ids= isset($_POST['Ids']) ? $_POST['Ids'] : '';
  include "connect.php";
     $que = $db->query("SELECT * FROM `exchange` WHERE `fromp` = '".$Ids."' AND `top` = '".$Id."'");

    /* Get query results */

    $results = $que->fetch_assoc();

    /* Send back to client */

    echo json_encode($results);
    exit;  

This is my first time using posting variables from javascript to php so I am sure I have done something wrong, or maybe there is a way to get the information that was passed by the script using javascript, perhaps a way to get the variables I need within my javascript function rather than the mysql query?

  • 写回答

2条回答 默认 最新

  • doujue9767 2015-10-21 17:35
    关注

    You're not setting the values for the fields from your json response, try

    $.post("postit.php", { 
        Id: Id,
        Ids: Ids
    }, function(data){
    
        $("#fee").val(data['fee']);
        $("#cost").val(data['cost']);
    
    }, 'json' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)