Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
Also, the code here are just snippets of the full code. There would be a lot of unrelated stuff that doesn't pertain to this main idea. –  fender357 Apr 11 '12 at 19:53
 
Php is processed server side, javascript is processed client side, there is no chance you'll be able to add non existing php code from javascript –  legrandviking Apr 11 '12 at 19:54
 
Right, that's what I'm finally figuring out. So how would you implement this type of functionality? –  fender357 Apr 11 '12 at 19:55
 
You can make sure on the php side can deal with all the fields even the ones that were not filled by a user. using javascript and ajax to generate additionnal text fields is a good idea. To make a long story short, you planned your php for all possible fields and you deal with only the ones the user had entered. –  legrandviking Apr 11 '12 at 20:12
 
So like make a bunch of SQL input's for a particular field already in the PHP code, so once a field is added it will use that code? And then obviously you will be limited to who ever many iterations of the code you put in (which would totally be fine)? –  fender357 Apr 11 '12 at 20:23
show 1 more comment

1 Answer

up vote 0 down vote accepted

PHP runs on the server before the page is sent to the client. JavaScript runs on the client. AJAX would be the way to go.

share|improve this answer
 
Ok, any documentation you could point me towards that might give me a jump start for this kind of thing? –  fender357 Apr 11 '12 at 20:00
 
This is where I started... w3schools.com/ajax/ajax_intro.asp –  blurd Apr 17 '12 at 11:14
 
Thanks, I read as much as I can on W3schools. –  fender357 Apr 22 '12 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.