diff --git a/Ajax.php b/Ajax.php index 897f08e..546b1d0 100644 --- a/Ajax.php +++ b/Ajax.php @@ -26,25 +26,61 @@ $ajax = new Ajax(); echo $ajax->procesa(); class Ajax { - private function respuesta($exito, $mensaje) + private $sql; + private $tabla; + + public function __construct() + { + $this->sql = new Sql(SERVIDOR, USUARIO, CLAVE, BASEDATOS); + if ($this->sql->error()) { + return $this->respuesta($this->mensaje(false, 'Error conectando con la Base de Datos')); + }; + $this->tabla = $_GET['tabla']; + } + private function respuesta($mensaje) { - $resp = json_encode(array("success" => $exito, "msj" => $mensaje)); header('Content-Type: application/json', true, 200); - return $resp; + return $mensaje; } public function procesa() + { + $opc = $_GET['opc']; + switch ($opc) { + case "get": return $this->obtiene(); + case "put": return $this->actualiza(); + } + } + private function mensaje($exito, $texto) + { + return json_encode(array("success" => $exito, "msj" => $texto)); + } + private function actualiza() { if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { - $tabla = $_GET['tabla']; - $sql = new Sql(SERVIDOR, USUARIO, CLAVE, BASEDATOS); - if ($sql->error()) { - return $this->respuesta(false, 'Error conectando con la Base de Datos'); + $comando = "update " . mysql_escape_string($this->tabla) . " set " . mysql_escape_string($_POST['name']) . " = '" . mysql_escape_string($_POST['value']) . "' where id = '" . mysql_escape_string($_POST['pk']). "';"; + $this->sql->ejecuta($comando); + $exito = !$this->sql->error(); + $mensaje = $this->sql->mensajeError(); + $resp = $this->mensaje($exito, $mensaje); + return $this->respuesta($resp); + } + } + private function obtiene() + { + if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { + $comando = "select id, descripcion from " . $this->tabla . " order by descripcion asc;"; + $this->sql->ejecuta($comando); + $exito = !$this->sql->error(); + $mensaje = $this->sql->mensajeError(); + if (!$exito) { + return $this->respuesta($this->mensaje($exito, $mensaje)); } - $comando = "update " . mysql_escape_string($tabla) . " set " . mysql_escape_string($_POST['name']) . " = '" . mysql_escape_string($_POST['value']) . "' where id = '" . mysql_escape_string($_POST['pk']). "';"; - $sql->ejecuta($comando); - $exito = !$sql->error(); - $mensaje = $sql->mensajeError(); - return $this->respuesta($exito, $mensaje); + $filas = array(); + while($r = $this->sql->procesaResultado()) { + $filas[] = array($r['id'] => $r['descripcion']); + } + $resp = json_encode($filas); + return $this->respuesta($resp); } } } diff --git a/AportaContenido.php b/AportaContenido.php index ca8c078..c572c5a 100644 --- a/AportaContenido.php +++ b/AportaContenido.php @@ -53,6 +53,7 @@ define('CREDITOS', '