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.
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.
My JavaScript looks like this..
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 = ' <input type=hidden id=' +num+ ' value=' +num+ '> <?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); ?>';
ni.appendChild(newdiv);
}
The "newdiv.innerHTML" is where all the code is, and when my JavaScript runs it sends it to a DIV on my PHP page.
<?php
if (isset($_POST['submit'])) {
//add aditional phone numbers
echo '<input type="hidden" value="0" id="theValue2" />';
echo '<div id="SQLphoneDiv"></div>';
?>
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).
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?
Any tips are greatly appreciated.
Nathan