欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

JS ajax 后台交互

程序员文章站 2022-05-14 23:13:53
...
/***************************************************后台交互**************************************************/

/**
 * 状态切换
 * @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[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[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('删除失败!!');
	});
}

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了JS ajax 后台交互,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

JS ajax 后台交互

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频