Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    Ajax Help Needed

    I'm experienced in Ajax programming in PHP but I'm having trouble understanding why this ajax code isn't working. The thing that is different about this ajax code is that I am creating a table on a form that did not exist when the form was first loaded.

    I have a php form. When I click on a link I want a table to be displayed with a width of 850, done using Ajax.

    I can create the table fine using ajax but I can't populate the table width attribute with my php variable holding the value of 850. The line snippet shown here creates the table ok:

    PHP Code:
    <TABLE ID="table_width" border=0 width="<?php echo $php_width?>">
    The JS code below would normally populate the table width with no problems:

    PHP Code:
    var table_width = '<?php echo $php_width?>';
    document.getElementById('table_width').width = table_width;
    HOWEVER, what I have determined is that since the table structure never existed before (when the form first loaded), the getElementById('table_width').width statement doesn't work.

    If the table structure existed on the form when first loaded then of course it would work and set the table width.

    So, this ajax problem is different from all of my other ajax coding because here I am trying to "ajax" a new table onto an existing form that was never there before AND then set its table width attribute. The table structure comes into the form fine but I can't access it's .width attribute using my php variables.

    Does anyone know the fix for this?

    Thanks...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    Planet Earth
    Posts
    59
    Thanks
    1
    Thanked 8 Times in 8 Posts
    The only problem i found with your code is in the JS part.

    You have:

    PHP Code:
    var table_width = '<?php echo $php_width?>'; 
    document.getElementById('table_width').width = table_width;
    Your are missing a ";" at end of $php_width, should be:

    PHP Code:
    var table_width = '<?php echo $php_width?>'; 
    document.getElementById('table_width').width = table_width;
    Maybe thats the problem. Hope it helps.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    zactanaz's PHP error is correct, but I assume that you have tried manually replacing the PHP variable with 850 to see if the problem is in the JS, or the PHP, correct?

    If that is not your problem, then the issue may be the order in which you create the element, and how you are setting its attributes. If the table exists in the HTML already on load, then a document.getElementById method will work fine. If you are creating it through the DOM, then that method will only work after the element has been appended to the document. If you need to set properties before you append it to the document, then you need to give your element a variable name, and do something like mytable.width = tabe_width or mytable.setAttribute('width','850')

    Like i said, if zactanaz's reply works for you great, otherwise we need to see a little bit more of the JS you are using to create the table.

    Note: This should go in the JS section.
    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

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