且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AJAX-获得从PHP数据

更新时间:2023-11-27 18:45:16

您可以把jQuery来很好地利用这里。该文档是在这里 http://api.jquery.com/jQuery.ajax/

You can put jQuery to good use here. The docs are here http://api.jquery.com/jQuery.ajax/.

下面是一个例子:

<html>
<head>
<!-- Include jquery from Google here -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<script  type="text/javascript">
// Wait till dom is loaded
$(document).ready(function() {
  // When id with Action is clicked
  $("#Action").click(function()
  {
     // Load ajax.php as JSON and assign to the data variable
     $.getJSON('ajax.php', function(data) {
        // set the html content of the id myThing to the value contained in data
        $("#myThing").html(data.value);
     });   
  });
});
</script>
</head>
<body>
  <a id="Action">Click Me</a>
  <p id="myThing"></p>
</body>
</html>

您ajax.php文件可以只包含:

Your ajax.php file can just contain:

<?php
    echo json_encode(array("value" => "Hello World"));
?>