员工资料编辑页面
user_edit.php
<?php $id = intval(trim($_GET['id'])); $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $stmt = $pdo->prepare("SELECT * FROM `user` WHERE `id`={$id}"); //echo $stmt->debugDumpParams().'<br>'; $stmt->execute(); //die(); //var_dump($flag); $user = $stmt->fetch(PDO::FETCH_ASSOC); //echo '<pre>'; //print_r($user); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户编辑</title> <style> h3 { text-align: center; } div { width: 300px; height: 180px; /*background-color: lightblue;*/ margin: 0 auto; text-align: center; padding: 20px; border: 1px dashed #888; border-radius: 5%; } div input { border: none; border-bottom: 1px solid #333; } button:hover { cursor: pointer; background-color: lightblue; } .success { color: green; } .error { color: red; } </style> </head> <body> <h3>用户编辑</h3> <div> <form name="user"> <p> <label>用户名: <input type="text" name="name" value="<?=$user['name']?>" disabled> </label> </p> <p> <label>邮 箱: <input type="email" id="email" name="email" value="<?=$user['email']?>" autofocus> </label> </p> <p> <label>密 码: <input type="password" id="password" name="password" value="<?=$user['password']?>"> </label> </p> <!-- 将当前用户的id,做为更新条件,以隐藏域的方式发送到服务器端--> <input type="hidden" id="id" value="<?=$user['id']?>"> <p> <button type="button" onclick="save(this.form)">保存</button> <button type="button" onclick="history.back()">取消</button> </p> <!-- 提示信息占位符--> <p></p> </form> </div> <script> function save(form) { var request = new XMLHttpRequest(); request.onreadystatechange = function () { // console.log(request.responseText); if (request.readyState === 4 && request.status === 200) { var data = JSON.parse(request.responseText); // console.log(data); // 获取提示信息占位元素 var tips = form.lastElementChild; tips.innerText = data.message; // 根据返回的状态,添加适当的class样式 if (data.status === 1) { tips.classList.add('success'); } // 没有更新或更新错误采用同一个样式 else { tips.classList.add('error'); } // 2秒后跳转到上一个页面,即用户列表页 setTimeout(function () { history.back(); }, 2000); } }; request.open('post','user_manage.php?action=save'); request.setRequestHeader('content-type','application/x-www-form-urlencoded'); request.send('email='+form.email.value+'&password='+form.password.value+'&id='+form.id.value); } </script> </body> </html>
user_manage.php
<?php // 屏蔽php的通知级别的错误 //error_reporting(E_ALL ^ E_NOTICE); // 屏蔽php的警告级别的错误 //error_reporting(E_ALL ^ E_WARNING); //print_r(); $action = trim($_GET['action']); $id = trim($_POST['id']); $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); switch ($action){ case 'save': $email = strtolower(trim($_POST['email'])); $password = strtolower(trim($_POST['password'])); $stmt = $pdo->prepare("UPDATE `user` SET `email`='{$email}', `password`='{$password}' WHERE `id`={$id}"); // echo $stmt->debugDumpParams(); // $sql = 'UPDATE `user` SET `email`=:email, `password`=:password WHERE `id`=:id'; // $stmt = $pdo->prepare($sql); // // $email = strtolower(trim($_POST['email'])); // $password = sha1(strtolower(trim($_POST['password']))); // $id =strtolower(trim($_POST['id'])); // $stmt->bindParam('email',$email, PDO::PARAM_STR,60); // $stmt->bindParam('password',$password, PDO::PARAM_STR,20); // $stmt->bindParam('id',$id, PDO::PARAM_INT); if ($stmt->execute()){ if ($stmt->rowCount() == 1) { $status = 1; $message = '更新成功'; } else if ($stmt->rowCount() == 0) { $status = 0; $message = '信息没有更新'; } }else { // 项目如何上线商用, 直接提示出错即可, 不要输出具体出错信息 // die(print_r($stmt->errorInfo())); print_r($stmt->errorInfo()); $status = -1; $message = '保存失败,请检查'; } echo json_encode(['status'=>$status, 'message'=>$message]); break; case 'del': $stmt = $pdo->prepare("DELETE FROM `user` WHERE `id`={$id}"); if ($stmt->execute()){ if ($stmt->rowCount() == 1){ $status = 1; $message = '删除成功'; }elseif ($stmt->rowCount() == 0){ $status = 0; $message = '信息没有更新'; } }else{ $status = -1; $message = '出现未知错误,请联系管理员'; } echo json_encode(['status'=>$status,'message'=>$message]); break; }
使用ajax的好处是能够实现异步处理,不用太多页面跳转,直接后台异步返回数据到前台,提高用户体验
在URL传值的参数是GET方式,php脚本可以使用$_GET来获取传递过来的参数
POST传值的参数在php脚本中使用$_POST来获取参数
php脚本中json_encode()方法可以将一个序列数组转为JSON对象并返回,便于前端数据获取和操作