ajax php

文章目录

php安装,后台处理脚本语言。

后端开发语言不能直接允许,必须放在服务器对对应的文件夹下运行。

如:wamp的对应服务器的文件夹是www


get请求

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02-get</title>
</head>
<body>

<form action="02-get.php" method="get">
    <input type="text" name="userName"><br>
    <input type="password" name="userPwd"><br>
    <input type="submit" value="提交"><br>

</form>


</body>
</html>
c 复制代码
<?php
//print_r($_GET);//查看可以知道这回得到一个Array的数据
echo $_GET["userName"];
echo $_GET["userPwd"];

?>



post

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02-post</title>
</head>
<body>

<form action="02-post.php" method="post">
    <input type="text" name="userName"><br>
    <input type="password" name="userPwd"><br>
    <input type="submit" value="提交"><br>

</form>

</body>
</html>
c 复制代码
<?php

//print_r($_GET);//查看可以知道这回得到一个Array的数据
echo $_POST["userName"];
echo $_POST["userPwd"];

?>

get和post的异同点



ajax

原生步骤

  1. 创建异步对象
  2. 设置请求方式和请求地址
  3. 发送请求
  4. 监听状态变化

  5. 处理返回的结果

代码及现象:

c 复制代码
 //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {



        		}
c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04-ajax-get</title>
    <!--
    1.什么是Ajax


    -->
    <script >
        window.onload = function () {
            var oBtn= document.querySelector("button");
            oBtn.onclick = function (evl) {
                //1.需要创建异步对象
                var xmlhttp = new XMLHttpRequest();
                //2.设置请求方式和请求地址
                xmlhttp.open("GET","04-ajax-get.php",true);
                //3.发生请求
                xmlhttp.send();
                //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {
                    if(xmlhttp.readyState === 4){
                        //判断是否请求成功
                        if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
                            xmlhttp.status === 304){
                            //5.处理返回的结果
                            console.log("接收到服务器返回的数据");
                        }
                        else {
                            //5.处理返回的结果
                            console.log("没有接收到服务器返回的数据");
                        }
                    }

                }


            }
        }
    </script>
</head>
<body>

<button>发生请求</button>
</body>
</html>
c 复制代码
<?php
echo "ajax get test"
?>

完整步骤代码及结果:

c 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04-ajax-get</title>
    <!--
    1.什么是Ajax


    -->
    <script >
        window.onload = function () {
            var oBtn= document.querySelector("button");
            oBtn.onclick = function (evl) {
                //1.需要创建异步对象
                var xmlhttp = new XMLHttpRequest();
                //2.设置请求方式和请求地址
                xmlhttp.open("GET","04-ajax-get.php",true);
                //3.发生请求
                xmlhttp.send();
                //4.监听状态的变化,状态变化都会执行
                xmlhttp.onreadystatechange = function () {
                    if(xmlhttp.readyState === 4){
                        //判断是否请求成功
                        if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
                            xmlhttp.status === 304){
                            //5.处理返回的结果
                            //console.log("接收到服务器返回的数据");
                            alert(xmlhttp.responseText);
                        }
                        else {
                            //5.处理返回的结果
                            //console.log("没有接收到服务器返回的数据");
                        }
                    }
                }
            }
        }
    </script>
</head>
<body>

<button>发生请求</button>
</body>
</html>
c 复制代码
<?php
echo "ajax get test"
?>

jquery步骤


优点

小写也可正常发送

相关推荐
難釋懷4 小时前
Vue解决开发环境 Ajax 跨域问题
前端·vue.js·ajax
androidwork5 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
斯奕sky_small-BAD7 小时前
C++ if语句完全指南:从基础到工程实践
java·开发语言·php
Nick同学7 小时前
GatewayWorker 使用总结
后端·php
Lhuu(重开版10 小时前
Vue:Ajax
vue.js·ajax·okhttp
CRMEB定制开发11 小时前
CRMEB 中 PHP 快递查询扩展实现:涵盖一号通、阿里云、腾讯云
阿里云·php·腾讯云·商城系统·商城源码
CRMEB定制开发11 小时前
PHP 打印扩展开发:从易联云到小鹅通的多驱动集成实践
php·小程序源码·商城源码·微信商城·php商城源码
Bruce_Liuxiaowei11 小时前
PHP文件包含漏洞详解:原理、利用与防御
开发语言·网络安全·php·文件包含
Bruce_Liuxiaowei15 小时前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
痴人说梦梦中人21 小时前
自建 dnslog 回显平台:渗透测试场景下的隐蔽回显利器
web安全·网络安全·渗透测试·php·工具