Simple example of writing database operation with PHP + ajax

Time:2020-10-15

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

HTML

< 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!!! ""
  }

  $.ajax({
   type:'post',
   url:'http://www.netnic.com.cn/formapi/form_qishangh5.php',
   data:{name:name,tel:tel},
   
   success:function (data, status, xhr) {
    Alert ('submitted successfully! Please wait for the staff to contact! )
   },
   error:function (xhr, errorType, error) {

   }

  })
 })

</script>

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

need:

                   
① 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


<?php
ini_set('date.timezone','Asia/Shanghai');

$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)
VALUES
('$_POST[name]','$_POST[tel]','$time')";

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

?>

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

solve:

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} );

$(document).ready(function(){
$.ajax({
/*url:'http://172.16.69.220:8100/get_user/',*/
url:'http://192.168.1.72:8002/get_user/',
type:'get',
dataType:'jsonp',
jsonp:'callback',
processData: false,
jsonpCallback:'flightHandler',
success:function (json) {
Alert (data returned successfully)
console.log(json.is_login);
console.log(json);
}
});
});

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.