php + ajax 實(shí)現(xiàn)的寫(xiě)入數(shù)據(jù)庫(kù)操作簡(jiǎn)單示例
本文實(shí)例講述了php+ ajax 實(shí)現(xiàn)的寫(xiě)入數(shù)據(jù)庫(kù)操作。分享給大家供大家參考,具體如下:
這個(gè)是最簡(jiǎn)單的表單提交 延伸:后面有很多需要提交的信息 如何快速部署接口
此例子是移動(dòng)端H5頁(yè)面,使用的是zepto.min.js
HTML
<input type='text' placeholder='請(qǐng)輸入您的姓名' id='name'><input type='text' placeholder='請(qǐng)輸入您的手機(jī)號(hào)碼' id='tel'><span id='form_tijiao'><img src='http://www.aoyou183.cn/bcjs/images/page8/button.png'></span>
前端頁(yè)面JS 不跨域的情況下
<script type='text/javascript'> $(’#form_tijiao’).click(function () { var name =$(’#name’).val(),tel=$(’#tel’).val(); if(name==’’ && tel ==’’){ alert('請(qǐng)檢查是否輸入姓名和電話!!!') } $.ajax({ type:’post’, url:’http://www.netnic.com.cn/formapi/form_qishangh5.php’, data:{name:name,tel:tel}, success:function (data, status, xhr) { alert(’提交成功!請(qǐng)等候工作人員聯(lián)系!’) }, error:function (xhr, errorType, error) { } }) })</script>
后端PHP頁(yè)面 注意不要跨域
1.這里接收前端數(shù)據(jù) $_POST[name],$_POST[tel],post傳送過(guò)來(lái),這里需要過(guò)濾數(shù)據(jù)是否正常
需要:
①過(guò)濾數(shù)據(jù) √ ②數(shù)據(jù)唯一性檢測(cè)(手機(jī)號(hào)唯一) √ ③php生成當(dāng)前寫(xiě)入時(shí)間(時(shí)間戳格式) √ ④如何返回結(jié)果給前端(返回字符串或者是json數(shù)據(jù)) √ ⑤手機(jī)號(hào)格式驗(yàn)證(正則匹配驗(yàn)證) √⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳
2.鏈接數(shù)據(jù)庫(kù)
1)mysql_connect(’localhost’,’數(shù)據(jù)庫(kù)用戶名’,’數(shù)據(jù)庫(kù)密碼’),連接數(shù)據(jù)庫(kù)
2)選擇數(shù)據(jù)庫(kù) mysql_select_db(’數(shù)據(jù)庫(kù)名稱’,數(shù)據(jù)庫(kù)鏈接上一步)
3)數(shù)據(jù)庫(kù)時(shí)區(qū)設(shè)置 測(cè)試環(huán)境php5.2.6 ,在頭部添加 ini_set(’date.timezone’,’Asia/Shanghai’); 測(cè)試可行;網(wǎng)上說(shuō)的 date_default_timezone_set(’PRC’);并沒(méi)有效果;不知道哪里有問(wèn)題;
參考資料:
date_default_timezone_set()設(shè)置時(shí)區(qū)
<?phpini_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.問(wèn)題:Notice: Use of undefined constant callback - assumed ’callback’
解決:
在每個(gè)文件頭上加 error_reporting(0);
sql語(yǔ)句清理表數(shù)據(jù)
1.全部清理表中數(shù)據(jù),不刪除表 TRUNCATE TABLE 表名
跨域jsonp解決方案
最簡(jiǎn)單jsonp實(shí)例
附:jsonp jquery接收和原生JS接收
客戶端返回實(shí)例:
后臺(tái)接收回調(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ù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. 基于javascript處理二進(jìn)制圖片流過(guò)程詳解2. JS sort方法基于數(shù)組對(duì)象屬性值排序3. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……4. JAVA上加密算法的實(shí)現(xiàn)用例5. Django-migrate報(bào)錯(cuò)問(wèn)題解決方案6. ajax請(qǐng)求添加自定義header參數(shù)代碼7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟9. 使用Python和百度語(yǔ)音識(shí)別生成視頻字幕的實(shí)現(xiàn)10. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式
