doulan8054 2014-07-23 13:15
浏览 48
已采纳

使用jQuery Ajax load()[duplicate]从PHP文件加载变量

This question already has an answer here:

Assuming my example.php looks like this:

<?php 
$variable = "I am a variable";
?>

and on another page I have:

<div id="loadme"></div>

How can I get that variable to my div?

Can it be achieved by using .load()?

I can imagine could be something like this:

$('#loadme').load("example.php", $variable);

Am I even close?

</div>
  • 写回答

3条回答 默认 最新

  • doulou1989 2014-07-23 13:18
    关注

    You're close, you need to echo the variable in the PHP file -

    <?php
    $variable = "foo";
    echo $variable;
    ?>
    

    Then load like this -

    $('#loadme').load('example.php');
    

    The content of #loadme will now be 'foo'.

    EDIT: Based on the OP's comments -

    Each echo containing a variable could be a div with an id, e.g.,

    echo '<div id="foo">$variable</div>';
    

    Then your load statement would look like this -

    $('#loadme').load('example.php #foo');
    

    Now #loadme would contain the variable echoed in that div. This is not an efficient plan if the variables are to be re-used by JavaScript/jQuery code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题