Javascript Course

PHP code is executed on the server side and output data are transmitted to Web browser.
JavaScript script code is executed by the browser on user's computer.
Combining these two web programming languages, JavaScript scripts can achieve dynamic results based on data received and processed by the server. Thus, the same web page can contain a JavaScript code for a user and other JS code for another user.

- There are two ways to combine PHP with JavaScript to achieve a dynamic and personalized result:

      1) By writing the entire JS script within PHP code and adding it to the web page with PHP function 'echo' (or 'print').
echo '<script> var str ="JS code"; </script>';
- The string returned by 'echo' must be a JS code with correct syntax.

      2) By adding in the JS script within the HTML document only the necessary data processed by PHP.
var var_js = '<?php echo $var_php; ?>';
• Both versions must be written into .php files that can be processed by the PHP module.

Alert window with the user name

For example, in a web page there is an authentication system that displays an alert window with the user name, after he logged in.
In this case we assume that the username is stored in a session variable ( $_SESSION['username'] ).
- Using the first method, we use the following PHP code:
// ... php code ...
echo "<script>alert('Welcome ". $_SESSION['username'] ."');</script>";

- Or the second way:
<?php session_start(); ?>
<!-- HTML code -->
alert('Welcome <?php echo $_SESSION["username"]; ?>');

Clock with server time

A clock made with JavaScript and added in a web page it displays the visitor's computer time. If you want to display the same time for all visitors, you can add and use the server time in the JS script, like in the following example:
<div id='tag_ora'></div>
// Clock script server-time,

// use php to get the server time
var dt_serv = new Date(<?php echo date('y,n,j,G,i,s'); ?>);

var ore = dt_serv.getHours(); //hour
var minute = dt_serv.getMinutes(); //minutes
var secunde = dt_serv.getSeconds(); //seconds

// function that process and display data
function jsClock(){
 secunde = 0;
 minute = 0;
 if(ore>23) ore = 0;

 var output ='<h4>Server time - '+ore+':'+minute+':'+secunde+'</h4>';
 document.getElementById('tag_ora').innerHTML = output;

//calls the function every second
setInterval('jsClock()', 1000);
- Test it yourself, in a .php file, on the server.

Displaying data from PHP with JavaScript, according to a URL

Maybe you have seen in sites for traffic or banner that is necessary to add a little JavaScript code in the page with a specific 'src' attribute.
They use the same principle, to combine PHP with JavaScript.
The URL in the JS code added in the web page calls a PHP script. The PHP script uses $_GET to get the parameters from the received URL. According to those parameters, the PHP processes data on the server and return a HTML and JavaScript code that can display in your page what they want, the traffic site, a banner, ....
To understand better, try the following example:

      1. Create a php file on your server (named 'phpjs_test.php') and add the following code:
$ids =[1=>'php-mysql', 2=>'javascript', 3=>'html'];

// gets the id from URL
 if($str = $ids[$_GET['id']]){

 //return a string with a JS instruction that will display a link
 echo 'document.write("<a href=\''. $str. '\'>Course '. $str. '</a>");';

      2. In the same folder on the server create a html file (ex. 'test_jsphp.html') where you add the fallowing code:
<script src='phpjs_test.php?id=2'></script>

- Call this html file on the server. The result will be a link (written by document.write()) defined in php script, based on the 'id' added in the URL in 'src' attribute.

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];
JavaScript code and PHP

Last accessed pages

  1. How to get JSON data from JavaScript to PHP (175)
  2. JavaScript code and PHP (29598)
  3. Node.js Move and Copy Directory (8423)
  4. querySelector and querySelectorAll (16171)
  5. Send E-mail with HTML tags and Attachment (2907)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (455)
  2. Register and show online users and visitors (324)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (247)
  4. JavaScript Course - Free lessons (237)
  5. querySelector and querySelectorAll (225)