ajax单击按钮在后台运行窗体

时间:2014-12-08 17:34:03

标签: javascript php jquery ajax

好的,所以我有一个iput和一个按钮,当我点击时我想让ajax在后台运行一个脚本并用php文件中的脚本更新另一个div。

这里是按钮和输入

<button type="button" id="calculate" onclick="jQuery('#modal-2').modal('show');"   class="btn btn-purple btn-icon">
<span>calculate</span>
<i class="fa-money"></i>

Ajax在同一个文件中:

 <script>
$(document).ready(function() {
$('calculate').click(function(){
    $.ajax({
        type: 'GET',
        url: '/system/calculate.php',
        data: 'ammount=' + $('ammount').val() ,
        success: function(msg) {
            $('#modal-body').html(msg);
        }
    });
});
});
</script>

和php文件在后台运行

<?php
//edit, do not leave like this.
$ammount = $_POST['ammount'];
$btcvalue = $_POST['price'];
 $total = ($btcvalue * $ammount) * (1 - $system->fee());
echo $fee;
echo 'kjhgjkj';

 ?>



<div class="modal fade custom-width" id="modal-2">
    <div class="modal-dialog" style="width: 60%;">
    <br/><Br/><br/><Br/>            <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"><?php echo $lang['totalpriceof']; ?> <?=$coin;?> to buy</h4>
            </div>

            <div class="modal-body">


            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-info">Save changes</button>
            </div>
        </div>
    </div>
</div>

我已经在php文件中添加了一个警告计算并且它没有提醒,我不知道ajax函数是否有效。

debbugged

Remote Address:[::1]:80

请求网址:system / calculate.php 请求方法:POST 状态代码:200 OK 请求Headersview源 接受: / Accept-Encoding:gzip,deflate 接受语言:EN-US,EN; Q = 0.8 AlexaToolbar-ALX_NS_PH:AlexaToolbar / alxg-3.3 连接:保持活跃 内容长度:0 主持人:本地主机 起源: ?引荐:/仪表盘ammount的= 1&安培;价格= 366.61 User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 39.0.2171.71 Safari / 537.36 X-要求 - 由于:XMLHttpRequest的 响应标题来源 连接:保持活动 内容长度:236 内容类型:text / html的 日期:星期一,2014年12月8日18:45:56 GMT 保持活跃:超时= 5,最大= 96 服务器:Apache / 2.4.10(Win32)OpenSSL / 1.0.1i PHP / 5.5.19 X-已启动方式:PHP / 5.5.19 ConsoleSearchEmulationRendering

保留日志


注意:未定义的索引:ammount in C:\ xampp \ htdocs \ system \ calculate.php 在行 3
注意:未定义的索引: C:\ xampp \ htdocs \ system \ calculate.php 4 的价格
0

2 个答案:

答案 0 :(得分:0)

尝试:

$('#calculate').click(function(){

...因为你要求id

答案 1 :(得分:0)

我会使用数组作为响应。

PHP:

$total = ($_POST['price'] * $_POST['ammount']) * (1 - $system->fee());
define('ARRAYSPLIITER', '::');

echo $fee . ARRAYSPLIITER . 'Second String';

jQuery Ajax:

$.ajax({
     type: 'GET',
     url: '/system/calculate.php',
     data: 'ammount=' + $('ammount').val() ,
     success: function(msg) {
         array = msg.split("::");
         $('#modal-body').html(array[0]);
         $('#element2').html(array[1]);
     }
})