: This article mainly introduces JSajax background interaction. For more information about PHP tutorials, see.
/*************************************** ************************** * *********************** // ***** Status switch * @ param {Object} obj *@ param {Object} table * @ param {Object} fieldAndValue * @ param {Object} whereFieldAndId */function switchStatus (obj, table, fieldAndValue, whereFieldAndId) {var val = (obj. src. match (/yes.gif/I ))? 0: 1; fieldAndValue + = '#' + val; $. post ('/wxbackend/common/switchStatus/flag/0', {'table': table, 'fieldandvalue': fieldAndValue, 'wherefieldandid': whereFieldAndId}, function (data) {if (data> 0) {if (fieldAndValue. indexOf ('1')> 0) obj. src = '/images/yes.gif'; else obj. src = '/images/no.gif ';}});} /*** edit a single field * @ param {Object} obj * @ param {Object} table * @ param {Object} fieldAndValue * @ param {Object} whereFieldAndId */function editField (obj, table, fieldAndValue, whereFieldAndId) {var tag = obj. firstChild. tagName; if (typeof (tag )! = "Undefined" & tag. toLowerCase () = "input") {return;}/* save the original content */var org = obj. innerHTML; var val = Browser. isIE? Obj. innerText: obj. textContent;/* create an input box */var txt = document. createElement ("INPUT"); txt. value = (val = 'n'/')? '': Val; txt. style. width = (obj. offsetWidth + 12) + "px";/* Hide the content of the object and add the input box to the object */obj. innerHTML = ""; obj. appendChild (txt); txt. focus ();/* input event processing function in the editing area */txt. onkeypress = function (e) {var evt = Utils. fixEvent (e); var obj = Utils. srcElement (e); if (evt. keyCode = 13) {obj. blur (); return false;} if (evt. keyCode = 27) {obj. parentNode. innerHTML = org ;}}/* processing function with no focus in the editing area */txt. onblur = funct Ion (e) {if (Utils. trim (txt. value). length> 0 & txt. value! = Org) {fieldAndValue + = '#' + Utils. trim (txt. value); $. post ('/wxbackend/common/switchStatus/flag/1', {'fieldandvalue': fieldAndValue, 'table': table, 'wherefieldandid': whereFieldAndId}, function (data) {obj. innerHTML = (data! = '')? Data: org ;}, 'text');} else {obj. innerHTML = org ;}}} /*** modify a single record * @ param {Object} obj * @ param {Object} table * @ param {Object} fieldList * @ param {Object} whereFieldAndId */function updateRowByPrimaryKey (obj, table, fieldList, whereFieldAndId) {var fields = new Array (); fields = fieldList. split (","); var fieldAndValue = ''; for (I = 0; I
'+ $ (' # '+ Fields [I]). val () + '#';} fieldAndValue = fieldAndValue. substring (0, fieldAndValue. length-2); $. post ('/wxbackend/common/InsertNewOrUpdateRow/option/updat', {'table': table, 'fieldandvalue': fieldAndValue, 'wherefieldandid': whereFieldAndId}, function (data) {if (data = 1) alert ('added successfully! '); Else alert (' failed! ');}/*** Get data * @ param {Object} table * @ param {Object} fieldList, separate * @ param {Object} whereFieldAndId */function selectData (table, fieldList, whereFieldAndId) {$. post ('/wxbackend/common/selectData', {'table': table, 'fieldlist': fieldList, 'wherefieldandid': whereFieldAndId}, function (data) {if (data! = '') Return JSON. parse (data); else return '';}, 'text ');} /*** add a single record * @ param {Object} obj * @ param {Object} table * @ param {Object} fieldList to separate */function insertNewRow (table, fieldList) {var fields = new Array (); fields = fieldList. split (","); var fieldAndValue = ''; for (I = 0; I
'+ $ (' # '+ Fields [I]). val () + '#';} fieldAndValue = fieldAndValue. substring (0, fieldAndValue. length-2); $. post ('/wxbackend/common/InsertNewOrUpdateRow', {'table': table, 'fieldandvalue': fieldAndValue}, function (data) {if (data = 1) alert ('added successfully! '); Else alert (' failed! ');});} /*** Delete a single record using the primary key * @ param {Object} obj * @ param {Object} table * @ param {Object} whereFieldAndId */function delRowByPrimaryKey (obj, table, whereFieldAndId) {$. post ('/wxbackend/common/delrowbyprimarykey', {'table': table, 'wherefieldandid': whereFieldAndId}, function (data) {if (data> 0) {var parentObj = obj. parentNode; parentObj. parentNode. remove ();} else alert ('deletion failed !! ');});}
PHP:
/*** Public status switch */public function actionSwitchStatus ($ flag) {$ this-> checkPrivilege ('actionswitchstatus '); $ table = isset ($ _ POST ['table'])? '{{'. Trim ($ _ POST ['table']). '}': ''; if (empty ($ table) die ('0'); $ fieldAndValue = isset ($ _ POST ['fieldandvalue'])? Explode ('#', trim ($ _ POST ['fieldandvalue']): ''; $ whereFieldAndId = isset ($ _ POST ['wherefieldandid'])? Explode ('#', trim ($ _ POST ['wherefieldandid']): ''; if ($ table = '{milk_order}' & $ fieldAndValue [0] = 'best _ time ') {// milk reservation special case $ fieldAndValue [1] = strtotime ($ fieldAndValue [1]);} $ SQL = "UPDATE $ table SET {$ fieldAndValue [0]} =: val WHERE {$ whereFieldAndId [0]} =: idVal "; $ data = array ('Val' => $ fieldAndValue [1], 'idval' => $ whereFieldAndId [1]); $ num = YD_MysqlUtil: YD_execute ($ SQL, $ data); if ($ flag = 1) {$ sq L = "SELECT {$ fieldAndValue [0]} FROM $ table WHERE {$ whereFieldAndId [0] }={ $ whereFieldAndId [1]}"; $ result = YD_MysqlUtil :: YD_getOne ($ SQL); if ($ table = '{milk_order}' & $ fieldAndValue [0] = 'best _ time ') {// milk reservation special case $ result = date ('Y-m-d H: I: S', $ result );} // modify the coupon amount for both waybills if ($ table ==' {system_setting} '& ("{$ whereFieldAndId [0] }={ $ whereFieldAndId [1 ]} "= 'Id = 6 ') & (''value' = $ fieldAndValue [0]) {$ SQL = 'update {coupon} SET resist_money =: money WHERE cou_type = 9'; YD_MysqlUtil: YD_execute ($ SQL, array ('Money' => $ fieldAndValue [1]);} echo $ result; // echo $ fieldAndValue [1];} else echo $ num; $ content = Yii:: app ()-> session ['yd [admin_name] ']. 'modify table :'. $ table. '. $ whereFieldAndId [0]. '=> '. $ whereFieldAndId [1]. 'recorded '. $ fieldAndValue [0]. '=> '. $ fieldAndValue [1]. 'field'; YDLib: admin_log ($ content);}/*** get Data */public function actionSelectData () {$ this-> checkPrivilege ('actionselectdata'); $ table = isset ($ _ POST ['table'])? '{{'. Trim ($ _ POST ['table']). '}': ''; if (empty ($ table) die ('0'); $ fieldList = isset ($ _ POST ['fieldlist'])? Trim ($ _ POST ['fieldlist']): ''; $ whereFieldAndId = isset ($ _ POST ['wherefieldandid'])? Explode ('#', trim ($ _ POST ['wherefieldandid']): ''; $ SQL = "SELECT $ fieldList FROM $ table WHERE {$ whereFieldAndId [0]}"; switch ($ whereFieldAndId [1]) {case 'getone': $ data = YD_MysqlUtil :: YD_getOne ($ SQL); break; case 'getrow': $ data = YD_MysqlUtil: YD_getRow ($ SQL); break; case 'getall': $ data = YD_MysqlUtil :: YD_getAll ($ SQL); break; default: $ data = YD_MysqlUtil: YD_getColumn ($ SQL); break;} echo PHPTool: _ ToJson ($ Data);}/*** add and modify a new ajax record */public function actionInsertNewOrUpdateRow ($ option = 'insert') {$ this-> checkPrivilege ('actioninsertneworupdaterow '); $ table = isset ($ _ POST ['table'])? '{{'. Trim ($ _ POST ['table']). '}': ''; if (empty ($ table) die ('0'); $ fieldAndValue = isset ($ _ POST ['fieldandvalue'])? Explode ('#', trim ($ _ POST ['fieldandvalue']): ''; if (! Empty ($ fieldAndValue) {if ($ option = 'insert') {$ data = array (); foreach ($ fieldAndValue as $ value) {list ($ key, $ val) = explode ('=>', $ value); $ data [$ key] = $ val;} echo YD_MysqlUtil: YD_insertInto ($ table, $ data )? '1': '0'; $ content = Yii: app ()-> session ['yd [admin_name] ']. "$ option $ table {$ _ POST ['fieldandvalue']}"; YDLib: admin_log ($ content );} else {$ whereFieldAndId = isset ($ _ POST ['wherefieldandid'])? Explode ('#', trim ($ _ POST ['wherefieldandid']): ''; $ data = array (); foreach ($ fieldAndValue as $ value) {list ($ key, $ val) = explode ('=>', $ value); $ data [$ key] = $ val ;} $ where = "{$ whereFieldAndId [0] }={$ whereFieldAndId [1]}"; echo YD_MysqlUtil: YD_updateSet ($ table, $ data, $ where )? '1': '0'; $ content = Yii: app ()-> session ['yd [admin_name] ']. "$ option $ table {$ _ POST ['fieldandvalue'] }{$ _ POST ['wherefieldandid']}"; YDLib: admin_log ($ content );}}} /*** delRowByPrimaryKey */public function actionDelRowByPrimaryKey () {$ this-> checkPrivilege ('actiondelrowbyprimarykey '); $ table = isset ($ _ POST ['table'])? '{{'. Trim ($ _ POST ['table']). '}': ''; if (empty ($ table) die ('0'); $ whereFieldAndId = isset ($ _ POST ['wherefieldandid'])? Explode ('#', trim ($ _ POST ['wherefieldandid']): ''; $ SQL =" DELETE FROM $ table WHERE {$ whereFieldAndId [0]} =: val "; $ data = array ('Val' => $ whereFieldAndId [1]); $ num = YD_MysqlUtil: YD_execute ($ SQL, $ data); $ content = Yii:: app ()-> session ['yd [admin_name] ']. 'use primary key '. $ whereFieldAndId [0]. 'Delete table :'. $ table. 'Record :'. $ whereFieldAndId [1]; YDLib: admin_log ($ content); echo $ num = 0? 0: 1 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces JS ajax background interaction, including some content, and hopes to help friends who are interested in PHP tutorials.