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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Calling Javascript function from PHP class

    I have a PHP class Display_Table which I am using as the container to save all the information I need in:
    $numRows = number of rows
    $columns = array of column headers
    $data = 2D array of data

    I am trying to call my java_script.js function drawTable( , , )

    heres the code I have
    Code:
    function printJSArray()
          {
    	echo '<script	type = "txt/javascript"	src = "java_script.js"></script>';
          	echo '<script type = "txt/javascript">';
    	echo 'var dataJS = <?php echo	json_encode($this->data)?>';
    	echo 'var columnsJS =	<?php echo json_encode($this->columns)?>';
    	echo 'var numRowsJS =	<?php echo json_encode($this->numRows)?>';
            echo 'drawTable(numRowsJS, columnsJS, dataJS)';
            echo '</script>';
          }
    i currently have in drawTable()
    Code:
    function drawTable(numRows, headers, data)
    {
        alert("Hello World");
    }
    nothngs happening, any ideas?

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    First of all correct the script MIME type as follows: "text/javascript"

    I do not understand why to put <script> tags into the printJSArray() function. Try to modify you code in the following way:
    Code:
    <?php
    	echo '<script type = "text/javascript" src="java_script.js"></script>';
    	echo '<script type="text/javascript">';
    	echo 'var dataJS = "', json_encode($this->data), '";';
    	echo 'var columnsJS = "', json_encode($this->columns), '";';
    	echo 'var numRowsJS = "' , json_encode($this->numRows), '";';
    	echo 'drawTable(numRowsJS, columnsJS, dataJS);';
    	echo '</script>';
    ?>
    And the function printJSArray() is needless in that case.
    (Clear away quotes I highlighted with dark red if the variables are not of string type).


    P.S. Sorry, I think I understand now the thing... Possibly, the reason is missing of semicolons (I have just highlighted them with green)
    Last edited by Amphiluke; 07-28-2009 at 07:20 PM. Reason: postscript added
    I am still learning English

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    39
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks, this is my first time using two (or more!) different languages in 1 project so i get kind of confused with tags and such.

    So im a little confused about you saying I don't need the function.

    my main page, index.php, creates an object of class Display_Table()
    Code:
    <html>
    <head>
    <title> Justin's Workplace </title>
    <link rel = "stylesheet" type = "text/css" href = "style.css"/>
    <script type = "txt/javascript" src = "java_script.js"></script>
    </head>
    <body>
    
    <?php
    include("Display_Table.php");
    
    $columnHeader = array('Green'=>23, 'Red'=>3, 'Blue'=>12, 'Yellow'=>4);
    $table = new Display_Table($columnHeader);
    
    $table->orderHeaderColumn();
    
    $newRow = array('Green'=> 50, 'Yellow' => 'hey!', 'Blue'=>4);
    $table->addRow($newRow);
    $newRow2 = array('Yellow'=> 'yeee', 'Green' => 1);
    $table->addRow($newRow2);
    $newRow3 = array('Red'=> 4, 'Blue'=>3, 'Yellow' => 5);
    $table->addRow($newRow3);
    
    
    $table->printJSArray();
    
    ?>
    
    </body>
    </html>
    So all the data is stored in $table

    but, my boss wants the Table to be printed entirely in JS (so it can be manipulated client-side)

    I have never worked in PHP, HTML, or JS, so the best way I thought of to do this was to make a function printJSArray() in Display_Table.php so I could call

    $table.printJSArray()

    and it would print a pretty little Table all in Javascript.

    so currently here is my plan of attack.... call printJSArray() which then calls a function in "java_script.js" that then prints the Table out to the screen.

    so currently here is printJSArray(thanks to the help)
    Code:
     function printJSArray()
          {
            echo '<script type = "text/javascript" src = "java_script.js"></script>';
            echo '<script type = "text/javascript">';
            echo 'var dataJS = "', json_encode($this->data), '";';
            echo 'var columnsJS = "', json_encode($this->columns), '";';
            echo 'var numRowsJS = "', json_encode($this->numRows), '";';
            echo 'drawTable(numRowsJS, columnsJS, dataJS);';
            echo '</script>';
          }
    and here is the drawTable() fnc that is called
    Code:
    function drawTable()
    {
        alert("Hello World!");
    }
    since Hello World is not popping up when I refresh the page, I think something is going wrong.

    any criticism, hints, tips, or otherwise useful information on anything, pertaining to either this code or the project in general would be greatly appreciated.

    My boss doesn't know these languages in any sort of veteran depth so im just stumbling my way through this project just 1 fnc at a time

    thanks!!!

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Are you sure that json_encode() returns a valid string from the viewpoint of Javascript? It would be a good thing to see the resulting HTML code generated by that PHP function...
    I am still learning English


  •  

    Posting Permissions

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