美文网首页
PHP编程实战15-16/17

PHP编程实战15-16/17

作者: 海边拾贝 | 来源:发表于2015-11-09 22:29 被阅读0次

前端

<!--PHP编程实战-->
<!--JSON & Ajax -->
<!--15-17-->
<!--从Ajax请求中加载响应的html文件-->
<html>
<head>
    <title>Predator/Prey Example</title>
</head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#predator").click(function(){
            $("#response").load("predator_prey.php?type=predator");
        })
        $("#prey").click(function(){
            $("#response").load("predator_prey.php?type=prey")
        });
    });
</script>
<body>
    <button id="predator">Predator</button>
    <button id="prey">Prey</button>
    <p><strong>Ajax response form PHP:</strong></p>
    <div id="response"> </div>
</body>
</html>

后台predator_prey.php

<!--PHP编程实战-->
<!--JSON & Ajax -->
<!--15-16-->
<!--PHP文件选择Predator或Prey动物,并以JSON格式输出,predator_prey.php-->
<?php
error_reporting(E_ALL);
$predators = array("bear", "shark", "lion", "tiger", "eagle", "human", "cat", "wolf"); //食肉动物
$prey = array("salmon", "seal", "gazelle", "rabbit", "cow", "moose", "elk", "turkey");//被捕食者
if (isset($_GET['type'])) {
    switch ($_GET['type']) {
        case "predator":
            print json_encode($predators[array_rand($predators)]);
            break;
        case "prey":
            print json_encode($prey[array_rand($prey)]);
            break;
        default:
            print json_encode("n/a");
            break;
    }
}
?>

重点

  • load() 方法通过 AJAX 请求从服务器加载数据,并把返回的数据放置到指定的元素中。

相关文章

  • PHP编程实战15-16/17

    前端 后台predator_prey.php 重点 load() 方法通过 AJAX 请求从服务器加载数据,并把返...

  • PHP编程实战14-17

    生成带有命名空间和属性的RSS样例.SimpleXMLXML文档在线验证

  • PHP Socket 编程实战总结

    在进入 PHP Socket 编程实战之前,我们先来了解一下 PHP Socket 的一些基础知识。 一、基础部分...

  • PHP编程实战15-14/15

    前端 服务器端json_example.php返回json字符串 重点 请求资源文件在PHP编程实战15-14$....

  • PHP编程实战14.1 xml

    要解析xml,可以使用树模型或事件驱动模型.树模型--把html和xml文档呈现为一棵元素树,容易直观一次加载完....

  • PHP编程实战15.2 Ajax

    Ajax的一些缺点: 浏览器后退按钮和书签不记录Ajax状态;搜索引擎很难对动态生成的内容进行索引;从容降级队非J...

  • PHP编程实战15-13

    xml数据文件 重点 $.get(请求的文件,回调函数,期望的数据类型默认text)$("#generated_c...

  • PHP编程实战15-19

    前端 服务器端save_drawing.php load_drawing.php 重点 像素文件保存为json格式...

  • PHP编程实战15-1

  • PHP编程实战15-2

网友评论

      本文标题:PHP编程实战15-16/17

      本文链接:https://www.haomeiwen.com/subject/pfhhhttx.html