<?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>Mas que Wordpress - Tutoriales, recursos web, JQuery y PHP, Wordpress , twitter y muchos recursos más &#187; selects anidados</title>
	<atom:link href="http://www.masquewordpress.com/tag/selects-anidados/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.masquewordpress.com</link>
	<description>Tutoriales y recursos web sobre JQuery , PHP, Wordpress , twitter. Aprende diseño web y programación</description>
	<lastBuildDate>Mon, 06 Feb 2012 15:02:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Selects anidados con AJAX y JQuery</title>
		<link>http://www.masquewordpress.com/selects-anidados-con-ajax-y-jquery/</link>
		<comments>http://www.masquewordpress.com/selects-anidados-con-ajax-y-jquery/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 03:18:30 +0000</pubDate>
		<dc:creator>Damian</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Tutoriales]]></category>
		<category><![CDATA[selects anidados]]></category>

		<guid isPermaLink="false">http://masquewordpress.com/?p=342</guid>
		<description><![CDATA[Los selects anidados o dependent cascading selects como dicen los ingleses , son usados hoy en día en la mayoria de formularios cuando nos registramos en una web. El ejemplo más comun es a la hora de elegir nuestro pais de procedencia , donde seguidamente carga en otro select las provincias de dicho pais. El [...]]]></description>
			<content:encoded><![CDATA[<p>Los <strong>selects anidados o dependent cascading selects</strong> como dicen los ingleses , son usados hoy en día en la mayoria de formularios cuando nos registramos en una web. El ejemplo más comun es a la hora de elegir nuestro pais de procedencia , donde seguidamente carga en otro select las provincias de dicho pais. El otro día precísamente <a title="Listado de paises + provincias" href="http://masquewordpress.com/recursos/listado-completo-de-paises-y-provincias/">publique un listado completo de paises + provincias</a>.</p>
<p>Hacer esto con JQuery es más que sencillo. Tan solo necesitaremos 3 archivos:</p>
<p><strong>index.php</strong> Donde tendremos un formulario simple con sus correspondientes selects.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cargarProvincias.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>   
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span> 
<span style="color: #009900;">&lt;? </span>
<span style="color: #009900;">   include_once <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'db.php'</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">   $conn<span style="color: #66cc66;">=</span> get_db_conn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #009900;">$paises<span style="color: #66cc66;">=</span>mysql_query<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * from paises&quot;</span>,$conn<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">   ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;niceform&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dl</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>  	  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rojo&quot;</span>&gt;</span>*<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> Pais :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
          	  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>	 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pais&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pais&quot;</span>  <span style="color: #000066;">onchange</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cargarProvincias();&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
                                 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;null&quot;</span>&gt;</span>Selecciona un pais<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
           	 <span style="color: #009900;">&lt;? 	          while<span style="color: #66cc66;">&#40;</span>$row<span style="color: #66cc66;">=</span>mysql_fetch_assoc<span style="color: #66cc66;">&#40;</span>$paises<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">					print <span style="color: #ff0000;">'&lt;option value=&quot;'</span>.$row<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #ff0000;">'&quot; &gt;</span></span>'.$row['pais'].'<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>';
											}?&gt;
				<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
                 <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dl</span>&gt;</span>                                                  
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dl</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rojo&quot;</span>&gt;</span>*<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> Provincia :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dt</span>&gt;</span>
           		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>	 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;provincia&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;provincia&quot;</span>  <span style="color: #66cc66;">/</span>&gt;</span>
                                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;null&quot;</span>&gt;</span>Selecciona un pais<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
           			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dd</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">dl</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Un archivo encargado de buscar las provincias según el pais indicado. Primero se <a href="http://masquewordpress.com/php/dbphp-como-definir-los-parametros-de-conexion-de-tu-base-de-datos/">conectará a la base de datos</a>, y luego devolverá los resultados.</p>
<p><strong>ajax_provincias.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> 
   <span style="color: #b1b100;">include_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$conn</span><span style="color: #339933;">=</span> get_db_conn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?</span> 
<span style="color: #000088;">$conn</span><span style="color: #339933;">=</span>get_db_conn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pais</span><span style="color: #339933;">=</span> cleanQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pais'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$provincias</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM estados WHERE relacion = '<span style="color: #006699; font-weight: bold;">$pais</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$provincias</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;option value=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'estado'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Y por último el código JQuery que hará la magia de pasar el Pais seleccionado al archivo <strong>ajax_provincias.php</strong> mediante <strong>AJAX </strong>y luego cargará los valores devueltos dentro del segundo <strong>SELECT</strong><br />
<strong><br />
cargarProvincias.js</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> cargarProvincias<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#provincia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;option selected&gt;Cargando&lt;/option&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #003366; font-weight: bold;">var</span> idPais<span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pais'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> toLoad<span style="color: #339933;">=</span> <span style="color: #3366CC;">'cargar_provincias.php?pais='</span><span style="color: #339933;">+</span> idPais <span style="color: #339933;">;</span>
	$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>toLoad<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>responseText<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#provincia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>responseText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
							<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Para terminar , le pueden echar una ojeada al <a href="http://masquewordpress.com/ejemplos/selects-anidados-con-ajax/">DEMO ONLINE</a><br />
Y <a href="http://masquewordpress.com/ejemplos/selects-anidados-con-ajax.zip">descargar el código</a> del ejemplo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masquewordpress.com/selects-anidados-con-ajax-y-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

