PHP method to implement app interface and return json data
This article mainly introduces the method of php implementing the app interface and returning json data. Interested friends can refer to it. I hope it will be helpful to everyone.
Step one: conn.PHP file, used to connect database and define the interface format, the code is as follows :
<?php header("charset=utf-8"); $servername="localhost"; $username="root"; $password="root"; $dbname="test"; $conn = mysql_connect($servername,$username,$password); if(!$conn){ echo "数据库连接失败!"; } mysql_select_db($dbname); class Response{ public static function json($code,$message="",$data=array()){ $result=array( 'code'=>$code, 'message'=>$message, 'data'=>$data ); //输出json echo json_encode($result); exit; } } ?>
Step 2: text.php, used to convert the data in the database into a json string and output:
<?php require_once('conn.php'); /* *选择数据表 * */ $sqla = "SELECT * from user"; $result = mysql_query($sqla,$conn); $dataarr = array(); while($row = mysql_fetch_array($result)){ $dataarr[]=$row; } $id=$_GET['id']; if($id==1){ Response::json(1,'数据返回成功',$dataarr); }else if($id==2){ Message::json(0,'失败'); } ?>
The third step: text.html, ajax loads json data and displays:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="jquery/2.0.0/jquery.min.js"></script> </head> <body> <input id="text" type="text"/> <input type="button" id="tijiao" value="提交" /> <p id="tex"></p> <script type="text/javascript"> $("#tijiao").click(function(){ var data={"id":$("#text").val()} $.get("text.php?flag=showmessage",data,function(res){ res=JSON.parse(res);//<span style="color:#cc0000;">将json字符串转化为json对象</span> if(res.code==1){ $("#tex").empty(); $.each(res.data, function(x,y) { $("#tex").append("id:"+y.id+"/姓名:"+y.username+"<br>"); }); } }) }) </script> </body> </html>
Related recommendations:
getJSON() asynchronously requests the server to return json format data (graphic tutorial)
JSON object definition in JS Detailed explanation of the value implementation steps
Json-lib processing solution when using frameworks such as Ajax or Easyui (graphic tutorial)
The above is the detailed content of PHP method to implement app interface and return json data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
