• ajax案例源码


    html文件中demo2_index.html

    --------------------------------------------------------------------------------------------------------------------

    <! DOCTYPE>

    <html>

    <head>

        <meta charset= "utf-8" >

        <title>Demo2 </title>

        <style>

    body, input, button, select, h1 {

                font-size: 18px;

                line-height: 1.8em;

    }

        </style>

    </head>

    <body>

    <h1>员工查询</h1>

    <label>输入员工编号:</label>

    <input type= "text" id= "keyword">

    <button id= "search">查询</button>

    <p id= "searchResult"></p>

    <h1>员工新建</h1>

    <label>请输入员工姓名</label>

    <input type= "text" id= "peopleName"> <br>

    <label>请输入员工编号</label>

    <input type= "text" id= "peopleNumber"> <br>

    <label>请输入员工性别</label>

    <select id= "peopleSex">

        <option>男</option>

        <option>女</option>

    </select>

    <br>

    <label>请输入员工职位</label>

    <input type= "text" id= "peopleJob"> <br>

    <button id= "save">保存</button>

    <p id= "createResult"></p>

    <script>

        document.getElementById( "search" ).onclick = function (){

    //发送Ajax查询请求并处理

    var request = new XMLHttpRequest();

            request.open( "GET" ,"/ajax/demo2.php?number=" + document.getElementById("keyword" ).value);

            request.send();

    // 监听

            request.onreadystatechange = function (){

    if(request.readyState === 4){

    if(request.status === 200){

                        document.getElementById( "searchResult" ).innerHTML = request.responseText;

                    } else {

                        alert( "发生错误:" + request.status);

                    }

                }

            }

        }

        document.getElementById( "save" ).onclick = function (){

    var request = new XMLHttpRequest();

            request.open( "POST" ,"/ajax/demo2.php" );//POST请求是隐藏的不需要显示参数

    var data = "name =" + document.getElementById("peopleName" ).value

                    + "&number=" + document.getElementById("peopleNumber" ).value

                    + "&sex=" + document.getElementById("peopleSex" ).value

                    + "&job=" + document.getElementById("peopleJob" ).value;

            request.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded" );

            request.send(data);

    if(request.readyState === 4){

    if(request.status === 200){

                        document.getElementById( "createResult" ).innerHTML = request.responseText;

                    } else {

                        alert( "发生错误:" + request.status);

                    }

                }

        }

    </script>

    <!--<script>

        document.getElementById("search").onclick() = function();

        var request = new XMLHttpRequest();

        request.open("GET","demo2.php?number="+document.getElementById("keyword").value);

        request.send();

        request.onreadystatechange = function () {

            if(request.state===4){

                if(request.status===200){

                    document.getElementById("searchResult").innerHTML = request.responseText;

                }else{

                    alert("参数错误:"+request.status);

                }

            }

        }

    </script>-->

    </body>

    </html>

    ---------------------------------------------------------------------------------------------------------------

    php文件中demo2.php

    <?php

    /**

    * Created by PhpStorm.

    * User: moka

    * Date: 2016/3/5

    * Time: 5:22

    */

    //设置页面内容html编码格式“utf-8”

    header ("Content-type:text/plain;charset=utf-8" );

    //header("Content-Type:application/json;charset=utf-8");

    //header("Content-Type:text/xml;charset=utf-8");

    //header("Content-type:text/html;charset=utf-8");

    //header("Content-type:application/javascript;charset=utf-8");

    //定义一个多维数组

    $people = array(

    array( "name" =>"洪七" ,"number" =>"101" ,"sex" =>"男" ,"job" =>"产品经理" ),

    array( "name" =>"郭靖" ,"number" =>"102" ,"sex" =>"男" ,"job" =>"程序员" ),

    array( "name" =>"黄蓉" ,"number" =>"103" ,"sex" =>"女" ,"job" =>"测试员" )

    );

    //判断如果是get请求,则进行搜索,如果是POST请求,则进行新建

    //$_SERVER是一个超全局变量,在一个脚本的全部作用域中都可以用,不用使用global关键字

    //$_SERVER["REQUEST_METHOD"]返回访问页面使用的请求方法

    if ($_SERVER[ "REQUEST_METHOD" ]=="GET" ){

    search();

    } elseif( $_SERVER["REQUEST_METHOD" ]=="POST" ){

    create();

    }

    //通过员工编号搜索员工

    function search(){

    //检查是否有员工编号的参数

    //isset检测变量是否设置;empty判断值是否为空

    //超全局变量 $_GET 和$_POST用于收集表单数据

    if(! isset( $_GET[ "number"])|| empty ($_GET[ "number" ])){

    echo "参数错误" ;

    return ;

        }

    //函数之外声明的变量拥有Global作用域,只能在函数以外进行访问。

    //global关键词用于访问函数内的全局变量

    global $people;

    //获取number参数

        $number = $_GET["number" ];

        $result = "没有找到员工!" ;

    //遍历$people多维数组,查找key值为number的员工是否存在,如果存在,则修改返回结果

    foreach($people as $key){

    if($key[ "number" ]==$number){

                $result = "找到员工:员工编号:" .$key["number" ]."员工姓名:" .$key["name" ]."员工性别:" .$key["sex" ]."员工职位:" .$key["job" ];

    break;

            }

        }

    echo $result;

    }

    //创建员工

    function create(){

    //判断信息是否填写完全、

    if(! isset( $_POST[ "name"])|| empty ($_POST[ "name" ])

            ||! isset ($_POST[ "number" ])||empty ($_POST[ "number" ])

            ||! isset ($_POST[ "sex" ])||empty ($_POST[ "sex" ])

            || isset ($_POST[ "job" ])||empty ($_POST[ "job" ])

        ){

    echo "参数错误,员工信息填写不全" ;

    return;

        }

    //TODO ;获取POST表单数据并保存到数据库

    //保存成功

    echo "员工:" .$_POST[ "name" ]."信息保存成功!" ;

    }

    ?>

    我生活的地方,我为何要生活。
  • 相关阅读:
    [ZJOI2010] 数字计数
    [USACO] 2004 Open MooFest 奶牛集会
    数星星
    [SCOI2011] 糖果
    西瓜种植
    [NOI2018] 归程
    [APIO2012] 派遣
    小K的农场
    妮可妮可妮 [Hash]
    [ZJOI2012] 灾难
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/5446455.html
Copyright © 2020-2023  润新知