php + ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
本文實例講述了php+ ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
這個是最簡單的表單提交 延伸:后面有很多需要提交的信息 如何快速部署接口
此例子是移動端H5頁面,使用的是zepto.min.js
HTML
<input class="name" type="text" placeholder="請輸入您的姓名" id="name"> <input class="tel" type="text" placeholder="請輸入您的手機(jī)號碼" id="tel"> <span class="button_tijiao" id="form_tijiao"><img src="images/page8/button.png"></span>
前端頁面JS 不跨域的情況下
<script type="text/javascript"> $('#form_tijiao').click(function () { var name =$('#name').val(),tel=$('#tel').val(); if(name=='' && tel ==''){ alert("請檢查是否輸入姓名和電話?。?!") } $.ajax({ type:'post', url:'http://www.netnic.com.cn/formapi/form_qishangh5.php', data:{name:name,tel:tel}, success:function (data, status, xhr) { alert('提交成功!請等候工作人員聯(lián)系!') }, error:function (xhr, errorType, error) { } }) }) </script>
后端PHP頁面 注意不要跨域
1.這里接收前端數(shù)據(jù) $_POST[name],$_POST[tel],post傳送過來,這里需要過濾數(shù)據(jù)是否正常
需要:
①過濾數(shù)據(jù) | √ | ②數(shù)據(jù)唯一性檢測(手機(jī)號唯一) | √ | ③php生成當(dāng)前寫入時間(時間戳格式) | √ | ④如何返回結(jié)果給前端(返回字符串或者是json數(shù)據(jù)) | √ | ⑤手機(jī)號格式驗證(正則匹配驗證) | √ |
⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳
2.鏈接數(shù)據(jù)庫
1)mysql_connect('localhost','數(shù)據(jù)庫用戶名','數(shù)據(jù)庫密碼'),連接數(shù)據(jù)庫
2)選擇數(shù)據(jù)庫 mysql_select_db('數(shù)據(jù)庫名稱',數(shù)據(jù)庫鏈接上一步)
3)數(shù)據(jù)庫時區(qū)設(shè)置 測試環(huán)境php5.2.6 ,在頭部添加 ini_set('date.timezone','Asia/Shanghai');
測試可行;網(wǎng)上說的 date_default_timezone_set('PRC');
并沒有效果;不知道哪里有問題;
參考資料:
date_default_timezone_set()設(shè)置時區(qū)
<?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.問題:Notice: Use of undefined constant callback - assumed 'callback'
解決:
在每個文件頭上加 error_reporting(0);
sql語句清理表數(shù)據(jù)
1.全部清理表中數(shù)據(jù),不刪除表 TRUNCATE TABLE 表名
跨域jsonp解決方案
附:jsonp jquery接收和原生JS接收
客戶端返回實例:
后臺接收回調(diào)函數(shù)callback ,函數(shù)的參數(shù) 就是json的數(shù)據(jù)
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("數(shù)據(jù)成功返回") console.log(json.is_login); console.log(json); } }); });
更多關(guān)于PHP相關(guān)內(nèi)容可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- ajax php 實現(xiàn)寫入數(shù)據(jù)庫
- php從數(shù)據(jù)庫中獲取數(shù)據(jù)用ajax傳送到前臺的方法
- php+ajax 實現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息
- Ajax PHP 邊學(xué)邊練 之三 數(shù)據(jù)庫
- PHP jQuery+Ajax結(jié)合寫批量刪除功能
- php+ajax實現(xiàn)圖片文件上傳功能實例
- php的ajax簡單實例
- php+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php采用ajax數(shù)據(jù)提交post與post常見方法總結(jié)
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- php+ajax+jquery實現(xiàn)點(diǎn)擊加載更多內(nèi)容
- PHP+jQuery+Ajax實現(xiàn)用戶登錄與退出
相關(guān)文章
php實現(xiàn)MySQL數(shù)據(jù)庫備份與還原類實例
這篇文章主要介紹了php實現(xiàn)MySQL數(shù)據(jù)庫備份與還原類,可實現(xiàn)針對MySQL數(shù)據(jù)庫的導(dǎo)入與導(dǎo)出功能,并且包含了針對數(shù)據(jù)庫操作時的加鎖與解鎖等功能,非常具有實用價值,需要的朋友可以參考下2014-12-12