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步骤


优点

小写也可正常发送

相关推荐
浪裡遊7 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
ejinxian14 小时前
PHP 超文本预处理器 发布 8.5 版本
开发语言·php
zorro_z15 小时前
PHP语法基础篇(九):正则表达式
php
The_cute_cat19 小时前
Ajax和Axios的初步学习
前端·学习·ajax
高压锅_12201 天前
思科与华为网络设备命令对比指南:从基础操作到高级配置
服务器·华为·php
SuperherRo1 天前
WEB攻防-文件包含&LFI&RFI&伪协议编码算法&无文件利用&黑白盒
php·文件包含·伪协议·lfi·无文件·黑白盒·rfi
用户Taobaoapi20141 天前
Taobao agent USA丨美国淘宝代购1688代采集运系统搭建指南
数据挖掘·php
蓝色记忆1 天前
Classmap 如何兼容旧代码
php
蓝色记忆2 天前
Composer PSR-4 自动加载机制的完整流程
php
only-lucky2 天前
C语言socket编程-补充
服务器·c语言·php