<?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>slacko.org &#187; Php</title>
	<atom:link href="http://www.slacko.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.slacko.org</link>
	<description>Un blog il cui autore ha deciso di farsi la barba con il rasoio di Occam ma non sa ancora che schiuma usare</description>
	<lastBuildDate>Thu, 22 Sep 2011 10:45:52 +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>Suggerimenti con jQuery e un pizzico di PHP</title>
		<link>http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/</link>
		<comments>http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 16:26:09 +0000</pubDate>
		<dc:creator>slacko</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.slacko.org/?p=365</guid>
		<description><![CDATA[Avendo bisogno di un bell&#8217;effetto per un form ho messo mano a jQuery per creare l&#8217;esempio di suggerimento in stile Google Suggest che è spiegato di seguito. Iniziamo con lo scheletro della pagina HTML. Molto semplice, due div e una &#8230; <a href="http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Avendo bisogno di un bell&#8217;effetto per un form ho messo mano a jQuery per creare l&#8217;esempio di suggerimento in stile Google Suggest che è spiegato di seguito.</p>
<p>Iniziamo con lo scheletro della pagina HTML. Molto semplice, due div e una textbox.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
				Scrivi le prime lettere del paese:
&nbsp;
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inputString&quot;</span> <span style="color: #000066;">onkeyup</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lookup(this.value);&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;suggestions&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;suggestionsBox&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display: none;&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position: relative; top: -12px; left: 30px;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upArrow.png&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upArrow&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Vediamo ora la parte javascript.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.2.1.pack.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">function</span> lookup<span style="color: #009900;">&#40;</span>inputString<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>inputString.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// se non è stato inserito nessun carattere nascondiamo il div che contiene i suggerimenti</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#suggestions'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rpc.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>queryString<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">+</span>inputString<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<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>data.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#suggestions'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#autoSuggestionsList'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// lookup dei suggerimenti</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> fill<span style="color: #009900;">&#40;</span>thisValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">//riempi la textbox con il risultato e poi nascondi i suggerimenti</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#inputString'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>thisValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$('#suggestions').hide();&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>In pratica il compito della prima funzione è quello di raccogliere l&#8217;input della textbox e passarlo (in POST) allo script PHP utilizzando la chiamata $.post di jQuery.<br />
Tale funzione prende 3 parametri: l&#8217;url a cui <em>postare </em>i dati, il dato da postare e una funzione di callback da eseguire <em>di ritorno</em>.<br />
Infatti se la funzione di callback ritorna qualcosa solo allora è mostrata la lista dei suggerimenti</p>
<p>Infine una funzione <em>fill </em>che riempie la textbox con il suggerimento selezionato.</p>
<p>Lo script PHP è ampiamente commentato ed in pratica consiste in una funzione che prende in argomento la stringa presente in POST, esegue la query al database ritornando i suggerimenti disponibili.</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
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
&nbsp;
	<span style="color: #666666; font-style: italic;">// mysqli('url', 'user', 'pass', 'database');</span>
	<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span> <span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'prove'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ERRORE: Impossibile connettersi al DB.'</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: #666666; font-style: italic;">// controllo se c'è qualche stringa con cui effettuare la query</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'queryString'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'queryString'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//se la stringa è più lunga di 0...</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryString</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// usiamo LIKE con il carattere jolly % alla fine... </span>
				<span style="color: #666666; font-style: italic;">//quind se il valore di querystring è &quot;it&quot;</span>
				<span style="color: #666666; font-style: italic;">// allora verranno cercati i valori che iniziano con &quot;it&quot; e sono seguiti da altri n caratteri</span>
&nbsp;
				<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT value FROM countries WHERE value LIKE '<span style="color: #006699; font-weight: bold;">$queryString</span>%' LIMIT 10&quot;</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;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// finquando ho dei risultati li piazzo in un oggetto</span>
					<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// formattiamo i risultati mettendoli in una lista</span>
						<span style="color: #666666; font-style: italic;">// quindi riempiamo la textbox con la funzione Fill</span>
&nbsp;
	         			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li onClick=&quot;fill(\''</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\');&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
	         		<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'ERRORE: C\'è stato un problema con la query.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span> 
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Attenzione, non permettere l\'accesso diretto a questo script'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Alla fine ho condito il tutto con un po&#8217; di CSS3 (giusto per vedere l&#8217;effetto che fa&#8230;)<br />
L&#8217;esempio funzionante lo trovate <a href="http://www.slacko.org/suggester">qui</a></p>
<p>Per qualsiasi problema commentate pure.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Suggerimenti+con+jQuery+e+un+pizzico+di+PHP+http%3A%2F%2Fslacko.org%2F%3Fp%3D365" title="Post to Twitter"><img class="nothumb" src="http://www.slacko.org/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Suggerimenti+con+jQuery+e+un+pizzico+di+PHP+http%3A%2F%2Fslacko.org%2F%3Fp%3D365" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2008. |
<a href="http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/">Permalink</a> |
<a href="http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/#comments">4 commenti</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/&title=Suggerimenti con jQuery e un pizzico di PHP">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2008/11/01/suggerimenti-con-jquery-e-un-pizzico-di-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>File upload in Ajax</title>
		<link>http://www.slacko.org/2008/07/08/file-upload-in-ajax/</link>
		<comments>http://www.slacko.org/2008/07/08/file-upload-in-ajax/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 20:29:44 +0000</pubDate>
		<dc:creator>slacko</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Varie ed eventuali]]></category>

		<guid isPermaLink="false">http://www.slacko.org/?p=265</guid>
		<description><![CDATA[Con questo breve tutorial vedremo come realizzare un elementare sistema di file upload usando PHP e Ajax. A dire la verità non è possibile realizzare un sistema di file-upload in Ajax puro a causa delle limitazioni di Javascript (su cui &#8230; <a href="http://www.slacko.org/2008/07/08/file-upload-in-ajax/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Con questo breve tutorial vedremo come realizzare un elementare sistema di file upload usando PHP e Ajax.<br />
A dire la verità non è possibile realizzare un sistema di file-upload in Ajax puro a causa delle limitazioni di Javascript (su cui si basa Ajax) in campo di sicurezza.<br />
La soluzione è quella di simulare l&#8217;effetto asincrono tramite un iframe.</p>
<p>Abbiamo quindi bisogno di:</p>
<ul>
<li>Un form HTML</li>
<li>Un javascript che esegua una animazione, chiamato al submit del form.</li>
<li>Uno script PHP che esegua l&#8217;upload vero e proprio</li>
<li>Un iframe invisibile nella stessa pagina del form</li>
</ul>
<p>Il form HTML è davvero semplice, c&#8217;è un campo per la scelta del file e un bottone.<br />
Da notare il parametro <em>enctype</em> che indica il tipo di codifica.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span> File: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myfile&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submitBtn&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #66cc66;">/</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></div></div>

<p>A questa pagina c&#8217;è da aggiungere un blocco che mostrerà l&#8217;animazione durante l&#8217;upload e per dare la conferma all&#8217;utente che l&#8217;operazione è riuscita. Inoltre, come già detto, aggiungiamo un iframe invisibie da usare come target del form.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;f1_upload_process&quot;</span>&gt;</span>Loading...
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loader.gif&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;result&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span> File: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myfile&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submitBtn&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload_target&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload_target&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:0;height:0;border:0px solid #fff;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span></pre></div></div>

<p>Scriviamo adesso la semplice funzione javascript che rende il blocco contenente l&#8217;animazione visibile sulla pagina.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">      <span style="color: #003366; font-weight: bold;">function</span> startUpload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'visible'</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>E di riflesso scriviamo anche la funzione che verrà chiamata al termine del processo per mostrare un messaggio all&#8217;utente e fermare l&#8217;animazione.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">      <span style="color: #003366; font-weight: bold;">function</span> stopUpload<span style="color: #009900;">&#40;</span>success<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>success <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'result'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span>
      <span style="color: #3366CC;">'&lt;span class=&quot;msg&quot;&gt;The file was uploaded successfully!&lt; <span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br /&gt;&lt;br /&gt;'</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>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'result'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span>
      <span style="color: #3366CC;">'&lt;span class=&quot;emsg&quot;&gt;There was an error during file upload!&lt; <span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br /&gt;&lt;br /&gt;'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>span<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Visto che ci siamo diamo un po&#8217; di stile al nostro form con qualche regola CSS</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">      <span style="color: #cc00cc;">#f1_upload_process</span><span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">100</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">visibility</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">400px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
      <span style="color: #00AA00;">&#125;</span>
&nbsp;
      form<span style="color: #00AA00;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">390px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
      <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Ora passiamo alla parte server.<br />
Dovrebbe essere abbastanza semplice da capire, spieghiamo comunque qualcosa. Prima di tutto andiamo a settare il percorso di destinazione del file. Poi introduciamo una variabile che ci conterrà l&#8217;eventuale errore in fase di upload del file.<br />
Procediamo quindi a spostare il file dalla directory temporanea alla sua destinazione finale.<br />
Il codice PHP dovrebbe essere una cosa del genere</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000088;">$destination_path</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$target_path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$destination_path</span> <span style="color: #339933;">.</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</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: #339933;">@</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target_path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Come visto nelle proprietà del form il codice PHP verrà eseguito all&#8217;interno dell&#8217;iframe nascosto. A questo punto non ci rimane che aggiungere al suo interno il codice js che chiamerà la funzione scritta precedentemente e che notificherà l&#8217;utente se l&#8217;upload è andato a buone fine o meno</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">      <span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
      window.<span style="color: #660066;">top</span>.<span style="color: #660066;">window</span>.<span style="color: #660066;">stopUpload</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span> <span style="color: #339933;">?</span>php echo $result<span style="color: #339933;">;</span> <span style="color: #339933;">?&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Cosa vi avevo detto? Facile e veloce.<br />
Se vi siete persi qualche passaggio o non vi è del tutto chiaro il funzionamento, se volete farmi i complimenti o mandarmi a cag*re, se volete invitarmi a cena fuori fatelo pure nei commenti.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=File+upload+in+Ajax+http%3A%2F%2Fslacko.org%2F%3Fp%3D265" title="Post to Twitter"><img class="nothumb" src="http://www.slacko.org/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=File+upload+in+Ajax+http%3A%2F%2Fslacko.org%2F%3Fp%3D265" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2008. |
<a href="http://www.slacko.org/2008/07/08/file-upload-in-ajax/">Permalink</a> |
<a href="http://www.slacko.org/2008/07/08/file-upload-in-ajax/#comments">8 commenti</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2008/07/08/file-upload-in-ajax/&title=File upload in Ajax">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2008/07/08/file-upload-in-ajax/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Form con validazione in Ajax con mootols 3/3</title>
		<link>http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/</link>
		<comments>http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 20:06:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/</guid>
		<description><![CDATA[Eccoci finalmente arrivati all&#8217;ultima parte di questo tutorial, come capirete si tratta solo di impostare la pagina HTML che conterrà i nostri scritp Ajax e PHP. Per prima cosa creiamo un file index.php, dentro ci mettiamo prima la chiamata alla &#8230; <a href="http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Eccoci finalmente arrivati all&#8217;ultima parte di questo tutorial, come capirete si tratta solo di impostare la pagina HTML che conterrà i nostri scritp Ajax e PHP.</p>
<p>Per prima cosa creiamo un file index.php, dentro ci mettiamo prima la chiamata alla libreria mootools, poi il codice js della <a href="http://www.slacko.org/2007/09/07/form-con-validazione-in-ajax-con-mootools-13/" target="_blank">prima</a> lezione.</p>
<p>Fatto questo possiamo mettere su un bel form html condito con un po di CSS per renderlo più attraente, l&#8217;importate è che vi ricordiate di mettere nel attributo <em>target</em> del tag <em>form</em> il valore register.php, cioè il nome della pagina creata nella <a href="http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/" target="_blank">seconda</a> lezione.</p>
<p>Il risultato lo potete vedere <a href="http://www.slacko.org/ajaxform/index.php" target="_blank">qui</a>!</p>
<p>Per qualsiasi informazione, dubbio, chiarimento e affini non esitate a commentare.</p>
<p class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9a416370-7e56-4fc6-9bbd-ebd02f319fa1" style="margin: 0px; padding: 0px; display: inline">Technorati Tag:  		<a href="http://technorati.com/tags/javascript/" rel="tag">javascript</a> 		,  		<a href="http://technorati.com/tags/php/" rel="tag">php</a> 		,  		<a href="http://technorati.com/tags/mootols/" rel="tag">mootols</a> 		,  		<a href="http://technorati.com/tags/tutorial/" rel="tag">tutorial</a></p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Form+con+validazione+in+Ajax+con+mootols+3%2F3+http%3A%2F%2Fslacko.org%2F%3Fp%3D189" title="Post to Twitter"><img class="nothumb" src="http://www.slacko.org/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Form+con+validazione+in+Ajax+con+mootols+3%2F3+http%3A%2F%2Fslacko.org%2F%3Fp%3D189" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2007. |
<a href="http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/">Permalink</a> |
<a href="http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/#comments">1 commento</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/&title=Form con validazione in Ajax con mootols 3/3">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2007/10/29/form-con-validazione-in-ajax-con-mootols-33/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Form con validazione in Ajax con mootools 2/3</title>
		<link>http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/</link>
		<comments>http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 15:46:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/</guid>
		<description><![CDATA[Nel prcecedente tutorial abbiamo visto come usare la libreria js mootools e far effettuare una richiesta asincrona. Oggi vedremo come creare il &#34;motore&#34; su cui si basa la validazione del nostro form; per prima cosa controlleremo se l&#8217;utente ha inserito &#8230; <a href="http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Nel prcecedente tutorial abbiamo visto come usare la libreria js <strong>mootools</strong> e far effettuare una richiesta asincrona.</p>
<p>Oggi vedremo come creare il <em>&quot;motore&quot;</em> su cui si basa la validazione del nostro form; per prima cosa controlleremo se l&#8217;utente ha inserito tutti i dati, poi se le password inserite combaciano ed infine se l&#8217;indirizzo e-mail &#xE8; valido.</p>
<blockquote><p>&lt;?php      <br />&#xA0;&#xA0;&#xA0; include(&#8216;functions.php&#8217;); </p>
<p>&#xA0;&#xA0;&#xA0; if ($_POST['Nome']==&#8221; || strlen($_POST['Nome'])&lt;3)      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Campo obbligatorio, il nome deve contenere almeno 3 caratteri&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if ($_POST['Cognome']==&#8221; || strlen($_POST['Cognome'])&lt;3)      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Campo obbligatorio, il cognome deve contenere almeno 3 caratteri&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if ($_POST['Username']==&#8221; || alpha_numeric($_POST['Username'])==FALSE)      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Username obbligatorio, deve contenere valori alfanumerici&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if ($_POST['Password']==&#8221; || alpha_numeric($_POST['Password'])==FALSE)      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Password obbligatoria, deve contenere valori alfanumerici&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if ($_POST['Password']!=$_POST['re_Password'])      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Le due password non corrispondono&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if (valid_email($_POST['Email'])==FALSE)      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; $errors[] = &#8216;Per piacere inserisci un indirizzo valido&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; if(is_array($errors))      <br />&#xA0;&#xA0;&#xA0; {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; echo &#8216;&lt;p class=&quot;error&quot;&gt;&lt;b&gt;Si sono verificati i seguenti errori&lt;/b&gt;&lt;/p&gt;&#8217;;       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; while (list($key,$value) = each($errors))       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; { </p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; echo &#8216;&lt;span class=&quot;error&quot;&gt;&#8217;.$value.&#8217;&lt;/span&gt;&lt;br /&gt;&#8217;;      <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; }       <br />&#xA0;&#xA0;&#xA0; }       <br />&#xA0;&#xA0;&#xA0; else {       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; //fai qualcosa,       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; echo &#8216;&lt;p&gt;&lt;b&gt;OK!&lt;/b&gt;&lt;/p&gt;&#8217;;       <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; echo &#8216;&lt;span&gt;La tua registrazione &#xE8; avvenuta con successo!&lt;/span&gt;&#8217;;       <br />&#xA0;&#xA0;&#xA0; } </p>
<p>?&gt;</p>
</blockquote>
<p>Il codice proposto include un file <em>&quot;functions.php&quot;</em> che contiene la definizione delle funzioni <em>&quot;alphanumeric&quot;</em> e <em>&quot;valid_email&quot;</em> utili alla validazione. Successivamente controlliamo che nome e cognome siano stringhe di almeno 3 caratteri, in caso contrario memorizziamo l&#8217;errore in un array chiamato <em>&quot;errors&quot;</em>, controlliamo quindi che lo username e la password siano alfanumeriche e le 2 password combacino, infine controlliamo che l&#8217;e-mail sia valida mettendo la descrizione degli errori nell&#8217;array questo perch&#xE8; ci potrebbero essere pi&#xF9; errori contemporaneamente e quindi alla fine conviene mostrare il contenuto dell&#8217;array tramite un ciclo while, altrimenti se l&#8217;array non &#xE8; stato creato (e quindi non ci sono errori) mostriamo un messaggio che indica all&#8217;utente l&#8217;avvenuta registrazione.</p>
<p>Come detto vengono utilizzate 2 funzioni per la validazione di utente, password ed email, eccovi il codice:</p>
<blockquote><p>&lt;?php </p>
<p>&#xA0;&#xA0;&#xA0; function alpha_numeric($str)     <br />&#xA0;&#xA0;&#xA0; {      <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; return ( ! preg_match(&quot;/^([-a-z0-9])+$/i&quot;, $str)) ? FALSE : TRUE;      <br />&#xA0;&#xA0;&#xA0; } </p>
<p>&#xA0;&#xA0;&#xA0; function valid_email($str)     <br />&#xA0;&#xA0;&#xA0; {      <br />&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; return ( ! preg_match(&quot;/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix&quot;, $str)) ? FALSE : TRUE;      <br />&#xA0;&#xA0;&#xA0; } </p>
<p>?&gt;</p>
</blockquote>
<p>semplicemente valutano la stringa che viene loro passata tramite con una espressione regolare e restituiscono il relativo valore di verit&#xE0;.</p>
<p>Per un approfondimento sulle espressioni regolari in PHP fate riferimento a questi articoli <a href="http://php.html.it/articoli/leggi/851/espressioni-regolari/1/" target="_blank">qui</a> e <a href="http://it.php.net/manual/it/function.preg-match.php" target="_blank">qui</a>.</p>
<p>La prossima volta metteremo tutto insieme e vedremo all&#8217;opera quanto creato fin&#8217;ora, vi aspetto!</p>
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3377507e-23c1-4f20-8680-01bb24a93fc0" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tag:  		<a href="http://technorati.com/tags/php/" rel="tag">php</a> 		,  		<a href="http://technorati.com/tags/javascript/" rel="tag">javascript</a> 		,  		<a href="http://technorati.com/tags/form/" rel="tag">form</a> 		,  		<a href="http://technorati.com/tags/validazione/" rel="tag">validazione</a> 		,  		<a href="http://technorati.com/tags/mootols/" rel="tag">mootols</a> 		</div>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Form+con+validazione+in+Ajax+con+mootools+2%2F3+http%3A%2F%2Fslacko.org%2F%3Fp%3D184" title="Post to Twitter"><img class="nothumb" src="http://www.slacko.org/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Form+con+validazione+in+Ajax+con+mootools+2%2F3+http%3A%2F%2Fslacko.org%2F%3Fp%3D184" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2007. |
<a href="http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/">Permalink</a> |
<a href="http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/#comments">2 commenti</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/&title=Form con validazione in Ajax con mootools 2/3">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2007/09/19/form-con-validazione-in-ajax-con-mootools-23/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Un semplice esempio di geo-tagging</title>
		<link>http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/</link>
		<comments>http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/#comments</comments>
		<pubDate>Wed, 28 Feb 2007 17:41:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/</guid>
		<description><![CDATA[per Geotagging si intende il processo di aggiunta di una identificazione geografica ai dati (o metadati). Quello che faremo perÃ² sarÃ un semplice sistema che sfruttando le API di HostIp ci fornirÃ le informazioni riguardo la nazione e la cittÃ &#8230; <a href="http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>per <strong>Geotagging</strong> si intende il processo di aggiunta di una identificazione geografica ai dati (o metadati).</p>
<p>Quello che faremo perÃ² sarÃ  un semplice sistema che sfruttando le <a href="http://it.wikipedia.org/wiki/Application_programming_interface">API</a> di <a href="http://www.hostip.info">HostIp</a> ci fornirÃ  le informazioni riguardo la nazione e la cittÃ  dei visitatori del nostro sito web.</p>
<p>Gli ingredienti necessari sono:</p>
<ul>
<li><a href="http://www.php.net">PHP</a></li>
<li>Web server per provare il nostro script</li>
<li>Le API di HostIp reperibili <a href="http://www.hostip.info/use.html">qui</a></li>
<li>Un editor di testo</li>
<li>Sale e pepe quanto basta <img src='http://www.slacko.org/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </li>
</ul>
<p>Cominciamo da HostIp, Ã¨ un progetto nato al fine di geo-localizzare gli ip il cui database Ã¨ disponibili al pubblico tramite l&#8217;utilizzo di semplici API.</p>
<p>Vogliamo quindi realizzare un script che ci indichi la provenienza del nostro visitatore, quindi per prima cosa ci serve conoscere il suo ip, niente di piÃ¹ semplice<br />
<code>$ip=$_SERVER['REMOTE_ADDR'];</code><br />
poi se avete dato un occhiata alle API avrete notato come HostIp costruisce un URL del tipo http://api.hostip.info/get_html.php?ip= indirizzo del client.<br />
Quindi costruiamo l&#8217;URL e la mettiamo in una variabile<br />
<code>$url = "http://www.hostip.info/api/get.html?ip=".$ip;</code><br />
vi sembrerÃ  strano ma il piÃ¹ Ã¨ fatto, ora ci basta acquisire il contenuto della nostra URL in un vettore i cui elementi sono una riga del file tramite la funzione <em>file</em> (che fantasia)<br />
<code>$result = file($url); </code><br />
e adesso stampiamo il tutto con un ciclo<br />
<code>foreach($result as $line_num=&gt;$line){<br />
echo ($line."&lt;br/&gt;");<br />
}</code><br />
Questo potrebbe giÃ  bastare in quanto mostra Nazione e CittÃ  del client (anche se a volte il risultato non Ã¨ proprio preciso) ma possiamo aggiungere qualcosina, come ad esempio visualizzare la bandiera della nazione.<br />
HostIp fornisce un semplice meccanismo per far questo sempre sottoforma di un URL costruita ad-hoc del tipo http://api.hostip.info/flag.php?ip= indirizzo del visitatore.<br />
Quindi creiamo un immagine con il &lt;img&gt; e all&#8217;attributo <em>src</em> diamo l&#8217;URL con l&#8217;ip che ci interessa<br />
<code><br />
echo("&lt;A href=\"http://www.hostip.info\"&gt;");<br />
echo("&lt;IMG src=\"http://api.hostip.info/flag.php?ip=".$ip."ALT=\"IP Address Lookup\"&gt;");<br />
echo("&lt;/A&gt;");<br />
</code></p>
<p>Ed ecco <a href="http://www.slacko.org/geoip.php">qui</a> un esempio.<br />
Davvero semplice, nel prossimo tutorial spero di poter mostrare come ottenere anche longitudine e latitudine per visualizzare la posizione all&#8217;interno di <a href="http://maps.google.it/" target="_blank">Google Maps</a>, quindi alla prossima!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Un+semplice+esempio+di+geo-tagging+http%3A%2F%2Fslacko.org%2F%3Fp%3D74" title="Post to Twitter"><img class="nothumb" src="http://www.slacko.org/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Un+semplice+esempio+di+geo-tagging+http%3A%2F%2Fslacko.org%2F%3Fp%3D74" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2007. |
<a href="http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/">Permalink</a> |
<a href="http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/#comments">Nessun commento</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/&title=Un semplice esempio di geo-tagging">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2007/02/28/un-semplice-esempio-di-geo-tagging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

