0

I have a question regarding the access of html elements.

File1.PHP

<table id="tableID">
<tr>
    <td>&nbsp
    </td>
</tr>
</table>
<input type=button onclick=changeValue();>
<script type="text/javascript" src="file2.JS"></script>

File2.JS

function changeValue(){ 
//HAS AJAX OBJECT THAT CALLS file3.php to read a file
ajax.doPost("file3.php", callback); }

File3.php

  function fileRead(){
   ...
   $line = fgets($file_handle);
   ///After reading the lines of the file in File3.php 
   ///I want to output it in the table in File1.PHP without returning 
   ///it as xmlResponse to File2.JS
   ///Is it possible to acces the table in File1.PHP here?
 }

Is this flow possible? Because I can't make it work.

Help..

4
  • 4
    It'll be much easier if you just pass all the necessary values from file1.php with AJAX to file3.php Commented Feb 16, 2012 at 8:00
  • @KemalFadillah you mean i would get the table object from file2.js and pass it also to file3.php? Commented Feb 16, 2012 at 8:07
  • Actually, I was thinking of traversing the DOM with JavaScript and grab all the values you need from the table and then send them through AJAX. I'm pretty sure what you're looking for is the data and not the table itself. Commented Feb 16, 2012 at 8:12
  • @KemalFadillah its the read values to the table.. i used ajax and thought of returning the read values through xmlResponse but file is quite big.. so i wanted to directly access the table to populate. Commented Feb 16, 2012 at 8:26

2 Answers 2

1

The easiest way to get a similiar behavior is to put the table into its own file

table.php

<?php
  ob_start();
?>
<table id="tableID">
  <tr>
    <td>&nbsp</td>
  </tr>
</table>
<?php
  // Save content of this page in a variable
  $table = ob_get_contents();
  ob_end_clean();
?>

File1.PHP

<?php
  include "table.php";
  // Access $table defined in table.php
  echo $table;
?>
<input type=button onclick=changeValue();>
<script type="text/javascript" src="file2.JS"></script>

File3.php

  function fileRead(){
   // Access $table defined in table.php
   echo $table;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks @Gottox how can i acces the cells of $table? i need to put the read values in its cells..
i think this one il try but Im not too sure how to access the cells of the table here..
cannot access the cells in the table because its string :(
0

yes it is possible.

function changeValue(){ 
$.ajax({
                type: "POST",
                data:"filenametoread=filename",
                url:"path to file3.php
                success: function(msg){
                   $("#tableID").html(msg);  
                }
            });
}

file3.php

if(isset($_REQUEST['filenametoread']){
      return fileRead($_REQUEST['filenametoread']);
}
function fileRead($filename){
   ...
   $line = fgets($file_handle);
   return $line;
 }

I don't think so you can access file1.php content here!(if not passed as an ajax data)

1 Comment

but because my file is too big.. and i read it line per line, if i return after reading a line, i cannot read everything..

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.