title: Dynamically add PHP/MySQL code with JavaScript (or AJAX?)
body: <p>I have to ask this question because I'm stuck, and I think its because my understanding of PHP and JavaScript is not fully formed. 
  I think the way I've started to try doing this may be very very wrong but no one else's question hits on something that makes sense to me.</p>
  
  <p>Basicaly:
  I'm trying to have a JavaScript button on a form that will add additional form input boxes and at the same time add additional SQL code in PHP for inserting the data from the new forms to my database.</p>
  
  <p>My JavaScript looks like this..</p>
  
  <pre><code>    function addElement2() {
  
  var ni = document.getElementById('SQLphoneDiv'); 
      var numi = document.getElementById('theValue2'); 
      var num = (document.getElementById('theValue2').value -1 + 2); 
      numi.value = num; 
      var newdiv = document.createElement('div'); 
      var divIdName = 'SQLphoneDiv'+num+''; 
      newdiv.setAttribute('id',divIdName); 
      newdiv.innerHTML = ' &lt;input type=hidden id=' +num+ ' value=' +num+ '&gt; &lt;?php      $insert_phone = "INSERT INTO phone (Phone_Cust_ID,Phone_Numb,Ext) VALUES    (\'$cust_ID[0]\',\'".$_POST[\'phone' +num+ '\']."\',\'".$_POST[\'ext' +num+ '\']."\')";    $add_phone = mysql_query($insert_phone); ?&gt;'; 
      ni.appendChild(newdiv); 
      }
  </code></pre>
  
  <p>The "newdiv.innerHTML" is where all the code is, and when my JavaScript runs it sends it to a DIV on my PHP page.</p>
  
  <pre><code>     &lt;?php
      if (isset($_POST['submit'])) { 
  
           //add aditional phone numbers
  
           echo    '&lt;input type="hidden"  value="0" id="theValue2" /&gt;';
           echo    '&lt;div id="SQLphoneDiv"&gt;&lt;/div&gt;';
       ?&gt;
  </code></pre>
  
  <p>I've found that putting the echo parts outside of the "if" section makes the code actually show up when I hit my add button (at least the DIV's show up, of course it shows no PHP in live mode on Dreamweaver).</p>
  
  <p>So anyway, if I understand what I'm just figuring out this is totally wrong, because the PHP is set once the page is run, so trying to add more code after the fact makes no sense.
  Am I thinking right on this? How would you actually do this? Something with AJAX?</p>
  
  <p>Any tips are greatly appreciated.</p>
  
  <p>Nathan</p>