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


优点

小写也可正常发送

相关推荐
zorro_z3 小时前
PHP语法高级篇(二):文件处理
php
SuperherRo6 小时前
Web攻防-PHP反序列化&原生内置类&Exception类&SoapClient类&SimpleXMLElement
php·xss·反序列化·exception·ssrf·原生类·soapclient
消失的旧时光-19438 小时前
OkHttp SSE 完整总结(最终版)
android·okhttp·okhttp sse
阿维的博客日记8 小时前
TCP和UDP区别
tcp/ip·udp·php
MZ_ZXD00110 小时前
flask校园学科竞赛管理系统-计算机毕业设计源码12876
java·spring boot·python·spring·django·flask·php
Q_Q196328847512 小时前
python的平安驾校管理系统
开发语言·spring boot·python·django·flask·node.js·php
是阿建吖!16 小时前
【Linux | 网络】应用层
linux·网络·php
夏至春来-美美19 小时前
php绘图添加水印,文字使用imagick库的操作
php
果子⌂20 小时前
Docker高级管理
开发语言·docker·云计算·php
凌辰揽月1 天前
AJAX 学习
java·前端·javascript·学习·ajax·okhttp