如何在.load()之后获取javascript函数的返回值?

如何在.load()之后获取javascript函数的返回值?,javascript,php,html,function,Javascript,Php,Html,Function,所以昨天我发现了在加载后调用另一个函数的可能性。现在,我尝试获取从加载的页面生成的php变量。这是代码——我想我只是少了几行左右 function compute(id) { var qty=document.getElementById('qty'+id).value; $("#sub"+id).show(); $("#sub"+id).load('subtotal.php?qty='+qty+'&cartid='+id, function (total) {

所以昨天我发现了在加载后调用另一个函数的可能性。现在,我尝试获取从加载的页面生成的php变量。这是代码——我想我只是少了几行左右

function compute(id)
{
    var qty=document.getElementById('qty'+id).value;
    $("#sub"+id).show();
    $("#sub"+id).load('subtotal.php?qty='+qty+'&cartid='+id, function (total) {
        document.getElementById('totalholder').value = total;
    });
}
这是subtotal.php

<?php
    require("connect.php");
    session_start();

    $qty=$_GET['qty'];
    $cartid=$_GET['cartid'];
    $id=$_SESSION['ID'];

    $query=mysql_query("update tblcart set quantity=$qty where CartID = $cartid and UserID=$id");
    mysql_query("update tblcart set subtotal=price*quantity where CartID = $cartid and UserID=$id");
    $x=mysql_fetch_array(mysql_query("Select Subtotal from tblcart where CartID = $cartid and UserID=$id"));
    $total=mysql_fetch_array(mysql_query("Select SUM(Subtotal) As Total from tblcart where UserID = $id"));
?>
<div id="<?php echo "sub".$cartid;?>" style="width:100%;float:left;">
<?php echo "Php ".number_format ( $x['Subtotal'], 2, '.', ','); ?>

</div>

<script>
    return (<?php $total; ?>);
</script>

);
我希望将php变量$total返回给函数,以便能够在load()之后使用它。非常感谢您的帮助,并提前向您表示感谢!xx

这就是你所缺少的

.load()将匹配元素的HTML内容设置为返回的 数据。这意味着该方法的大多数用途可能非常简单:

这可能是短路

document.getElementById('totalholder').value = total;

但是,想象一下,它在加载函数之外的自己的脚本标记中

<script type="text/javascript" >
    return (<?php $total; ?>);
</script>

请注意,我通常不使用.load()并主要使用$.post、$.get等…

我认为只需一个简单的
$.get()
就可以做到这一点:

然后,PHP返回一个JSON编码的正文:

header('Content-Type: application/json');
echo json_encode(array(
    'subtotal' => $x['Subtotal'],
    'total' => $total['Total'],
));

我在新的html中没有看到id为“totalholder”的元素,只是一个动态id。Js函数写在同一个文件中吗?(subtotal.php)如果是这样,您可以在其位置回显$total。此外,您的上缺少类型标记,但实际上您是在将加载返回到此容器$(“#sub”+id)dandavis,totalholder位于我的索引中。phpjohn,不,js位于我的索引中。phpThank you!我在这里实现了我的目标,尽管我在第一次加载后仍然坚持使用.show和.load()。不过,还是非常感谢你!
<script type="text/javascript" >
    alert(<?php $total; ?>);
</script>
<script type="text/javascript" >
      $('#totalholder').val(<?php $total; ?>);
</script>
<input type="hidden" id="total" value="<?php echo $total; ?>"/>
    $("#sub"+id).load('subtotal.php?qty='+qty+'&cartid='+id, function () {
            /*The callback is
fired once for each element in the jQuery collection, and this is set
to each DOM element in turn.*/
            if($(this).prop('id') == 'total'){
                var total = $(this).val();
            }
        });
function compute(id)
{
    $.get('subtotal.php', {
        qty: $('#qty' + id).val(),
        cartid: id
    }, function(data) {
        $('#sub' + id).text(data.subtotal).show();
        $('totalholder').val(data.total);
    });
}
header('Content-Type: application/json');
echo json_encode(array(
    'subtotal' => $x['Subtotal'],
    'total' => $total['Total'],
));