View Full Version : Ajax Help Needed

01-19-2008, 05:54 PM
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:

<TABLE ID="table_width" border=0 width="<?php echo $php_width?>">

The JS code below would normally populate the table width with no problems:

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?


01-22-2008, 08:18 PM
The only problem i found with your code is in the JS part.

You have:

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:

var table_width = '<?php echo $php_width; ?>';
document.getElementById('table_width').width = table_width;

Maybe thats the problem. Hope it helps.

01-22-2008, 08:54 PM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum