<?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; Javascript</title>
	<atom:link href="http://www.slacko.org/category/js/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>Twitter Hack</title>
		<link>http://www.slacko.org/2009/01/12/twitter-hack/</link>
		<comments>http://www.slacko.org/2009/01/12/twitter-hack/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 02:10:00 +0000</pubDate>
		<dc:creator>slacko</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.slacko.org/2009/01/12/twitter-hack/</guid>
		<description><![CDATA[Di sfuggita ho visto un piccolo hack per Twitter qui. In pratica si tratta di mostrare su una propria pagina web lo status di un visitatore. Tramite javascript si effettua una chiamata alle API di Twitter, è quindi possibile ottenere &#8230; <a href="http://www.slacko.org/2009/01/12/twitter-hack/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Di sfuggita ho visto un piccolo hack per Twitter <a href="http://www.wait-till-i.com/2009/01/05/detecting-and-displaying-the-information-of-a-logged-in-twitter-user/">qui</a>.</p>
<p>In pratica si tratta di mostrare su una propria pagina web lo status di un visitatore.</p>
<p>Tramite javascript si effettua una chiamata alle API di Twitter, è quindi possibile ottenere sul client i dati relativi all’utente.</p>
<p>L’utilità di questo sta, ad esempio, nella creazione di widget del tipo <em>Twit-this, </em>che permettono all’utente di <em>twittare</em> la pagina su cui sta navigando.</p>
<p>La funzione richiamata è <em>user_timeline</em> e quindi si ha una cosa del genere</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span>
src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;'http://twitter.com/statuses/user_timeline.json&amp;amp;count=1&amp;callback=TwitterData&quot;</span><span style="color: #339933;">&gt;;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Quindi la funzione richiamata in callback è TwitterData</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> TwitterData<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Un esempio di quello che si può fare è <a href="http://icant.co.uk/sandbox/twitter-hi-demo.html">questo</a>.</p>
<div class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">Technorati Tag: <a rel="tag" href="http://technorati.com/tags/twitter">twitter</a>,<a rel="tag" href="http://technorati.com/tags/API">API</a>,<a rel="tag" href="http://technorati.com/tags/javascript">javascript</a>,<a rel="tag" href="http://technorati.com/tags/hack">hack</a></div>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Twitter+Hack+http%3A%2F%2Fslacko.org%2F%3Fp%3D396" 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=Twitter+Hack+http%3A%2F%2Fslacko.org%2F%3Fp%3D396" title="Post to Twitter">Tweet This Post</a></p></div><hr />
<p><small>© slacko for <a href="http://www.slacko.org">slacko.org</a>, 2009. |
<a href="http://www.slacko.org/2009/01/12/twitter-hack/">Permalink</a> |
<a href="http://www.slacko.org/2009/01/12/twitter-hack/#comments">Nessun commento</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2009/01/12/twitter-hack/&title=Twitter Hack">del.icio.us</a>
<br/>
Post tags: <a href="http://www.slacko.org/tag/api/" rel="tag">API</a>, <a href="http://www.slacko.org/tag/hack/" rel="tag">hack</a>, <a href="http://www.slacko.org/tag/js/" rel="tag">Javascript</a>, <a href="http://www.slacko.org/tag/twitter/" rel="tag">twitter</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2009/01/12/twitter-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>Ajax e POST, una chiarificazione</title>
		<link>http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/</link>
		<comments>http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 21:13:31 +0000</pubDate>
		<dc:creator>slacko</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/</guid>
		<description><![CDATA[Solitamente le applicazioni sviluppate in Ajax utilizzano il metodo GET come visto ad esempio qui. In questi giorni sto sviluppando un piccolo client per Ping.fm, le cui API accettano solo richieste di tipo POST. Vediamo quindi brevemente come comportarci in &#8230; <a href="http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/">Continua a leggere<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Solitamente le applicazioni sviluppate in <strong>Ajax</strong> utilizzano il metodo <a href="http://it.wikipedia.org/wiki/HTTP#Riga_di_richiesta" target="_blank">GET</a> come visto ad esempio <a href="http://www.slacko.org/2006/12/08/ajax-chat/" target="_blank">qui</a>. In questi giorni sto sviluppando un piccolo client per <a href="http://www.ping.fm" target="_blank">Ping.fm</a>, le cui API accettano solo richieste di tipo <a href="http://it.wikipedia.org/wiki/HTTP#Riga_di_richiesta" target="_blank">POST</a>.</p>
<p>Vediamo quindi brevemente come comportarci in questo caso.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;get_data.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> params <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;lorem=ipsum&amp;amp;name=binny&quot;</span><span style="color: #339933;">;</span>
http.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> url<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></pre></div></div>

<p>con POST creiamo l&#8217;oggetto XMLHttpRequest e  l’URL senza parametri.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">http.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
http.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-length&quot;</span><span style="color: #339933;">,</span> params.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
http.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Connection&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Settiamo poi gli headers http insieme alla richiesta in modo che i dati vengano siano interpretati come inviati da un form, e magari diamo anche la lunghezza dei parametri che invieremo.</p>
<p>Scriviamo un handler per l’evento <em>ready state </em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">http.<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>http.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> http.<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: #006600; font-style: italic;">// fai qualcosa qui quando è generato l'evento</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Infine inviamo i parametri insieme alla richiesta. L’url, infatti, verrà caricata solo in seguito all’esecuzione di questa riga. Con il metodo GET invece i parametri sono inviati, dalla funzione <em>send, </em>con valore null poichè contenuti direttamente nell’url.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">http.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>params<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Spero di essere stato chiaro e diretto, se c’è qualcosa che non vi quadra chiedete pure nei commenti.</p>
<div class="wlWriterSmartContent" style="0px">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/javascript">javascript</a>,<a rel="tag" href="http://technorati.com/tags/ajax">ajax</a>,<a rel="tag" href="http://technorati.com/tags/post">post</a>,<a rel="tag" href="http://technorati.com/tags/get">get</a></div>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Ajax+e+POST%2C+una+chiarificazione+http%3A%2F%2Fslacko.org%2F%3Fp%3D291" 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=Ajax+e+POST%2C+una+chiarificazione+http%3A%2F%2Fslacko.org%2F%3Fp%3D291" 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/20/ajax-e-post-una-chiarificazione/">Permalink</a> |
<a href="http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/#comments">1 commento</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/&title=Ajax e POST, una chiarificazione">del.icio.us</a>
<br/>
Post tags: <br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://www.slacko.org/2008/07/20/ajax-e-post-una-chiarificazione/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>

