<?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>TangoRangers.com's Blog &#187; DOM</title>
	<atom:link href="http://blog.tangorangers.com/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tangorangers.com</link>
	<description>Misc crap and such</description>
	<lastBuildDate>Fri, 30 Jul 2010 01:13:05 +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>Dynamically add form fields using javascript and DOM with dynamic post method</title>
		<link>http://blog.tangorangers.com/2009/05/dynamically-add-form-fields-using-javascript-and-dom-with-dynamic-post-method/</link>
		<comments>http://blog.tangorangers.com/2009/05/dynamically-add-form-fields-using-javascript-and-dom-with-dynamic-post-method/#comments</comments>
		<pubDate>Wed, 06 May 2009 01:14:39 +0000</pubDate>
		<dc:creator>DaijoubuKun</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.tangorangers.com/?p=120</guid>
		<description><![CDATA[Lets say you need to add some extra text fields but you don&#8217;t want to write a bunch of code. I not only have how to quickly and easily add the input fields, but also how to take the data and put in into a MySQL DB! I start with creating the DB. Lets call [...]]]></description>
			<content:encoded><![CDATA[<p>Lets say you need to add some extra text fields but you don&#8217;t want to write a bunch of code.  I not only have how to quickly and easily add the input fields, but also how to take the data and put in into a MySQL DB!</p>
<p>I start with creating the DB.  Lets call the table &#8216;phone&#8217;.  We need 3 rows.  &#8216;idx&#8217; int(10) with Auto Increment and primary key. &#8216;name&#8217; varchar(30), and &#8216;numb&#8217; varchar(20).  You can setup the db how ever you choose.  I create the idx so that I have a key field.</p>
<p>Now we need some javascript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascipt" style="font-family:monospace;">var counter = 0;
Start a counter. Yes, at 0
function add_phone() {
    counter++;
// I find it easier to start the incrementing of the counter here.
    var newFields = document.getElementById('add_phone').cloneNode(true);
    newFields.id = '';
    newFields.style.display = 'block';
    var newField = newFields.childNodes;
    for (var i=0;i&lt;newField.length;i++) {
        var theName = newField[i].name
        if (theName)
                newField[i].name = theName + counter;
// This will change the 'name' field by adding an auto incrementing number at the end. This is important.
        }
        var insertHere = document.getElementById('add_phone');
// Inside the getElementById brackets is the name of the div class you will use.
        insertHere.parentNode.insertBefore(newFields,insertHere);
}</pre></div></div>

<p>Now I like to put that in a seperate .js file, but you can add it to your HTML HEAD tag.</p>
<p>Next we need to build the form.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;add_a_phone&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;fieldset&gt;
&lt;div id=&quot;phone&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;phone_0&quot; value=&quot;&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;phone_num_0&quot; value=&quot;&quot; /&gt;&lt;br&gt;
&lt;/div&gt;
&lt;div id=&quot;add_phone&quot; style=&quot;display: none;&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;phone_&quot; value=&quot;&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;phone_num_&quot; value=&quot;&quot; /&gt;&lt;br&gt;
&lt;/div&gt;
&lt;input type=&quot;button&quot; id=&quot;add_phone()&quot; onclick=&quot;add_phone()&quot; value=&quot;Give me more fields!&quot; /&gt;&lt;br&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot; /&gt;
&lt;/fieldset&gt;
&lt;/form&gt;</pre></div></div>

<p>Notice how in the input button field that the &#8216;id&#8217; and &#8216;onclick&#8217; are the same. That&#8217;s important, and yes you do need the brackets for some reason.<br />
Also note that the form name can NOT be the same as any div id!!! I keep making that mistake.<br />
You will need some way of checking how many fields were added when you get to your &#8216;if(isset($_POST[&#8216;submit&#8217;))&#8217; statement. This gets a little messy.<br />
Because I like my forms to go back to this page and check if submit was clicked we need to put some php code at the very top.</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: #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;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You clicked submit!&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Here is your data&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phone_num_0'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$continue</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</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;">'phone_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phone_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; = &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phone_num_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$phone</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phone_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$phone_num</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phone_num_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO phone (idx, name, numb) VALUES ('NULL', '<span style="color: #006699; font-weight: bold;">$phone</span>', '<span style="color: #006699; font-weight: bold;">$phone_num</span>')&quot;</span><span style="color: #339933;">;</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: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$i</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: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Obviously I left out some information.  Like connecting to your DB.  I&#8217;m assuming you already know how to do that.</p>
<p>I first wrote this on my main page.  This post is almost a copy/paste from the main page. I like adding it to the blog since it gets better indexing from search engines and hopefully the information will be useful to you.  On the main page there is also a working example so you can see how it all ties in.  Check it out <a href="http://www.tangorangers.com/examples/dynadddynpost/index.php">Here</a></p>
<p>Questions?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tangorangers.com/2009/05/dynamically-add-form-fields-using-javascript-and-dom-with-dynamic-post-method/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
