<?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>Esteban Lombardo &#187; Tutorial</title>
	<atom:link href="http://estebanlombardo.com/category/tutorial-programmazione-web/feed/" rel="self" type="application/rss+xml" />
	<link>http://estebanlombardo.com</link>
	<description>...Just another Web programmer...</description>
	<lastBuildDate>Wed, 25 Nov 2009 15:24:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ajax: Richieste GET XMLHttp</title>
		<link>http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/</link>
		<comments>http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 15:27:55 +0000</pubDate>
		<dc:creator>Este</dc:creator>
				<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/</guid>
		<description><![CDATA[
Ci occuperemo di un semplice script scritto in Ajax che ci permette di visualizzare delle informazioni da un database,senza dover ricaricare la pagina web.
Questo procedimento ci permette di rendere la pagina più fluida,in modo da caricare solo le informazioni che ci servono,e non ltutta la pagina.
Questo tutorial è dedicato ai programmatori che già hanno una]]></description>
			<content:encoded><![CDATA[<p><a href="http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/"><img class="size-medium wp-image-17 alignleft" style="border: 0pt none; margin: 0px 2px;" title="Ajax Logo" src="http://estebanlombardo.com/wp-content/uploads/2008/04/18okt04_ajax_logo_150_rgb-284x300.jpg" alt="Ajax Logo" width="112" height="119" /></a></p>
<p>Ci occuperemo di un semplice script scritto in Ajax che ci permette di visualizzare delle informazioni da un database,senza dover ricaricare la pagina web.<br />
Questo procedimento ci permette di rendere la pagina più fluida,in modo da caricare <strong>solo</strong> le informazioni che ci servono,e non ltutta la pagina.</p>
<p>Questo tutorial è dedicato ai programmatori che già hanno una conoscenza basilare di <em>PHP</em> ed <em>SQL</em>.</p>
<p><span id="more-8"></span><br />
Per una questione di compatibilità con i browser,ci serve utilizzare una libreria (che potete scaricare <a title="Libreria zXml" href="http://estebanlombardo.com/download/tutorial/zxml.zip" target="_blank"><strong>qui</strong></a>) che ci permette di creare oggetti XMLHttp validi per tutti i browser.</p>
<p>Per questo tutorial potremmo utilizzare solo 3 file,ma per una questione di ordine,preferisco impostare il lavoro su 5 semplici file:</p>
<ol>
<li><span style="color: #000000;"><strong>config.php</strong></span> &#8211; Dove ci sono tutte le info per connettersi al database</li>
<li><span style="color: #000000;"><strong>script.js</strong></span> -Dove inseriremo il codice JavaScript</li>
<li><span style="color: #000000;"><strong>index.php</strong></span> &#8211; Dove si trova il form che manda la richiesta GET</li>
<li><span style="color: #000000;"><strong>getData.php</strong></span> -Dove si effettua la connessione al database</li>
<li><span style="color: #000000;"><strong>zxml.js</strong></span> &#8211; La libreria che avete scaricato prima</li>
</ol>
<p>In pratica il nostro obiettivo sarà semplicemente quello di aggiornare un tag <em>&lt;DIV&gt; </em>con delle informazioni che vengono prelevate dal database. Dal file<span style="color: #000000;"> <strong>index.php</strong></span> viene mandata la richiesta che passa da<strong> <span style="color: #000000;">getData.php</span></strong> per poi tornare nuovamente a <span style="color: #000000;"><strong>index.php</strong></span>,e tutto questo senza la necessità di ricaricare la pagina!</p>
<p>Iniziamo con la creazione della tabella del database eseguendo la seguente query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`persone`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> INT<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">11</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`nome`</span> VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">255</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`cognome`</span> VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">255</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`anni`</span> INT<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">11</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">`id`</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Successivamente creiamo il file <span style="color: #000000;"><strong>config.php</strong></span>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;tutorial&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sostituite a questi valori,quelli del vostro database! Se lo eseguite in locale con un programma come <a title="Easy-PHP" href="http://it.wikipedia.org/wiki/EasyPHP" target="_blank">Easy-PHP</a> dovrete cambiare solo il nome del database con quello creato da voi e lasciare gli altri valori invariati.  Occupiamoci adesso del file <span style="color: #000000;"><strong>index.php:</strong></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;script.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;!--</span>mce<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #339933;">--&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
&nbsp;
ID utente<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;id&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>input onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;getData()&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Invia&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>

<p>Il file <strong><span style="color: #000000;">script.js</span> </strong>ha al suo interno tutto il codice JavaScript che andremo ad usare ,che nel nostro esempio racchiude solo 1 funzione:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> sId <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> oXmlHttp <span style="color: #339933;">=</span> zXmlHttp.<span style="color: #660066;">createRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
oXmlHttp.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;getData.php?id=&quot;</span><span style="color: #339933;">+</span>sId<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
oXmlHttp.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;If-Modified-Since&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Sat, 1 Jan 2000 00:00:00 GMT&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
oXmlHttp.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oXmlHttp.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>oXmlHttp.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> divInfo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;info&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> divInfo.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> oXmlHttp.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> divInfo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;info&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
divInfo.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;C'è stato il seguente errore: &quot;</span> <span style="color: #339933;">+</span> oXmlHttp.<span style="color: #660066;">statusText</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
oXmlHttp.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Infine il file <strong><span style="color: #000000;">getData.php</span> </strong>che esegue la richiesta al database:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span>”Content<span style="color: #339933;">-</span>Type<span style="color: #339933;">:</span> text<span style="color: #339933;">/</span>plain”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>”config<span style="color: #339933;">.</span>php”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$sId</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_password</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;">$db</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span>”Errore nella connessione<span style="color: #339933;">.</span> Verificare i parametri nel <span style="color: #990000;">file</span> config<span style="color: #339933;">.</span>inc<span style="color: #339933;">.</span>php”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span>”Errore nella selezione del database<span style="color: #339933;">.</span> Verificare i parametri nel <span style="color: #990000;">file</span> config<span style="color: #339933;">.</span>inc<span style="color: #339933;">.</span>php”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> “SELECT <span style="color: #339933;">*</span> FROM persone WHERE id<span style="color: #339933;">=</span>’<span style="color: #000088;">$sID</span>’”<span style="color: #339933;">;</span> 
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sInfo</span><span style="color: #339933;">=</span>”
Nome<span style="color: #339933;">:</span> “<span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;nome&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span>”
Cognome<span style="color: #339933;">:</span> “<span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;nome&quot;</span><span style="color: #009900;">&#93;</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;">$sInfo</span> <span style="color: #339933;">=</span> “Nessuna persona trovata con il seguente ID”<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sInfo</span><span style="color: #339933;">;</span></pre></div></div>

<p>E questo è tutto! una volta richiesto l&#8217;ID tramite il <em>&#8220;form&#8221;</em> verrà aggiornato automaticamente il vostro <em>&lt;div&gt; </em>e verranno visualizzati ,<em>se esistono</em>,i dati prelevati nel database!</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 456px; width: 1px; height: 1px;">
<pre><code>&lt;pre lang="java" line="1"&gt;
</code></pre>
</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/" /></p>]]></content:encoded>
			<wfw:commentRss>http://estebanlombardo.com/2008/04/02/ajax-richieste-get-xmlhttp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
