<?php
require_once("lib/xajax/xajax_core/xajax.inc.php");
//instanciamos el objeto de la clase xajax
$xajax = new xajax();
$xajax->setCharEncoding('UTF-8');
$xajax->decodeUTF8InputOn();
//echo "Cadena de conección: ",$cadena;
// iniciando la coneccion a postgesql
$usuario='postgres';
$password='xxxxx';
$cadena='host=localhost dbname=database user='.$usuario.' password='.$password;
function leer_form($id){
$respuesta = new xajaxResponse();
$respuesta->setCharacterEncoding('UTF-8');
if ($id=="") {
$respuesta->assign("er","innerHTML","<b>NO SE HA ESPECIFICADO NADA QUE BUSCAR</b>");
return $respuesta;
}
global $cadena;
@$query = pg_query($conn,"select * from clientes where idclientes=".$id.";"); if (! $query){
$respuesta->assign("er","innerHTML",$id." no existe");
}else{
$respuesta->assign("id","value",$row[0]);
$respuesta->assign("nombre","value",$row[1]);
$respuesta->assign("apellido","value",$row[2]);
$respuesta->assign("fecha","value",$row[3]);
$respuesta->assign("er","innerHTML","");
//$respuesta->addCreateInput("clientes", "text", "nuevo", "nuew");
}
//$respuesta->assign("er","innerHTML","r: ".$conn);
return $respuesta;
}
function guardar_form($form){
$respuesta = new xajaxResponse();
$respuesta->setCharacterEncoding('UTF-8');
global $cadena;
if($form['id']==""){
$updatestring="insert into clientes values(default,'".$form['nombre']."','".$form['apellido']."','".$form['fecha']."');";
}else{
$updatestring="update clientes set nombre='".$form['nombre']."', apellido='".$form['apellido']."', fecha='".$form['fecha']."' where idclientes=".$form['id'].";";
}
@$query = pg_query($conn,$updatestring); if (! $query){
$respuesta->assign("er","innerHTML",$form['id']." no se almacenó: ".pg_last_error($conn)); }else{
$respuesta->assign("er","innerHTML",$form['id']." Se ha almacenado satisfactoriamente");
if($form['id']==""){
$respuesta->assign("id","value",$id[0]);
}
}
if (isset($form['nuevo'])) { foreach($form['nuevo'] as $i){
$c .= $i."<br>";
}
$respuesta->assign("obb","innerHTML","r: ".$c);
}
return $respuesta;
}
$xajax->registerFunction("leer_form");
$xajax->registerFunction("guardar_form");
$xajax->processRequests ();
function loadform(){
if ($_GET['id']!=""){
echo ' <script type="text/javascript" language="javascript"> xajax_leer_form('.$_GET['id'].'); </script>';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Leyendo desde PostgreSQL con xajax</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php
//En el <head> indicamos al objeto xajax se encargue de generar el javascript necesario
$xajax->printJavascript("xajax/");
?>
<script language="javascript">
function crea(){
//var Obj = document.createElement('input type="text" name="nuevo" value="one"');
var Obj = document.createElement('input');
Obj.setAttribute('type','text');
Obj.setAttribute('name','nuevo[]');
Obj.setAttribute('id','n');
//var f = document.getElementById('clientes');
document.forms['clientes'].appendChild(Obj);
/*var m = document.getElementById("marco");
m.appendChild(Obj);*/
}
function leer(event){
if(event.keyCode==13){
var obj=document.forms['clientes'].elements['id'];
xajax_leer_form(obj.value);
obj.focus();
}
//xajax_leer_form(clientes.elements['id'].value);
}
function men(obj){
var o = document.createElement('div');
o.style.width="300px";
o.style.height="30px";
//o.style.background="";
o.style.position="absolute";
o.innerHTML="d";
o.style.top=obj.offsetTop+"px";
document.body.appendChild(o);
o.innerHTML='<div style="filter: alpha (Opacity=60); opacity:0.6; background:lightblue;"><p style="margin:0px;color:black; font-weight: bold; ">texto texto texto texto</p></div>';
}
function carga(){
var obj = document.getElementById("estado");
obj.innerHTML='<img src="load.gif">';
}
function terminado(){
var obj = document.getElementById("estado");
obj.innerHTML="completado";
}
xajax.loadingFunction=carga;
xajax.doneLoadingFunction=terminado;
</script>
</head>
<body>
<form name="clientes" id="clientes" onkeypress="leer(event);">
<input type="text" id="id" name="id" >
<label for="nombre" >Nombre: <input type="text" id="nombre" name="nombre"></label>
<input type="text" id="apellido" name="apellido">
<input type="text" id="fecha" name="fecha">
<input type="button" id="s" value="enviar">
<a href="javascript:" onclick="xajax_leer_form(clientes.elements['id'].value); clientes.elements['id'].focus();">Cargar</a>
<a href="#" onclick="xajax_guardar_form(xajax.getFormValues('clientes'));clientes.elements['nombre'].focus();">Guardar</a>
<a href="javascript:" onclick="clientes.reset();clientes.elements['nombre'].focus();">Nuevo</a>
<a href="javascript: crea();">Nuevo e</a>
</form>
<div id="er" style="color:red;" onclick="men(this);"></div>
<div id="obb" style="color:blue;"></div>
<div id="ff" style="background: #333; position:absolute; width:30px; heigth:30px; top:300px; left:50px;">hg</div>
<div id="estado"></div>
<div id="marco" style="position:absolute; top:50px;left:50px; ">zxz</div>
<?php loadform(); ?>
</body>
</html>