Home>Article>Web Front-end> Ajax passes JSON example code
Although the full name of ajax is asynchronous javascript and XML. But currently when using ajax technology, passing JSON has become the de facto standard. This article mainly introduces the Ajax delivery JSON example code. Friends who need it can refer to the previous words
Although the full name of ajax is asynchronous javascript and XML. But currently when using ajax technology, passing JSON has become the de facto standard. Because compared to XML, JSON is simple and convenient. This article rewrites the example in the previous article and uses JSON to transfer data
Front-end page
Terminal pageDocument 员工查询
员工创建
"洪七","number"=>"101","sex"=>"男","job"=>'总经理'), array("name"=>"郭靖","number"=>"102","sex"=>"男","job"=>'开发工程师'), array("name"=>"黄蓉","number"=>"103","sex"=>"女","job"=>'产品经理') ); //判断如果是get请求,则进行搜索;如果是POST请求,则进行新建 //$_SERVER["REQUEST_METHOD"]返回访问页面使用的请求方法 if($_SERVER["REQUEST_METHOD"] == "GET"){ search(); }else if($_SERVER["REQUEST_METHOD"] == "POST"){ create(); } //通过员工编号搜索员工 function search(){ //检查是否有员工编号的参数 //isset检测变量是否设置;empty判断值是否为空 if(!isset($_GET['number']) || empty($_GET['number'])){ echo '{"success":false,"msg":"参数错误"}'; return; } global $staff; $number = test_input($_GET['number']); $result = '{"success":false,"msg":"没有找到员工"}'; //遍历$staff多维数组,查找key值为number的员工是否存在。如果存在,则修改返回结果 foreach($staff as $value){ if($value['number'] == $number){ $result = '{"success":true,"msg":"找到员工:员工编号为' .$value["number"] .',员工姓名为' .$value["name"] .',员工性别为' .$value["sex"] .',员工职位为' .$value["job"] .'"}'; break; } } echo $result; } //创建员工 function create(){ //判断信息是否填写完全 if(!isset($_POST['name']) || empty($_POST['name']) || !isset($_POST['number']) || empty($_POST['number']) || !isset($_POST['sex']) || empty($_POST['sex']) || !isset($_POST['job']) || empty($_POST['job']) ){ echo '{"success":false,"msg":"参数错误,员工信息填写不全"}'; return; } echo '{"success":true,"msg":"员工' .test_input($_POST['name']) .'信息保存成功!"}'; } ?>
Example demonstration
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.Related articles:
Characteristics of Ajax and garbled code problems (graphic tutorial)formData image and data upload based on Ajax
Ajax asynchronous request technology example explanation
The above is the detailed content of Ajax passes JSON example code. For more information, please follow other related articles on the PHP Chinese website!