0

I'm really having a hard time with accessing my html table from PHP.

This is what I have:

file.php

<?php

  ...some php code
  $someValue = '2012';

?>

<script language='javascript'>
    var table = document.getElementById('tableId');

     for (var i = '<php echo $myVar?>',row; row = table.rows[ '<php echo $myVar?>']; i++) {
        //iterate through rows
        //rows would be accessed using the 'row' variable assigned in the for loop
        for (var j = 0, col; col = row.cells[j]; j++) {
            //iterate through columns
            //columns would be accessed using the 'col' variable assigned in the for loop
            col.innerHTML = '<?php  $someValue; ?>'  
        }  
    }
</script> 

<?php

  ... continue php codes.       
?>

Do I make sense here? I wanted to place the $someValue to my table cell.

Help

The flow is something like this... I have

file-1.php

  • contains my table
  • contains file-1.js

file-1.js

  • has an ajax object that executes file-2.php

then I wanted file-2.php to access the table in file-1.php and fill out the cells with the values generated in file-2.php

0

3 Answers 3

1

You need to echo that variable.

<?php echo $someValue; ?>
Sign up to request clarification or add additional context in comments.

4 Comments

thanks @Kai Qing, i echo'ed it already but still not showing.. am i accessing the cell correctly?
If you view source do you get col.innerHTML = '2012'? It could be that php is echoing as expected but your js is never called to actually write the dom element, or is called incorrectly.
yes, the col.innerHTML has the correct value when i view source.. :( but its not reflecting on my page..
You should try putting all your js into a window onload listener then: window.onload = function(){ //your code }; it echoes as expected but is likely being called before the dom is actually written. Therefore, tableId is not present and the for loop may have nothing to iterate through.
1

You simply forgot to echo it out.

col.innerHTML = '<?php echo $someValue; ?>';

1 Comment

thanks.. yeah i forgot to echo.. i tried and still its not showing in my table ;( am i accessing the cell correctly?
0

As you are using ajax, send the new values from file2.php in a json form (http://www.json.org/) for eg try this ( http://www.itnewb.com/tutorial/Introduction-to-JSON-and-PHP)

Then use javascript to get the json values each and then u can update ur html table .

Better use some javascript libraries like Jquery ( http://www.jquery.com/ ) , so that u can do this easily

10 Comments

thanks @Akhil Thayyil but it is still not showing.. would it matter if my table is inside <div>?
Am not sure about ur logic , i have corrected it anyways var i = '<?php echo $myVar?>';//Changes this var i = <?php echo $myVar?>; Check this js in fiddle jsfiddle.net/A44RV
thanks @Akhil hmmm.. because they are different files.. is that possible?
what i mean is, the file which contains my html table is different from my php file that tries to access the table.. is that possible?
what ur saying ? ur doing it in javascript and the output after php parsing contain the javacript+html(including ur tables )+ur processed php code , so all these are finnaly comes under a single page . so how can both be different , pls state ur problem with all possible details then only we can help u at its best
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.