From PHP to Javascript

From all we know, to display Javascript using PHP is very easy.

$jsInPHP = "<script>";
$jsInPHP += "alert('Hello this is in Javascript');";
$jsInPHP += "</script>";

Most of the Plugins for UI we know uses Javascript and JQuery. For example, Google’s Chart Tools. To generate the chart, we must use data. but where do we get the data? It is of course the database. To call data from database, of course we will need to use PHP. However, Javascript does not support PHP. Means that we cannot assign PHP to Javascript just like that. This is because Javascript is the client-side while PHP is the server-side. The language is different. To shorten, Javascript cannot understand PHP.

The solution to this is to use JSON. JSON is a data-interchange format. Means that JSON will translate PHP to a format where Javascript will understand it.

For example:

<?php $phpData = mysqli_fetch_row(mysqli_query($connection,"select COUNT(*) from event where YEAR(event_date)=$last3year")); ?>

var PHPinJS = <?php print json_encode($phpData); ?>;

Thats it! Good luck!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s