<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>soft·in·spain &#187; jquery</title>
	<atom:link href="http://softinspain.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://softinspain.com</link>
	<description>negocios y software desde españa</description>
	<lastBuildDate>Thu, 19 May 2011 16:46:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tratamiento del AJAX en un solo fichero</title>
		<link>http://softinspain.com/desarrollo/tratamiento-del-ajax-en-un-solo-fichero/</link>
		<comments>http://softinspain.com/desarrollo/tratamiento-del-ajax-en-un-solo-fichero/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 09:05:26 +0000</pubDate>
		<dc:creator>Jose Alberto</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[arcadina]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://softinspain.com/?p=608</guid>
		<description><![CDATA[Últimamente estoy desarrollando sobre todo con PHP+JavaScript (jQuery). En un proyecto interno de Arcadina, que hace un uso intensivo del AJAX, decidí probar un planteamiento diferente del utilizado hasta ahora. Normalmente las llamadas a la parte AJAX del servidor se hacen a diferentes scripts PHP en función de lo que deseamos obtener (query) o guardar [...]


No hay posts relacionados.]]></description>
			<content:encoded><![CDATA[	<p>Últimamente estoy desarrollando sobre todo con PHP+JavaScript (jQuery). En un proyecto interno de <a href="http://www.arcadina.com">Arcadina</a>, que hace un uso intensivo del <a href="http://es.wikipedia.org/wiki/AJAX">AJAX</a>, decidí probar un planteamiento diferente del utilizado hasta ahora.</p>
	<p>Normalmente las llamadas a la parte AJAX del servidor se hacen a diferentes scripts PHP en función de lo que deseamos obtener (<em>query</em>) o guardar (<em>submit</em>). Por ejemplo, si estamos tratando una lista de clientes podemos llamar a <code>get_cliente.php</code> para solicitar los datos de un cliente pasando su identificador, podemos llamar a <code>delete_cliente.php</code> para eliminarlo o a <code>save_cliente.php</code> para guardar cambios en la ficha, etc. (tengo la manía de utilizar <em>spanglish</em> en el nombre de funciones, procedimientos, etc.). Ejemplo de llamada según esté método:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Descargar <a href="http://softinspain.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=608&amp;download=form1.php">form1.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6085"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p608code5"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ejemplo1&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/api/save_cliente.php&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;id</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>
Nombre<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nombre&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;nombre</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
EMail<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;email</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

	<p>Según el proyecto va avanzando nos podemos encontrar con decenas de scripts: <code>get_&lt;em&gt;loquesea&lt;/em&gt;</code>, <code>delete_&lt;em&gt;loquesea&lt;/em&gt;</code>.... En cada uno de ellos añadir el código de validación, los includes, etc. Se me ocurrió centralizarlo en dos ficheros <code>ajaxsubmit.php</code> y <code>ajaxquery.php</code>, y utilizar un parametro para indicar la acción a realizar. Ejemplo de un formulario utilizando este sistema:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Descargar <a href="http://softinspain.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=608&amp;download=form2.php">form2.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6086"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p608code6"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ejemplo2&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/api/ajaxsubmit.php&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ajaxsubmit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;save_cliente&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;id</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;</span>
Nombre<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nombre&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;nombre</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
EMail<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?= <span style="color: #006699; font-weight: bold;">$cliente-&gt;email</span> ?&gt;&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

	<p>Como se ve tampoco es que varíe mucho en la parte que se envía al navegador, se añade un nuevo campo oculto &#8220;ajaxsubmit&#8221; que indica la acción que queremos realizar. Veamos el <code>ajaxsubmit.php</code>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Descargar <a href="http://softinspain.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=608&amp;download=ajaxsubmit.php">ajaxsubmit.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6087"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
</pre></td><td class="code" id="p608code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Incluimos código mínimo de autenticación y librerías</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'includes.inc.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Si no está logeado el usuario, devolvemos error</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario_login</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario_login</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'nok'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Error: primero debe identificarse'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'nok'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Error no identificado'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$ajaxsubmit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajaxsubmit'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// &quot;AS_&quot; viene de AjaxSubmit ... ;)	</span>
	<span style="color: #000088;">$funcion</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'AS_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ajaxsubmit</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Comprobamos que tenemos una función con ese nombre</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$funcion</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$funcion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Llamamos a la función AS_ que indique el ajaxsubmit</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'nok'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ajaxsubmit</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'No se ha indicado &quot;ajaxsubmit&quot;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'AjaxSubmit: orden &quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ajaxsubmit</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; no reconocida.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Si el resultado es NOK registramos en el log de errores</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'nok'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">error_log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*** AJAXSUBMIT - error - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ---------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">// Devolvemos respuesta en formato JSON</span>
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Services_JSON<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$json</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// =======================================================================================</span>
<span style="color: #666666; font-style: italic;">// Procesos de guardar, eliminar, actualizar, etc...</span>
<span style="color: #666666; font-style: italic;">// =======================================================================================</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> AS_save_cfg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$respuesta</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$cfg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$clave</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$valor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Comprobar si $clave empieza por 'cfg_'</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'^cfg_'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clave</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^cfg_&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clave</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$valor</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$errormsg</span> <span style="color: #339933;">=</span> SaveConfig<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$errormsg</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ok'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ok'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Configuración guardada'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'nok'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$errormsg</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> AS_save_cliente<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$respuesta</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Código simplificado para el ejemplo: se debería</span>
	<span style="color: #666666; font-style: italic;">// añadir validación y protección contra SQL injection</span>
	<span style="color: #000088;">$cliente</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cliente<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$cliente</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nombre</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nombre'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$cliente</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$errormsg</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cliente</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$errormsg</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ok'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ok'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Cliente guardado'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resultado'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'nok'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$respuesta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mensaje'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$errormsg</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

	<p>La &#8220;gracia&#8221; del código está en ésta parte:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p608code8'); return false;">Ver código</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6088"><td class="line_numbers"><pre>19
20
21
22
23
24
25
</pre></td><td class="code" id="p608code8"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$ajaxsubmit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajaxsubmit'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// &quot;AS_&quot; viene de AjaxSubmit ... ;)	</span>
	<span style="color: #000088;">$funcion</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'AS_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ajaxsubmit</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Comprobamos que tenemos una función con ese nombre</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$funcion</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$funcion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Llamamos a la función AS_ que indique el ajaxsubmit</span></pre></td></tr></table></div>

	<p>Lo que se envia en la variable &#8220;ajaxsubmit&#8221; es el nombre de la función a llamar, pero la precedemos de &#8220;AS_&#8221; para evitar conflictos con otros nombres de funciones. Entonces gracias a la <em>magia</em> de PHP, porque es un lenguaje de script, aprovechamos la variable <code>$funcion</code> para realizar una llamada a una funcion: <code>$funcion()</code></p>
	<p>¿Qué ventajas tiene este sistema? Tenerlo centralizado, de esa forma, añadir una capa de autenticación o variar el formato de salida de JSON a XML, por ejemplo, es mucho más sencillo. Evidentemente si alguna función es compleja se puede separar en un fichero aparte y hacer el correspondiente <code>include</code>, que además lo recomiendo.</p>
	<p>Para facilitar la labor de serializar los inputs de un formulario y enviarlo por ajax estoy utilizando el plugin <a href="http://malsup.com/jquery/form/">ajaxForm</a> para jQuery.</p>


 

<p>No hay posts relacionados.</p>]]></content:encoded>
			<wfw:commentRss>http://softinspain.com/desarrollo/tratamiento-del-ajax-en-un-solo-fichero/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

