Simple example of writing database operation with PHP + ajax


This paper describes the PHP + Ajax implementation of writing database operations. For your reference, the details are as follows:

This is the simplest form submission extension: there are a lot of information to be submitted. How to quickly deploy the interface

This example is a mobile H5 page, which uses zepto.min.js


< input type = "text" placeholder = "please enter your name" >
< input type = "text" placeholder = "please enter your mobile phone number" >
<span><img src="images/page8/button.png"></span>

When the front page JS does not cross domain

<script type="text/javascript">
 $('#form_tijiao').click(function () {
  var name =$('#name').val(),tel=$('#tel').val();
  if(name=='' && tel ==''){
   Please check if you input your name and phone number!!! ""

   success:function (data, status, xhr) {
    Alert ('submitted successfully! Please wait for the staff to contact! )
   error:function (xhr, errorType, error) {




Be careful not to cross domain on the back-end PHP page

1. Receive front-end data here$_ POST[name],$_ Post [Tel], whether the data need to be filtered is normal


① Filtering data ② Data uniqueness detection (unique mobile phone number) ③ PHP generates the current write time (timestamp format) ④ How to return the result to the front end (return string or JSON data) ⑤ Mobile phone number format verification (regular matching verification)


2. Link database

1)mysql_ Connect (‘localhost ‘,’ database user name ‘,’ database password ‘), connect to the database

2) Select databasemysql_ select_ DB (‘Database name ‘, database link previous)

3) Set the test environment php5.2.6 in the database time zone and add it in the headerini_set('date.timezone','Asia/Shanghai');The test is workable; said on the Internetdate_default_timezone_set('PRC');There is no effect

reference material:

date_ default_ timezone_ Set() sets the time zone


$con = mysql_connect("localhost","admin","qishangxiangyunnetnic20160407");
if (!$con)
 die('Could not connect: ' . mysql_error());

mysql_select_db("qishang_form", $con);

$time = date('Y-m-d H:i:s',time());

$sql="INSERT INTO netnich5 (name, tel_number,posttime)

if (!mysql_query($sql,$con))
 die('Error: ' . mysql_error());
echo "1 record added";


1. Question:Notice: Use of undefined constant callback – assumed ‘callback’


Add error to each file header_ reporting(0);

SQL statement cleans table data

1. Clear all data in the table without deleting the truncate table name

Cross domain jsonp solution

The simplest jsonp instance

Attachment: jsonp jQuery receiving and native JS receiving

Client return instance:

The background receives the callback function callback, and the parameters of the function are the data of JSON

callback( {username: "", is_login: false} );

processData: false,
success:function (json) {
Alert (data returned successfully)

More about PHP related content can see the special topic of this website: “PHP + Ajax skills and application summary”, “PHP network programming skills summary”, “PHP string (string) Usage Summary”, “PHP + MySQL database operation introduction tutorial” and “PHP common database operation skills summary”

I hope this article will help you with PHP programming.