可独立放在一个文件中,也可和HTML混编
我的第一个PHP程序
<?php
echo "hello world";
?>
运行PHP程序
使用Apache服务器,将文件放在工作空间内,在浏览器输入网址运行
注释
//:单行注释
/**/:多行注释
输出
print和echo,常用echo
<?php
//echo可输出多个字符串,用逗号分隔
echo "hello php" ,"good luck";
//print只能输出一个字符串
print "hello world";
?>
创建变量
<?php
//创建变量必须以$开头,以js的var类似,具体类型由赋值类型决定
$a = 10086;
$b = "string";
$c = 3.14;
//输出变量时也要携带$
echo "$a ","$b ","$c ";
?>
字符串拼接
使用符号“.”拼接
<?php
echo "good"."evening";
?>
通过get和post获取参数
get
//html代码
<form action="get02.php" method="get">
<input type="text" name="username" placeholder="姓名">
<input type="text" name="userage" placeholder="年龄">
<input type="submit" name="" value="提交">
</form>
//php代码
<?php
//接收参数必须使用$_GET["name值"]
$name = $_GET["username"];
$age = $_GET["userage"];
echo $name."+++".$age;
?>
post
//html代码
<form action="get02.php" method="post">
<input type="text" name="username" placeholder="姓名">
<input type="text" name="userage" placeholder="年龄">
<input type="submit" name="" value="提交">
</form>
//php代码
<?php
//接收参数必须使用$_POST["name值"]
$name = $_POST["username"];
$age = $_POST["userage"];
echo $name."+++".$age;
?>
创建ajax对象
<body>
<div style="border: 1px solid skyblue "></div>
<script type="text/javascript">
var dv = document.getElementsByTagName("div")[0];
//1,创建ajax对象
var request = new XMLHttpRequest;
//2,open文件
request.open("GET","a.json");
//3,设置响应后的回调
request.onreadystatechange=function(){
if(request.readyState==4){
var txt = request.responseText;
dv.innerHTML = txt;
// alert(txt);
}
};
//4,发送请求
request.send(null);
</script>
</body>
封装XML
<body>
<div id="dv" style="border: 1px solid red"></div>
<script>
//1,创建一个函数对象封装请求xmlhttprequest
function createRequest(){
var req;
//兼容IE低版本
var activeArr = ["MSXML2.XMLHTTP5.0","MSXML2.XMLHTTP4.0","MSXML2.XMLHTTP3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
//判断浏览器类型版本,创建相应的函数对象
if(window.ActiveXObject){
for(var i=0;i<activeArr.length;i++){
req = new ActiveXObject(activeArr[i]);
if(req!=null){
break;
}
}
}else if(window.XMLHttpRequest){
req = new XMLHttpRequest();
return req;
}
};
function ajax2(config){
var request = createRequest();
//利用正则表达式判断请求类型
if(/^get$/i.test(config.method)){
request.open(config.method,config.uri+"?"+parmas(config));
//设置回调
request.onreadystatechange = function(){
if(request.readyState==4){
//接收响应内容
var txt = request.responseText;
//自定义回调内容
config.success(txt);
}
}
//发送请求
request.send(null);
}else if(/^post$/i.test(config.method)){
//open
request.open("POST",config.uri);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
//回调
request.onreadystatechange=function(){
if(request.readyState==4){
var txt = request.responseText;
config.success(txt);
}
};
//参数
request.send(parmas(config.data));
}
};
//拼接参数值
function parmas(data){
var str = "";
for (var i in data) {
str+=(i+"="+data[i]+"&");
}
return str.substr(0,str.length-1);
};
//配置信息,不在封装例,只做测试用
// var config = {
// method :'post',
// uri : 'test07.php',
// data:{"name":"za","age":18},
// success:function(data){
// document.getElementById("dv").innerHTML = data;
// }
// };
// ajax2(config);
</script>
百度账号名验证
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>利用Ajax百度账号名验证</title>
</head>
<body>
<form action="#" method="post">
<input type="text" name="username" id="username" onblur="checkusername()">
<span id="unameTip"></span>
<input type="submit" name="" value="提交">
</form>
<!-- //实现跨域请求,可以使用<script></script>,myajax.js既是封装的XML -->
<script type="text/javascript" src="myajax.js"></script>
<script>
//由script.src中的callback=callback调用,并返回响应的数据,函数名需与右边的callback同名
function callback(data){
console.log(data);
var tip = document.getElementById("unameTip");
if(data.userexsit==1){
tip.innerHTML="不可用";
}else {
tip.innerHTML="可用";
}
}
function checkusername(){
var username = document.getElementById("username").value;
console.log(username);
var script = document.createElement("script");
script.src="https://passport.baidu.com/v2/?regnamesugg&username="+username+"&callback=callback";
document.body.appendChild(script);
}
</script>
</body>
</html>
利用jQuery实现ajax
<body>
<script type="text/javascript" src="jquery-3.4.1.min.js" ></script>
<script>
$.ajax({
type:"get",//请求类型
url:"get01.php?name=zs&age=18",//要发送的文件url及参数值
async:true, //是否异步,是异步
success:function(data){
alert(data.message);//成功时,触发此函数
},
dataType:'json' //规定返回的数据类型
});
</script>
</body>
get01
<?php
//获取
$name = $_GET['name'];
$age = $_GET['age'];
if($name=="zs"){
echo '{"message":"success"}';
}else{
echo '{"message":"error"}';
}
?>