www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] PHP and Javascript problem

  1. #1
    Join Date
    Mar 2010
    Posts
    48

    resolved [RESOLVED] PHP and Javascript problem

    Hi guys,

    I have a really weird problem. I'm creating some fields dynamically with Javascript using document.createElement.

    Each control has it's id and name set.

    After this script is run, with PHP I try to populate the newly created fields. I'm creating the fields FROM php, I just do an echo of the javascript function that creates the elements for me. And like I said all this is done before populating the fields.

    Problem is that when I try to reference these newly created items, javascript complains that they don't exist.

    Viewing the source code of the web page actually doesn't show the dynamically created items.

    Any one have any idea of why this is happening or of a solution to this?

    Thanks!

  2. #2
    Join Date
    Oct 2005
    Location
    Cambridgeshire, UK
    Posts
    191
    Do you have any code you could provide? It might help us understand what exactly is going on and what you are trying to achieve.

    Cheers

  3. #3
    Join Date
    Mar 2010
    Posts
    48
    Sure, it looks something like this (condensed version of what I do):

    This function creates my html element. For purposes of this example, assume newRowValue =1 so the element will be called Quantity1
    Code:
    <head>
    
    function addRow(tableId) {
      var table = document.getElementById(tableId);
      newRowValue= document.getElementById('activeProducts').value;
      newRowValue=parseInt(newRowValue)+1;
                   document.getElementById('activeProducts').value=newRowValue.toString();
      var row = table.insertRow(newRowValue);
      var i=1;
      var cell1 = row.insertCell(0);
      var optn;
      var element1 = document.createElement("select");
      element1.name= "Quantity"+newRowValue;
      element1.id= "Quantity"+newRowValue;
      for (i=1;i<100; i++){ 
         optn = document.createElement("option");
         optn.text = i;
         optn.value = i;
         element1.options.add(optn);
       }					
       cell1.appendChild(element1);
    }
    </head>
    With php I add a new row with my javascript function (assume the table products exists and has no rows):

    Code:
    <html>
       <table id='products'>
       </table>  
       <?php
       echo"
           <script>
             addRow('products');
            </script>
       ";
       ?>
    ...
    after I add the row I try to populate the newly created element:

    Code:
    ....
    
    <?php
       echo"
           <script>
              document.getElementbyId('Quantity1').value= 'some value';
            </script>
       ";
       ?>
    </html>
    The above line throws the error: document.getElementById("Quantity1") is null, which obviously means that the element doesn't exist.

    The element does indeed exist though, it shows perfectly on my web page and it even is submitted if I try to do so.
    Source code of the page doesn't show the newly created row and element though.

    I transcribed this from the top of my head so there might be syntax errors (not logical errors though) so if you notice any, that's not the problem, my code runs fine.
    Tried to be as clear as possible, hope you guys can help me out.

    Thanks!
    Last edited by mavalos88; 04-12-2010 at 09:00 PM.

  4. #4
    Join Date
    Mar 2010
    Posts
    48
    Found the error, nothing to do with this, an error elsewhere.
    Thanks though!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles