...

View Full Version : Calling Javascript function from PHP class



JustinC474
07-28-2009, 06:28 PM
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


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()


function drawTable(numRows, headers, data)
{
alert("Hello World");
}


nothngs happening, any ideas?

Amphiluke
07-28-2009, 08:07 PM
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:

<?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)

JustinC474
07-28-2009, 08:23 PM
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()


<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)


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


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!!!

Amphiluke
07-28-2009, 08:38 PM
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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum