Ajax学习笔记01--php基础语法

1、php上传文件

html文件

<html>
    <style type="text/css">
    </style>
    <script type="text/javascript"> 
     
    

    </script> 
<head></head>
<body>    
    <form action="http://127.0.0.1/ex01/mytest.php" method="post" enctype="multipart/form-data">
      <input type="file" name="upfile"><br/>
      <input type="submit" value="上传"><br/>
    </form>
</body>
</html>

php文件

<?php
$fileInfo=$_FILES["upfile"];
print_r($fileInfo);
$fileName=$fileInfo["name"];
$filePath= $fileInfo["tmp_name"];
move_uploaded_file($filePath,"./sources/".$fileName);
?>

在这里插入图片描述

2、完整的ajax发送异步请求的过程
<html>
    <style type="text/css">
    </style>
    <script type="text/javascript"> 
    window.onload=function(event){
      var obtn=document.querySelector("button");
      obtn.onclick=function(event1){
        //1、创建一个异步对象
          var xmlhttp=new XMLHttpRequest();
        //2、设置一个请求方式和请求地址
          xmlhttp.open("get","http://127.0.0.1/ex01/mytest.php",true);
        //3、发送请求
         xmlhttp.send();
        //4、监听状态的变化
        xmlhttp.onreadystatechange=function(event2){//监听到变化后调用此函数
          //判断请求是否成功
          if(xmlhttp.status>=200 && xmlhttp.status<300 || xmlhttp.status==304){
            //5、返回处理的结果
            if(xmlhttp.readyState==4){
              console.log("接收到服务器返回的数据");
            }
          }
          
        }
        
      }
    } 
    

    </script> 
<head></head>
<body>    
    <button>发送请求</button>
</body>
</html>
3、ajax发送异步请求的过程2

html文件

<html>
    <style type="text/css">
    </style>
    <script type="text/javascript"> 
    window.onload=function(event){
      var obtn=document.querySelector("button");
      obtn.onclick=function(event1){
        //1、创建一个异步对象
          var xhr=new XMLHttpRequest();
        //2、设置一个请求方式和请求地址
        xhr.open("GET","http://127.0.0.1/ex01/mytest.php",true);
        //3、发送请求
        xhr.send();
        //4、监听状态的变化
        xhr.onreadystatechange=function(event2){//监听到变化后调用此函数
          //判断请求是否成功
          if(xhr.readyState==4){
            //5、返回处理的结果
            if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
              console.log("请求成功");
              console.log(xhr.responseText);
            }
            else{
              console.log("请求失败");
            }
          }
          
        }
        
      }
    } 
    

    </script> 
<head></head>
<body>    
    <button>发送请求</button>
</body>
</html>

php文件

<?php

echo "ajax get page";

?>

打印
请求成功
ajax get page

4、ajax使用POST方式发送异步请求

html文件

<html>
    <style type="text/css">
    </style>
    <script type="text/javascript"> 
    window.onload=function(event){
      var obtn=document.querySelector("button");
      obtn.onclick=function(event1){
        //1、创建一个异步对象
          var xhr=new XMLHttpRequest();
        //2、设置一个请求方式和请求地址
        xhr.open("POST","http://127.0.0.1/ex01/mytest.php",true);
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        //3、发送请求
        xhr.send("username=zhangsan&password=123456");//字符串是传入的数据
        //4、监听状态的变化
        xhr.onreadystatechange=function(event2){//监听到变化后调用此函数
          //判断请求是否成功
          if(xhr.readyState==4){
            //5、返回处理的结果
            if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
              console.log("请求成功");
              console.log(xhr.responseText);
            }
            else{
              console.log("请求失败");
            }
          }
          
        }
        
      }
    } 
    

    </script> 
<head></head>
<body>    
    <button>发送请求</button>
</body>
</html>

php文件

<?php

echo $_POST["username"];
echo "<br/>";
echo $_POST["password"];

?>

打印
请求成功
zhangsan
123456

5、jQuery方式ajax使用POST方式发送异步请求

html文件

<html>
    <style type="text/css">
    </style>
    <script language="javascript"  src="jquery-3.5.1.js"></script><!-- 引入jquery库-->
    <script type="text/javascript"> 
    window.onload=function(event){
      var obtn=document.querySelector("button");
      obtn.onclick=function(event1){
        $.ajax({
          type:"GET",
          url:"http://127.0.0.1/ex01/mytest.php",
          data:"username=lisi&location=Boston",
          success:function(msg){
            console.log("Data sage"+msg);
          },
          error:function(xhr){
            console.log(xhr.status);
          }
        });
        
      }
    } 
    

    </script> 
<head></head>
<body>    
    <button>发送请求</button>
</body>
</html>

php文件

<?php

echo $_GET["username"];
echo "<br/>";
echo $_GET["location"];

?>

打印:
Data sagelisi
Boston

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值