douzhi9478 2013-03-28 17:37
浏览 75
已采纳

如何将php变量传递给jquery AJAX?

I have started learning jquery AJAX. I have run into a problem, and was wondering if you guys could help me. I am trying to pass a php variable back to jquery, but it displays as [object Object]. I will be posting my code below.

index.html:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $("button").click(function() {
        $("p").text($.get("return.php"));
    });
});
</script>
</head>

<body>
<p>This is a test!</p>
<button>Click Here</button>
</body>
</html>

return.php:

<?php 
    $message = "Another test!";
    echo $message;
?>

So what is it that I need to do to pass php variable $message into the paragraph using jquery ajax?

I know I could simply do if I changed index.html to index.php, but then if $message later changes, I have to reload the page. I am trying to learn how to make dynamic content without having to reload the page.

Thanks ahead of time for any help you provide! :-)

  • 写回答

3条回答 默认 最新

  • douxunnian0423 2013-03-28 17:39
    关注

    You'll have to wait until the data is returned before you can use it:

    $(document).ready(function(){
        $("button").click(function() {
            $.get("return.php", function(data) {
                 $("p").text(data);
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 服务器一块硬盘重启过程中安装后,硬盘不识别了怎么修复?
  • ¥15 微信红包不拆包 知道红包金额
  • ¥15 0基础学机器人运动控制要多久?
  • ¥15 .net core 怎么进行中英文转换
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?