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 数据转换为 数组或对象echo json_encode($resultArr);
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script><script type="text/javascript">
function loadData(){$.post("findData.php",function(data){
console.log(data);if(data.resultState){
var result = data.result;for(var i in result){
}}
},"json");}
</script></head>
var tr = $("<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>
<input type="button" value="获取数据" onclick="loadData()"><table>
<thead><tr>
</tr></thead>
<tbody id="data">
</tbody></table>
</body>
<td>编号</td><td>部门名称</td><td>部门地址</td>
</html>
PHP 实现 文件上传
<html><head>
<meta charset="utf-8">
<title>文件上传</title>
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script>
</head><body>
<form action="upload.php" method="post" enctype="multipart/form-data"><div>
<img src="" alt="" style="width: 200px;height: 200px" id="showImg"></div>
<div>
<label for="file">文件名:</label>
<input type="file" name="file" id="file"><script type="text/javascript">
// 图片预览$('#file').change(function() {
$("#showImg").attr("src", window.URL.createObjectURL(this.files[0]));});
</script></div>
<div>
<label for="info">描述:</label>
<input type="text" name="info" id="info">
</div>
<input type="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"];
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), <span style="font-size: 9.0