<?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; formulaire de connexion</title>
	<atom:link href="http://blog.arnaud-k.fr/tag/formulaire-de-connexion/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 : 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>

