PHP学习笔记5-PHP-AJAX请求、文件上传、验证码

05-PHP-AJAX请求、文件上传、验证码 

A: PHP 实现AJAX请求

<?php

require"util/dbUtil.php";

//定义一个最终用于返回的数组//该数组为关联数组$resultArr= array();

    $sql = "select * from dept";
    $result = mysqli_query($conn,$sql);

if($result->num_rows>0){//定义一个用于存放数据的数组//该数组为索引数组
$arr= array();

while($row= mysqli_fetch_array($result,MYSQLI_BOTH)) {//定义一个用于存储一行数据的数组
//该数组为索引数组
$temp= array();

$temp["deptno"] = $row["deptno"];$temp["dname"] = $row["dname"];
$temp["loc"] = $row["loc"];
//使用数组的array_push方法 将值放入到数组的最后array_push($arr,$temp);

}

//设置查询消息
$resultArr["msg"] = "查询成功";//设置查询状态true表示查询成功$resultArr["resultState"] = true;//将查询结果放置在关联数组中$resultArr["result"] = $arr;

}else{
$resultArr["msg"] = "查无数据";//设置查询状态false表示查询失败$resultArr["resultState"] = false;

}

//使用PHP json 工具 对数组进行json编码
// json工具中存在json_decode()方法 用于将json数据转换为 数组或对象echojson_encode($resultArr);

<!DOCTYPE html><html>
<
head>

<metacharset="utf-8">
<
metahttp-equiv="X-UA-Compatible"content="IE=edge">
<
title></title>
<
linkrel="stylesheet"href="">
<
scripttype="text/javascript"src="jquery/jquery-1.9.0.js"></script><scripttype="text/javascript">

functionloadData(){$.post("findData.php",function(data){

console.log(data);if(data.resultState){

varresult = data.result;for(vari inresult){

page1image13640 page1image13800 page1image13960 page1image14120 page1image14280 page1image14440 page1image14600 page1image14760

}}

},"json");}

</script></head>

vartr = $("<tr>").appendTo($("#data"));$("<td>").html(result[i].deptno).appendTo(tr);$("<td>").html(result[i].dname).appendTo(tr);$("<td>").html(result[i].loc).appendTo(tr);

<body>
<
inputtype="button"value="获取数据"onclick="loadData()"><table>

<thead><tr>

</tr></thead>

<tbodyid="data">

</tbody></table>

</body>

<td>编号</td><td>部门名称</td><td>部门地址</td>

</html>
B: PHP 实现 文件上传

page2image1552 page2image1712 page2image1872 page2image2032

<html><head>

<metacharset="utf-8">
<
title>文件上传</title>
<
scripttype="text/javascript"src="jquery/jquery-1.9.0.js"></script>

</head><body>

<formaction="upload.php"method="post"enctype="multipart/form-data"><div>

<imgsrc=""alt=""style="width: 200px;height: 200px" id="showImg"></div>

<div>
<
labelfor="file">文件名:</label>
<
inputtype="file"name="file"id="file"><scripttype="text/javascript">

//图片预览$('#file').change(function() {

$("#showImg").attr("src", window.URL.createObjectURL(this.files[0]));});

</script></div>

<div>
<
labelfor="info">描述:</label>
<
inputtype="text"name="info"id="info">

</div>

<inputtype="submit"name="submit"value="提交"></form>

</body></html>

<?php
    echo "<pre>";
    print_r($_POST);
    print_r($_FILES);
    echo "</pre>";
    echo "<hr>";

if($_FILES["file"]["error"] > 0){

echo "错误:". $_FILES["file"]["error"] . "<br>";}

else

{
echo
"上传文件名: " . $_FILES["file"]["name"] . "<br>";
echo
"文件类型: " . $_FILES["file"]["type"] . "<br>";
echo
"文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";echo"文件临时存储的位置: " . $_FILES["file"]["tmp_name"]."<br>";$ext = explode(".", $_FILES["file"]["name"]);
echo
"文件后缀: " . end($ext);

//判断当期目录下的upload目录是否存在该文件
//如果没有upload目录,你需要创建它,upload目录权限为777if(file_exists("upload/". $_FILES["file"]["name"]))
{

echo $_FILES["file"]["name"] . "文件已经存在。";}

else

{

}}

//如果upload目录不存在该文件则将文件上传到upload目录下(需要目录的完全可读权限)move_uploaded_file($_FILES["file"]["tmp_name"],"upload/". $_FILES["file"]["name"]);echo"文件存储在: " . "upload/". $_FILES["file"]["name"];

C: PHP 实现 验证码

<?php

//设置session,用与存放验证码session_start();

//创建图片并设置图片大小
$image= imagecreatetruecolor(100, 30);

//设置验证码颜色
//方法imagecolorallocate(图片对象, int red, int green, int blue);$bgcolor= imagecolorallocate($image,190,234,239);//设置为白色

//区域填充
//方法imagefill(图片对象, int x, int y, 颜色)// (x,y)所在的区域着色,col表示欲涂上的颜色imagefill($image, 0, 0, $bgcolor);
//设置验证码变量
$captcha_code= "";

//生成随机数字for($i=0;$i<4;$i++){

//设置字体大小
$fontsize= 6;
//设置字体颜色,随机颜色
// rand()PHP中的随机函数 标识 从0开始 到120的范围随机
// 0-120深颜色
$fontcolor= imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));//设置数字
$fontcontent= rand(0,9);
//连续定义变量
$captcha_code.= $fontcontent;
//设置当前字出现的位置
$x= ($i*100/4)+rand(5,10);
$y= rand(5,10);

//将文字写入到图片中//imagestring(图片对象,字体大小,x坐标,y坐标,文字类容,字体颜色);imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

}

//存到session
$_SESSION['authcode'] = $captcha_code;

//增加干扰元素,设置雪花点for($i=0;$i<200;$i++){

//设置点的颜色,50-200颜色比数字浅,不干扰阅读
$pointcolor= imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));//imagesetpixel —画一个单像素 的点
//imagesetpixel(图片对象,x坐标,y坐标,颜色)
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

}

//增加干扰元素,设置横线

for($i=0;$i<4;$i++){

//设置线的颜色
$linecolor= imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));

//设置线,两点一线
//imageline(图片对象,起始x,起始y,结束x,结束y,颜色)

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

}

//设置响应头为 图片响应image/png 

header('Content-Type: image/png');

//imagepng()压缩图片为png格式 (矢量图)imagepng($image);

//imagedestroy()结束图形函数 销毁临时 图片对象 

imagedestroy($image); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值