<?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; serialize</title>
	<atom:link href="http://blog.arnaud-k.fr/tag/serialize/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.arnaud-k.fr</link>
	<description>Le blog JQuery, PHP, html, design… // un blog de geek</description>
	<lastBuildDate>Wed, 08 Sep 2010 20:12:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Astuce JQuery : la fonction serialize ()</title>
		<link>http://blog.arnaud-k.fr/2010/developpement-web/cat-jquery/astuce-jquery-la-fonction-serialize/</link>
		<comments>http://blog.arnaud-k.fr/2010/developpement-web/cat-jquery/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 formulaire. Serialize va tout simplement retourner une chaine de caractères contenant toutes les valeurs des [...]]]></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%2Fdeveloppement-web%2Fcat-jquery%2Fastuce-jquery-la-fonction-serialize%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2010%2Fdeveloppement-web%2Fcat-jquery%2Fastuce-jquery-la-fonction-serialize%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly" 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>Sur le même sujet :<ol><li><a href='http://blog.arnaud-k.fr/2010/developpement-web/cat-jquery/astuce-jquery-du-jour-la-fonction-clone/' rel='bookmark' title='Permanent Link: Astuce JQuery du jour : la fonction clone()'>Astuce JQuery du jour : la fonction clone()</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2010/developpement-web/cat-jquery/astuce-jquery-la-fonction-serialize/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
