<?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>Not A n00b &#187; dialog</title>
	<atom:link href="http://notan00b.com/tag/dialog/feed/" rel="self" type="application/rss+xml" />
	<link>http://notan00b.com</link>
	<description>Tutorials, Scripts, and Rants</description>
	<lastBuildDate>Wed, 12 May 2010 08:08:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery, PHP, and Forms</title>
		<link>http://notan00b.com/2009/09/jquery-php-and-forms/</link>
		<comments>http://notan00b.com/2009/09/jquery-php-and-forms/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 01:56:57 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[box]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[hidden]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=204</guid>
		<description><![CDATA[I decided to put up a quick post on how I have been using jQuery and PHP to process forms for me very easily.  To get started, let me give you a background on what I have been trying to do.  I have a page with a list of info, and I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to put up a quick post on how I have been using jQuery and PHP to process forms for me very easily.  To get started, let me give you a background on what I have been trying to do.  I have a page with a list of info, and I wanted to give the user the option of editing that info using jQuery&#8217;s built in dialog box because it is awesome looking.  There were a few ways to go about doing this, which is what I am going to describe.</p>
<h1>This is the goal!</h1>
<li>I click the link</li>
<li>A dialog box pops up</li>
<li>The text boxes have info filled in already</li>
<li>Something like this:<br />Name:<br />
<input type="text" value="Bob Jones"/></li>
<li>I can edit it, then submit it</li>
<li>I want the code to be small</li>
<p>The first way that I thought of was to create a form for every piece of info I pulled from the database.  So lets say for ID=1, there would be 1 form with all the info preloaded into it.  ID=2 would be a separate form with all different info.  This works, but I had to have about 20 dialog boxes per page, and it would cause the HTML output to be about 20 times larger than I needed.</p>
<p>In my head, I knew there had to be a way to do this with DHTML and jQuery, and I finally figured it out.  All I had to do was create a value and store it in the button that I was already using (well it was actually just a link).  So I turned <code>&lt;a href="#" class="editThis"&gt;Edit!&lt;/a&gt;</code> into <code>&lt;a href="#" class="editThis" id="1"&gt;Edit!&lt;/a&gt;</code>.  I used PHP to echo out the data already but now each link will have a different ID:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> class<span style="color: #000099; font-weight: bold;">\&quot;</span>editThis<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[UserId]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Edit!&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we have 20 links, each looking the same, but each with a unique ID.  This is where jQuery comes in handy.  We can look up the anchor by class, and then store the ID as a variable for later use!</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;">&quot;.editThis&quot;</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;">&#123;</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;">'id'</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>So now we have the ID of the info we want to manipulate.  The next part was a little more difficult to figure out.  I want only 1 dialog box and 1 form, but want the info auto filled.  At first I was thinking we needed to do a lot of AJAX to pull out each and every variable that we want to edit, but then I figured out we could pass that info into HTML as a &#8220;HIDDEN&#8221; variable.  HTML is great because you can hide all sorts of stuff and the user will never see it.  I can set the style as &#8220;hidden&#8221;, or I can include it inside a tag somewhere, or better yet, and my favorite, I can create a hidden variable: <code>&lt;input type="hidden" id="editinfo1" value="myinfohere"&gt;</code>  Now we need to implement this into our PHP to write it to the HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> class<span style="color: #000099; font-weight: bold;">\&quot;</span>editThis<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[UserId]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Edit!&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; name=<span style="color: #000099; font-weight: bold;">\&quot;</span>editinfo<span style="color: #006699; font-weight: bold;">{$row['UserId']}</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; id=<span style="color: #000099; font-weight: bold;">\&quot;</span>editinfo<span style="color: #006699; font-weight: bold;">{$row['UserId']}</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$row[Name]</span>::<span style="color: #006699; font-weight: bold;">$row[Address]</span>::<span style="color: #006699; font-weight: bold;">$row[Age]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now our HTML will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;#&quot; class=&quot;editThis&quot; id=&quot;1&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;editinfo1&quot; id=&quot;editinfo1&quot; value=&quot;Bob Jones::123 N Street::26&quot;&gt;</pre></div></div>

<p>All our info is now conveniently stored for jQuery to access later!</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;">&quot;.editThis&quot;</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;">&#123;</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;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> variables<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#editinfo&quot;</span><span style="color: #339933;">+</span>id<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: #003366; font-weight: bold;">var</span> info<span style="color: #339933;">=</span>variables.<span style="color: #660066;">split</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: #000066;">alert</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">+</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">+</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</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>Now for the fun part, using jQuery to dynamically change the values of our SINGLE form!<br />
Our form might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;dialog&quot; title=&quot;Edit User&quot;&gt;
    &lt;form method=&quot;post&quot; name=&quot;edituserform&quot; action=editUser.php&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;edituser&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;userid&quot; id=&quot;euid&quot; value=&quot;0&quot;&gt;
    &lt;center&gt;
        &lt;table width=&quot;75%&quot;&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;User Name: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;eusername&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;Address: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;address&quot; id=&quot;eaddress&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td align=&quot;right&quot;&gt;Age: &lt;/td&gt;
                &lt;td align=&quot;left&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;age&quot; id=&quot;eage&quot;&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/center&gt;
    &lt;/form&gt;
&lt;/div&gt;</pre></div></div>

<p>And our jQuery would look like this:</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;">&quot;.editThis&quot;</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;">&#123;</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;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> variables<span style="color: #339933;">=</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#editinfo&quot;</span><span style="color: #339933;">+</span>id<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: #003366; font-weight: bold;">var</span> info<span style="color: #339933;">=</span>variables.<span style="color: #660066;">split</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eusername&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eaddress&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#eage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;euid&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</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;">&#40;</span><span style="color: #3366CC;">'#dialog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        autoOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
        width<span style="color: #339933;">:</span> <span style="color: #CC0000;">600</span><span style="color: #339933;">,</span>
        modal<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        position<span style="color: #339933;">:</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span>
        buttons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;Update user&quot;</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>
                document.<span style="color: #660066;">edituserform</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
            <span style="color: #3366CC;">&quot;Cancel&quot;</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: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it!  Hopefully it was easy to understand.  Let me know if you need anything cleared up a little!  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/09/jquery-php-and-forms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
