Home > Backend Development > PHP Tutorial > JS ajax background interaction

JS ajax background interaction

WBOY
Release: 2016-08-08 09:22:02
Original
929 people have browsed it
/***************************************************后台交互**************************************************/

/**
 * 状态切换
 * @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';
		}
	});
}

/**
 * 编辑单个字段
 * @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;
  }

  /* 保存原始的内容 */
  var org = obj.innerHTML;
  var val = Browser.isIE ? obj.innerText : obj.textContent;

  /* 创建一个输入框 */
  var txt = document.createElement("INPUT");
  txt.value = (val == 'N/A') ? '' : val;
  txt.style.width = (obj.offsetWidth + 12) + "px" ;

  /* 隐藏对象中的内容,并将输入框加入到对象中 */
  obj.innerHTML = "";
  obj.appendChild(txt);
  txt.focus();

  /* 编辑区输入事件处理函数 */
  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;
	}
  }
  
  /* 编辑区失去焦点的处理函数 */
  txt.onblur = function(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;
    }
  }
}

/**
 * 修改单条记录
 * @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.length ;i++){
		fieldAndValue += fields[i] + &#39;=>' + $('#'+fields[i]).val() + '##';
	}
	fieldAndValue = fieldAndValue.substring(0, fieldAndValue.length - 2);
	$.post('/wxbackend/common/InsertNewOrUpdateRow/option/update', 
		{'table' : table, 'fieldAndValue' : fieldAndValue, 'whereFieldAndId' : whereFieldAndId}, 
		function(data){
			if (data == 1) alert('添加成功!');
			else alert('失败!');
		}
	);
}

/**
 * 获取数据
 * @param {Object} table
 * @param {Object} fieldList  ,分隔
 * @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');
}

/**
 * 新增单条记录
 * @param {Object} obj
 * @param {Object} table
 * @param {Object} fieldList ,分隔
 */
function insertNewRow(table, fieldList){
	var fields = new Array();
	fields = fieldList.split(",");
	var fieldAndValue = '';
	for (i=0;i<fields.length ;i++){
		fieldAndValue += fields[i] + &#39;=>' + $('#'+fields[i]).val() + '##';
	}
	fieldAndValue = fieldAndValue.substring(0, fieldAndValue.length - 2);
	$.post('/wxbackend/common/InsertNewOrUpdateRow', {'table' : table, 'fieldAndValue' : fieldAndValue}, function(data){
		if (data == 1) alert('添加成功!');
		else alert('失败!');
	});
}

/**
 * 删除单条记录 通过主键
 * @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('删除失败!!');
	});
}
Copy after login

PHP:

/**
	 * 公用状态切换
	 */
	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'){//订奶特殊情况
			$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){
			$sql = "SELECT {$fieldAndValue[0]} FROM $table WHERE {$whereFieldAndId[0]} = {$whereFieldAndId[1]}";
			$result = YD_MysqlUtil::YD_getOne($sql);
			
			if($table == '{{milk_order}}' && $fieldAndValue[0] == 'best_time'){//订奶特殊情况
				$result = date('Y-m-d H:i:s',$result);
			}
			//修改运费同时修改面运费优惠券金额
			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]'].'修改表:'.$table.'的'.$whereFieldAndId[0].'=>'.$whereFieldAndId[1].'记录的'.$fieldAndValue[0].'=>'.$fieldAndValue[1].'字段';
		YDLib::admin_log($content);
	}
	
	/**
	 * 获取数据
	 */
	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);
	}
	
	/**
	 * ajax 添加修改新纪录
	 */
	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]'].'通过主键'.$whereFieldAndId[0].' 删除 表:'.$table.'记录:'.$whereFieldAndId[1];
		YDLib::admin_log($content);
		echo $num == 0 ? 0 : 1;
	}
Copy after login

Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above has introduced the JS ajax background interaction, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template