<?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>JQuery, PHP, html, design... // arnaud-k : un blog de geek &#187; ajax</title>
	<atom:link href="http://blog.arnaud-k.fr/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.arnaud-k.fr</link>
	<description>Le blog JQuery, Html, Web Design</description>
	<lastBuildDate>Thu, 21 Apr 2011 08:10:17 +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>Tutorial JQuery : formulaire de recherche en Ajax</title>
		<link>http://blog.arnaud-k.fr/2010/09/14/tutorial-jquery-formulaire-de-recherche-en-ajax/</link>
		<comments>http://blog.arnaud-k.fr/2010/09/14/tutorial-jquery-formulaire-de-recherche-en-ajax/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 11:00:24 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[tutorial JQuery]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=1797</guid>
		<description><![CDATA[Chose promis, chose due, voici un tutorial pour faire un formulaire de recherche en Ajax avec JQuery et PHP. Idéal si vous avez envie de copier Google Instant, ce type de formulaire permet de faire gagner du temps à vos&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2010/09/14/tutorial-jquery-formulaire-de-recherche-en-ajax/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F09%2F14%2Ftutorial-jquery-formulaire-de-recherche-en-ajax%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F09%2F14%2Ftutorial-jquery-formulaire-de-recherche-en-ajax%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="http://blog.arnaud-k.fr/wp-content/uploads/2010/09/ajax-search.jpg" alt="" title="Tutorial JQuery : formulaire de recherche en Ajax" width="600" height="230" class="aligncenter size-full wp-image-1810" /><br />
Chose promis, chose due, voici un tutorial pour faire un <strong>formulaire de recherche en Ajax avec JQuery et PHP</strong>. Idéal si vous avez envie de copier <a href="http://blog.arnaud-k.fr/2010/hors-sujet/autre/google-instant-search-la-recherche-plus-rapide/">Google Instant</a>, ce type de formulaire permet de faire gagner du temps à vos internautes. Et comme on commence à en voir un peu partout (Google, Youtube, Itunes&#8230;), il risque de devenir un standard.</p>
<p>Donc, comme d&#8217;habitude, j&#8217;ai préparé une démo du tutorial :<br />
<a class="demo" title="voir la demo du formulaire de recherche en Ajax" href="http://blog.arnaud-k.fr/demos/jquery-ajax-search/">Demo</a></p>
<p>Et c&#8217;est parti pour le code. On attaque donc par le code HTML de la page. On prépare le formulaire simplement, j&#8217;ai opté pour que l&#8217;on n&#8217;affiche pas le bouton soumission du formulaire. Et on ajoute un bloc vide, celui-ci contiendra les résultats éventuels.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--debut du formulaire--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;get&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;q&quot;</span>&gt;</span>Rechercher un article<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;q&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;q&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;">p</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: #808080; font-style: italic;">&lt;!--fin du formulaire--&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--preparation de l'affichage des resultats--&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;results&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Ensuite le gros du code, le code JavaScript utilisant JQuery :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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: #006600; font-style: italic;">// détection de la saisie dans le champ de recherche</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#q'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</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>
    $field <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#results'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on vide les resultats</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ajax-loader'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on retire le loader</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// on commence à traiter à partir du 2ème caractère saisie</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $field.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// on envoie la valeur recherché en GET au fichier de traitement</span>
      $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  	type <span style="color: #339933;">:</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// envoi des données en GET ou POST</span>
	url <span style="color: #339933;">:</span> <span style="color: #3366CC;">'ajax-search.php'</span> <span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// url du fichier de traitement</span>
	data <span style="color: #339933;">:</span> <span style="color: #3366CC;">'q='</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// données à envoyer en  GET ou POST</span>
	beforeSend <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: #006600; font-style: italic;">// traitements JS à faire AVANT l'envoi</span>
		$field.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;ajax-loader.gif&quot; alt=&quot;loader&quot; id=&quot;ajax-loader&quot; /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ajout d'un loader pour signifier l'action</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	success <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: #006600; font-style: italic;">// traitements JS à faire APRES le retour d'ajax-search.php</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ajax-loader'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on enleve le loader</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#results'</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: #006600; font-style: italic;">// affichage des résultats dans le bloc</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Et enfin le code du fichier &laquo;&nbsp;ajax-search.php&nbsp;&raquo; effectuant la recherche et le renvoi des éléments :</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//connexion à la base de données </span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nom_de_la_base'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nom_utilisateur'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mot_de_passe'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_HOST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$link</span>   <span style="color: #339933;">=</span>   <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span> DB_HOST <span style="color: #339933;">,</span> DB_USER <span style="color: #339933;">,</span> DB_PASSWORD <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span> DB_NAME <span style="color: #339933;">,</span> <span style="color: #000088;">$link</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//recherche des résultats dans la base de données</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span>   <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'SELECT post_title , post_date , guid
                          FROM wp_posts
                          WHERE post_status = \'publish\'
                          AND post_title LIKE \''</span> <span style="color: #339933;">.</span> safe<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'%\'
                          LIMIT 0,20'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// affichage d'un message &quot;pas de résultats&quot;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;h3 style=&quot;text-align:center; margin:10px 0;&quot;&gt;Pas de r&amp;eacute;sultats pour cette recherche&lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</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;">// parcours et affichage des résultats</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;div class=&quot;article-result&quot;&gt;
            &lt;h3&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">guid</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;&lt; ?php echo utf8_encode( $post-&gt;post_title ); ?&gt;&lt;/a&gt;&lt;/h3&gt;
            &lt;p class=&quot;date&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_date</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
            &lt;p class=&quot;url&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">guid</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
        &lt;/div&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
fonctions
*****/</span>
<span style="color: #000000; font-weight: bold;">function</span> safe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addcslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'%_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Voilà, c&#8217;est tout pour ce tutoriel. Après il peut encore être optimisé en ajoutant des suggestions (toujours à la Google <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). Mais on verra ça une autre fois !</p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2010/09/14/tutorial-jquery-formulaire-de-recherche-en-ajax/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>Google Instant Search : la recherche plus rapide !</title>
		<link>http://blog.arnaud-k.fr/2010/09/08/google-instant-search-la-recherche-plus-rapide/</link>
		<comments>http://blog.arnaud-k.fr/2010/09/08/google-instant-search-la-recherche-plus-rapide/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 20:12:56 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[news du web]]></category>
		<category><![CDATA[actualité]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=1754</guid>
		<description><![CDATA[Après deux jours de Google Doodle mystérieux, Google a fait la &#171;&#160;Big Annonce&#160;&#187; de la journée : la mise en ligne du Google Instant Search. Tout d&#8217;abord pour tester cela, rien de plus simple que d&#8217;aller sur le site de&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2010/09/08/google-instant-search-la-recherche-plus-rapide/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F09%2F08%2Fgoogle-instant-search-la-recherche-plus-rapide%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F09%2F08%2Fgoogle-instant-search-la-recherche-plus-rapide%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: left;"><img class="aligncenter size-large wp-image-1760" title="Google-Instant-Search" src="http://blog.arnaud-k.fr/wp-content/uploads/2010/09/Google-Instant-Search1-600x222.png" alt="" width="600" height="222" />Après deux jours de Google Doodle mystérieux, Google a fait la &laquo;&nbsp;Big Annonce&nbsp;&raquo; de la journée : la <strong>mise en ligne du Google Instant Search</strong>. Tout d&#8217;abord pour tester cela, rien de plus simple que d&#8217;aller sur le site de Google (pas la peine de te donner le <a href="http://google.com">lien</a> je présume&#8230;) et de commencer une recherche.</p>
<p style="text-align: left;">C&#8217;est magique, on arrive sur la page des résultats tout seul, sans même valider le formulaire. Les résultats s&#8217;actualisent à la frappe du clavier et est couplé avec le toujours présent Google Suggest. Une chose que l&#8217;on peut dire, c&#8217;est que c&#8217;est très très très rapide. Google <strong>Instant</strong> Search porte bien son nom, en effet c&#8217;est bel et bien instantané, et en plus, je ne pense pas être le seul à m&#8217;amuser avec ce soir (ou ce midi / après-midi aux USA).</p>
<p style="text-align: left;">A voir ce que cela va donner à l&#8217;utilisation, pour une première recherche, les internautes ont pris <strong>l&#8217;habitude d&#8217;utiliser la boîte de moteur de recherche du navigateu</strong>r (Firefox, Internet Explorer, Opéra, Chrome&#8230; le proposent tous).</p>
<p style="text-align: left;">Et je peux déjà parier sur une pléiade de tutoriels à venir tels que &laquo;&nbsp;Faire une recherche à la Google Instant Search&nbsp;&raquo; avec Jquery et PHP (ou Mootools et ASP par exemple) ! Et pourquoi pas un petit <a href="http://blog.arnaud-k.fr/tag/tutorial-jquery/">tutoriel avec JQuery</a> ici (j&#8217;ai déjà commencé à y réfléchir <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ).</p>
<p style="text-align: left;">Cette petite transgression dans le thème du blog me permet de parler d&#8217;une nouvelle fonctionnalité que je trouve particulièrement réussi et de voir l&#8217;éventuel impact d&#8217;un billet à chaud sur une actualité brûlante, avec peut-être un retour sur cette expérience bientôt !</p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2010/09/08/google-instant-search-la-recherche-plus-rapide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Astuce JQuery : la fonction serialize ()</title>
		<link>http://blog.arnaud-k.fr/2010/03/08/astuce-jquery-la-fonction-serialize/</link>
		<comments>http://blog.arnaud-k.fr/2010/03/08/astuce-jquery-la-fonction-serialize/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 07:51:00 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[astuce JQuery]]></category>
		<category><![CDATA[serialize]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=1627</guid>
		<description><![CDATA[Après le précédent billet présentant la fonction clone de JQuery, j&#8217;ai décidé de te parler d&#8217;une autre fonction méconnue de JQuery : la fonction serialize. Alors que fait-elle cette fonction ? Pour appeler serialize, il faut avoir en sélecteur un&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2010/03/08/astuce-jquery-la-fonction-serialize/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F03%2F08%2Fastuce-jquery-la-fonction-serialize%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2F03%2F08%2Fastuce-jquery-la-fonction-serialize%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Après le précédent billet présentant <a href="http://blog.arnaud-k.fr/2010/developpement-web/cat-jquery/astuce-jquery-du-jour-la-fonction-clone/" title="la fonction clone de JQuery">la fonction clone de JQuery</a>, j&#8217;ai décidé de te parler d&#8217;<strong>une autre fonction méconnue de JQuery : la fonction serialize.</strong></p>
<p>Alors que fait-elle cette fonction ?</p>
<p>Pour appeler serialize, il faut avoir en sélecteur un formulaire. <strong>Serialize va tout simplement retourner une chaine de caractères contenant toutes les valeurs des champs de ce formulaire.</strong> La chaine est formatée pour être envoyée en Ajax par la suite ( de type <em>champ1=valeur1&amp;champ2=valeur2&amp;champ3=valeur3</em>&#8230; ). La où la fonction serialize va être utile, c&#8217;est qu&#8217;elle permet de créer une fonction qui va permettre d&#8217;envoyer n&#8217;importe quel formulaire en Ajax avec une seule fonction.</p>
<p>Comme d&#8217;habitude, la demo est par là :<br />
<a href="http://blog.arnaud-k.fr/demos/jquery-serialize/" title="demo de la fonction serialize" class="demo">demo de la fonction serialize</a></p>
<p>Admettons que l&#8217;<strong>on ajoute la classe &laquo;&nbsp;ajax&nbsp;&raquo; à tous les formulaires</strong> que l&#8217;on souhaite envoyer en Ajax :</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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span>
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nom&quot;</span>&gt;</span>Nom :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nom&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nom&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;">p</span>&gt;</span>
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span>&gt;</span>Email :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&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;">p</span>&gt;</span>
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;typeform&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inscription&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;">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;inscription&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;">p</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>Ici, j&#8217;ai créé un formulaire d&#8217;inscription simple demandant seulement un nom et un email. Un champ caché (nommé typeform) va permettre de différencier le formulaire des autres pour le traitement en PHP (en assumant que tous les traitements en Ajax se passe dans le même fichier).</p>
<p>Le JQuery va être ici très simple (en connaissant les bases de JQuery <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form.ajax'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on empeche l'envoi du formulaire par le navigateur</span>
     <span style="color: #003366; font-weight: bold;">var</span> datas <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
          type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>      <span style="color: #006600; font-style: italic;">// envoi des données en POST</span>
          url<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'action'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// envoi au fichier défini dans l'attribut action</span>
          data<span style="color: #339933;">:</span> datas<span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// sélection des champs à envoyer</span>
          success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     <span style="color: #006600; font-style: italic;">// callback en cas de succès</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'success : '</span><span style="color: #339933;">+</span>datas<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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>C&#8217;était simple non ? <strong>La fonction serialize de JQuery est toute simple, mais super utile !</strong> Elle permet vraiment de factoriser le code.</p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2010/03/08/astuce-jquery-la-fonction-serialize/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Recherche à la Twitter avec JQuery et PHP (tutorial)</title>
		<link>http://blog.arnaud-k.fr/2009/08/13/recherche-a-la-twitter-avec-jquery-et-php-tutorial/</link>
		<comments>http://blog.arnaud-k.fr/2009/08/13/recherche-a-la-twitter-avec-jquery-et-php-tutorial/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 09:41:41 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=1157</guid>
		<description><![CDATA[Tutorial bien sympa trouvé sur le blog 9lessons : &#171;&#160;Twitter like Search with jQuery and Ajax&#160;&#187; (&#171;&#160;recherche à la twitter avec JQuery et Ajax&#160;&#187;). Ce tuto permet donc de créer un formulaire de recherche tout en Ajax. Les résultats sont&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2009/08/13/recherche-a-la-twitter-avec-jquery-et-php-tutorial/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F08%2F13%2Frecherche-a-la-twitter-avec-jquery-et-php-tutorial%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F08%2F13%2Frecherche-a-la-twitter-avec-jquery-et-php-tutorial%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="aligncenter size-full wp-image-1158" title="jquery-twitter" src="http://blog.arnaud-k.fr/wp-content/uploads/2009/08/jquery-twitter.png" alt="jquery-twitter" width="600" height="200" /></p>
<p><strong>Tutorial bien sympa trouvé sur le blog 9lessons</strong> : &laquo;&nbsp;<a href="http://9lessons.blogspot.com/2009/08/twitter-like-search-with-jquery-ajax.html">Twitter like Search with jQuery and  Ajax</a>&nbsp;&raquo; (&laquo;&nbsp;recherche à la twitter avec JQuery et Ajax&nbsp;&raquo;). Ce tuto permet donc de créer un formulaire de recherche tout en Ajax. Les résultats sont donc affichés directement sur la même page que le formulaire.</p>
<p>Une démo est disponible directement sur le blog 9lessons.<br />
<a href="http://9lessons.net63.net/newsearch.php" class="demo">Voir la démo</a></p>
<p>A noter que l&#8217;on également <strong>télécharger les scripts expliqués dans le tutorial</strong>. On y retrouve le JQuery, le PHP, l&#8217;HTML et même un peu de CSS. Le tuto est bien complet, l&#8217;effet de recherche en Ajax est toujours classe. J&#8217;espère juste pour toi que tu comprends l&#8217;anglais&#8230; <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2009/08/13/recherche-a-la-twitter-avec-jquery-et-php-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial JQuery : effet de suppression Ajax à la WordPress</title>
		<link>http://blog.arnaud-k.fr/2009/06/03/tutorial-jquery-effet-de-suppression-ajax-a-la-wordpress/</link>
		<comments>http://blog.arnaud-k.fr/2009/06/03/tutorial-jquery-effet-de-suppression-ajax-a-la-wordpress/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 11:05:46 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin JQuery]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=608</guid>
		<description><![CDATA[Dans l&#8217;admin de WordPress (qui utilise JQuery), il y des légers effets de couleurs, d&#8217;apparition. Je trouve qu&#8217;ils ont un rendu qui fait assez classe. Je te propose donc un tutorial pour faire plus ou moins le même effet. Pour&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2009/06/03/tutorial-jquery-effet-de-suppression-ajax-a-la-wordpress/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F06%2F03%2Ftutorial-jquery-effet-de-suppression-ajax-a-la-wordpress%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F06%2F03%2Ftutorial-jquery-effet-de-suppression-ajax-a-la-wordpress%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Dans l&#8217;admin de WordPress (qui utilise JQuery), il y des légers effets de couleurs, d&#8217;apparition. Je trouve qu&#8217;ils ont un rendu qui fait assez classe.</p>
<p>Je te propose donc un tutorial pour faire plus ou moins le même effet. Pour l&#8217;effet de transition de couleur il est nécessaire d&#8217;avoir le plugin JQuery.color (<a href="http://blog.arnaud-k.fr/effetSuppr/jquery.color.js">dispo ici</a>) et le plugin JQuery.delay pour une meilleure fluidité (au moment de l&#8217;appel en Ajax, <a href="http://blog.arnaud-k.fr/effetSuppr/jquery.delay.js">dispo ici</a>)</p>
<p><a class="demo" title="voir la démo" href="http://blog.arnaud-k.fr/effetSuppr/">Voir la démo</a></p>
<p>Comme d&#8217;habitude, on commence par inporter la librairie JQuery et les plugins nécessaires :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.color.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.delay.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>Ensuite on prépare la structure HTML des éléments à supprimer. J&#8217;ai choisi des paragraphes (balises &lt;p&gt;) pour que ce soit le plus simple possible, mais libre à vous de le changer et par conséquent de changer les sélecteurs JQuery.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Premier élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- on place dans l'attribut rel l'id de l'element à supprimer --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Deuxième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Troisième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Quatrième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;4&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Cinquième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;5&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Sixième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;6&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Septième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;7&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Huitième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;8&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Neuvième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;9&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</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>&gt;</span>Dixième élément <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;supprimer&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;10&quot;</span>&gt;</span>Supprimer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></td></tr></table></div>

<p>Enfin, la plus grosse partie : le code JQuery. Les commentaires sont directement dans le code (ils sont également dans le code de la page de démo).</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #3366CC;">'a.supprimer'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// quand on clique sur un lien supprimer</span>
		e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// annule l'effet du lien pour pas recharger la page</span>
		<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on recupere l'id de l'element a supprimer (qui est dans l'attribut rel du lien)</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Supprimer cet élément ?'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// on demande une confirmation</span>
			<span style="color: #003366; font-weight: bold;">var</span> parent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on recupere le paragraphe parent du lien (qui contient l'element a supprimer)</span>
			$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
				type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// les variables seront passées en POST (comme d'hab pour les recuperer en php $_POST[id] par exemple)</span>
				url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;suppr.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// on appelle le fichier php suppr.php qui supprimer l'element de la base de donnees (dans mon cas, ce fichier est vide)</span>
				data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;id=&quot;</span><span style="color: #339933;">+</span>id<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&amp;act=suppr&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// variable que l'on passe au fichier php</span>
				beforeSend<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>
					parent.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'backgroundColor'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'#FF3F47'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// animation a faire avant l'envoi (on change le fond avec un fondu jusqu'à la couleur #FF3F47)</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
				success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// en cas de succes</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1200</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: #006600; font-style: italic;">// on attend 1.2 secondes</span>
							parent.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</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: #006600; font-style: italic;">// et on fait disparaitre l'element en l'enroulant</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;">&#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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2009/06/03/tutorial-jquery-effet-de-suppression-ajax-a-la-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial : suggestions (auto-complétion) en Ajax avec JQuery et Php</title>
		<link>http://blog.arnaud-k.fr/2009/04/28/tutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php/</link>
		<comments>http://blog.arnaud-k.fr/2009/04/28/tutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 11:47:22 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[auto-complétion]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[suggestion]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=390</guid>
		<description><![CDATA[Pour ce deuxième tutoriel sur l&#8217;ajax, je te propose un formulaire avec auto-complétion. Simplement afficher des suggestions quand l&#8217;utilisateur commence à taper dans un champ de texte. Les suggestions sont affichées grâce à Ajax qui permet d&#8217;effectuer des requêtes SQL&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2009/04/28/tutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F04%2F28%2Ftutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F04%2F28%2Ftutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Pour ce deuxième tutoriel sur l&#8217;ajax, je te propose <strong>un formulaire avec auto-complétion</strong>. Simplement afficher des suggestions quand l&#8217;utilisateur commence à taper dans un champ de texte. Les suggestions sont affichées grâce à Ajax qui permet d&#8217;effectuer des requêtes SQL en &laquo;&nbsp;arrière-plan&nbsp;&raquo; de la page. Toute la partie JavaScript sera faite grâce au framework JQuery.</p>
<p><img class="aligncenter size-full wp-image-391" title="auto-completion avec Ajax et Jquery" src="http://blog.arnaud-k.fr/wp-content/uploads/2009/04/auto-completion.png" alt="auto-completion avec Ajax et Jquery" width="600" height="200" /></p>
<p>Comme d&#8217;hab, j&#8217;ai préparé <strong>une page de démo du script : <a href="http://blog.arnaud-k.fr/autoComplete/">disponible ici</a></strong>. Et il est également possible de <a href="http://blog.arnaud-k.fr/autoComplete/autoComplete.zip">télécharger les fichiers sources de cette démo</a>. Et bien sûr, tout le code présenté ici est dépouillé de toute mise en forme (libre à toi de le personnaliser après !).</p>
<p><a class="demo" title="démo JQuery : auto-complétion" href="http://blog.arnaud-k.fr/autoComplete/">Jquery : auto-complétion</a></p>
<p>Donc, pour commencer, on va préparer le code HTML nécessaire : le champ de texte sur lequel les suggestions vont être appliqué et le bloc (caché au début de la page) qui va éventuellement contenir les suggestions :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</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>
		Rechercher un pays :
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</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;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&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;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inputString&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--  champ texte à analyser pour les suggestions --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;suggestionsBox&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;suggestions&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: #808080; font-style: italic;">&lt;!-- bloc contenant les eventuelles suggestions --&gt;</span>
		<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;upArrow.png&quot;</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;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upArrow&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> <span style="color: #808080; font-style: italic;">&lt;!-- image de la fleche vers le haut --&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;suggestionList&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;autoSuggestionsList&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- liste contenant les suggestions --&gt;</span>
			<span style="color: #ddbb00;">&amp;nbsp;</span>
		<span style="color: #009900;">&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;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Ensuite, dans <strong>le code javascript</strong>, on retrouve <strong>les deux fonctions nécessaires</strong> : lookup qui permet de faire l&#8217;appel ajax et fill qui permet, comme son nom l&#8217;indique, de remplir le champ avec la valeur suggerée. Après avoir défini les deux fonctions, on ajoute les &laquo;&nbsp;écouteurs&nbsp;&raquo; (qui permettent de surveiller si un événement est effectué) avec keyup (quand on appuie sur le clavier, à la remontée de la touche) et blur (quand le champ texte perd le focus).</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><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;">// si le champs txte est vide</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: #006600; font-style: italic;">// on cache les suggestions</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: #006600; font-style: italic;">// sinon</span>
			$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ajax.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: #006600; font-style: italic;">// on envoit la valeur du champ texte dans la variable post queryString au fichier ajax.php</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: #006600; font-style: italic;">// si il y a un retour, on affiche la liste</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: #006600; font-style: italic;">// et on remplit la liste des données</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>
&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;">// remplir le champ texte si une suggestion est cliquée</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>
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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: #006600; font-style: italic;">// lorsque la page est entierement chargée</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input#inputString&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</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: #006600; font-style: italic;">// si on presse une touche du clavier en étant dans le champ texte qui a pour id inputString</span>
		lookup<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input#inputString&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</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: #006600; font-style: italic;">// si le champs texte perd le focus</span>
		fill<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Enfin, il ne reste plus qu&#8217;à développer le php qui sera exécuté grâce à l&#8217;appel ajax. Dans ce script, on  récupère la valeur passée (ce qui est saisi dans le champ texte) et il n&#8217;y a plus qu&#8217;à <strong>faire une requête SQL dans la base de données</strong> et d&#8217;afficher les résultats sous forme de liste (avec en prime un petit onClick=fill() sur chaque élément de liste permettant de remplir le champ texte.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">﻿<span style="color: #339933;">&lt;</span> ?php	
&nbsp;
	<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hote de la base&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Impossible de se connecter : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;db278690514&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// si une variable queryString a été posté</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>
&nbsp;
			<span style="color: #666666; font-style: italic;">// si la longueur du contenu de la variable est superieur à 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>
&nbsp;
				<span style="color: #666666; font-style: italic;">// requete sql à personnaliser pour correspondre à votre base de données</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #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;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">// on parcourt les resultats</span>
					<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pays</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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;">// on affiche les resultats dans un element de liste en ajoutant </span>
                                                <span style="color: #666666; font-style: italic;">// la fonction fill sur l'evenenement onClick</span>
		         			<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;">$pays</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;">$pays</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;">'Il y a une probleme avec la requete sql.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> 
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Il ne devrait pas avoir un accès direct à ce script !!!'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Tutoriel basé sur celui du blog nodstrum : <a href="http://nodstrum.com/2007/09/19/autocompleter/">http://nodstrum.com/2007/09/19/autocompleter/</a></p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2009/04/28/tutorial-suggestions-auto-completion-en-ajax-avec-jquery-et-php/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Tutorial : connexion en AJAX avec JQuery et PHP</title>
		<link>http://blog.arnaud-k.fr/2009/04/22/tutorial-connexion-en-ajax-avec-jquery-et-php/</link>
		<comments>http://blog.arnaud-k.fr/2009/04/22/tutorial-connexion-en-ajax-avec-jquery-et-php/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 12:12:54 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[formulaire de connexion]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=310</guid>
		<description><![CDATA[Pour ce premier tutorial AJAX, je propose un simple formulaire de connexion sans rafraichissement de la page. Ca permettra également d&#8217;introduire un peu de php sur le blog. Il est possible d&#8217;essayer une démo (login : test, mot de passe&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2009/04/22/tutorial-connexion-en-ajax-avec-jquery-et-php/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F04%2F22%2Ftutorial-connexion-en-ajax-avec-jquery-et-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F04%2F22%2Ftutorial-connexion-en-ajax-avec-jquery-et-php%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>
Pour ce premier tutorial AJAX, je propose un simple f<strong>ormulaire de connexion sans rafraichissement de la page</strong>. Ca permettra également d&#8217;introduire un peu de php sur le blog.
</p>
<p><img src="http://blog.arnaud-k.fr/wp-content/uploads/2009/04/connexion-ajax.png" alt="connexion en ajax avec JQuery et PHP" title="connexion en ajax avec JQuery et PHP" width="600" height="200" class="alignnone size-full wp-image-311" /></p>
<p>
<strong>Il est possible d&#8217;<a href="http://blog.arnaud-k.fr/connexionAjax/">essayer une démo</a> (login : test, mot de passe : ajax) ou de <a href="http://blog.arnaud-k.fr/connexionAjax/connexionAjax.zip">télécharger la source</a> de cette démo</strong> (fichier php et mise en forme css inclus). Tout le code affiché ici sera du code dépouillé de tout mise en forme.
</p>
<p>
Pour commencer, on va <strong>préparer le formulaire HTML</strong> servant à la connexion. Deux champs sont nécessaires : nom d&#8217;utilsateur et mot de passe. Il ne faut pas oublier de donner un id (ici &laquo;&nbsp;connexionForm&nbsp;&raquo;) au formulaire pour la gestion des évènements avec JQuery. J&#8217;ai également ajouté un <strong>span vide qui contiendra éventuellement un message d&#8217;erreur</strong> si le login ou le mot de passe n&#8217;est pas bon.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;connexionForm&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;connexionForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- début du formulaire de connexion --&gt;</span>   
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;erreur&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- span qui contiendra les éventuels messages d'erreur --&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span>&gt;</span>Nom d'utilisateur :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- champ pour le login --&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pass&quot;</span>&gt;</span>Mot de passe :<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pass&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pass&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- champ pour le mot de passe --&gt;</span>
&nbsp;
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</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;Je me connecte&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bouton&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- bouton de connexion --&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: #808080; font-style: italic;">&lt;!-- fin du formulaire --&gt;</span></pre></td></tr></table></div>

<p>
On passe ensuite à la partie JQuery. On commence par charger le framework dans le fichier html (entre les balises &lt;head&gt; et &lt;/head&gt;).
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>
A nous de jouer maintenant. <strong>L&#8217;appel en Ajax ne se fera qu&#8217;à la soumission du formulaire</strong> (avec l&#8217;événement &laquo;&nbsp;submit()&nbsp;&raquo;, ligne 2).
</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;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#connexionForm&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</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: #006600; font-style: italic;">// à la soumission du formulaire						 </span>
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// fonction permettant de faire de l'ajax</span>
		   type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// methode de transmission des données au fichier php</span>
		   url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;login.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// url du fichier php</span>
		   data<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;login=&quot;</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#login&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&amp;pass=&quot;</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#pass&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// données à transmettre</span>
		   success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// si l'appel a bien fonctionné</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si la connexion en php a fonctionnée</span>
				<span style="color: #009900;">&#123;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div#connexion&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;span id=<span style="color: #000099; font-weight: bold;">\&quot;</span>confirmMsg<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Vous &amp;ecirc;tes maintenant connect&amp;eacute;.&lt;/span&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #006600; font-style: italic;">// on désactive l'affichage du formulaire et on affiche un message de bienvenue à la place</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #006600; font-style: italic;">// si la connexion en php n'a pas fonctionnée</span>
				<span style="color: #009900;">&#123;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span#erreur&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>bomb.png<span style="color: #000099; font-weight: bold;">\&quot;</span> style=<span style="color: #000099; font-weight: bold;">\&quot;</span>float:left;<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&amp;nbsp;Erreur lors de la connexion, veuillez v&amp;eacute;rifier votre login et votre mot de passe.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #006600; font-style: italic;">// on affiche un message d'erreur dans le span prévu à cet effet</span>
				<span style="color: #009900;">&#125;</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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// permet de rester sur la même page à la soumission du formulaire</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;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Enfin, il ne reste plus qu&#8217;à <strong>créer le fichier php (login.php)</strong> permettant de tester les paramètres de connexion en testant dans la base de données des membres, si le login et le mot de passe correspondent (ce code est ultra-simplifié, il n&#8217;est pas forcément très sécurisé <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ). <strong>Si la connexion est validée, on créé un cookie</strong> (que l&#8217;on pourra tester par la suite avec la fonction &laquo;&nbsp;isset($_COOKIE[id])&nbsp;&raquo; ).<br />
Contenu du fichier login.php :
</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// connexion avec la base de données</span>
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Impossible de se connecter : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nomDeMaBase&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// on exécute maintenant la requete sql pour tester si les parametres de connexion sont ok</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT login, pass, id FROM membres WHERE login = '<span style="color: #006699; font-weight: bold;">$_POST[login]</span>' AND pass = '<span style="color: #006699; font-weight: bold;">$_POST[pass]</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$membre</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>login<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #000088;">$membre</span><span style="color: #009900;">&#91;</span>login<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>pass<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #000088;">$membre</span><span style="color: #009900;">&#91;</span>pass<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: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$membre</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// genere un cookie contenant l'id du membre</span>
	<span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$membre</span><span style="color: #009900;">&#91;</span>login<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// genere un cookie contenant le login du membre		</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// on 'retourne' la valeur 1 au javascript si la connexion est bonne</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;">&quot;0&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// on 'retourne' la valeur 0 au javascript si la connexion n'est pas bonne</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
C&#8217;est tout pour le premier tutorial AJAX/JQuery/PHP. Cela permet <strong>un premier contact avec AJAX</strong> qui peut être facilement mis en place grâce à JQuery (on économise beaucoup de lignes de codes par rapport au &laquo;&nbsp;JavaScript normal&nbsp;&raquo;&#8230;).</p>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2009/04/22/tutorial-connexion-en-ajax-avec-jquery-et-php/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

