Home Free Templets Works Mind Chiller Photo Gallery Family & Friends Links
 
 

Use Javascript alertbox in php

 In this short tutorial I will show you how to display the values of php variables in an javascript alertbox/message box .

Sometime it becomes very necessery to display the intermediate values of php variables while the script is running. This is an extremely useful php debugging technique .
As php is a server side language it does not have any facility to display javascript alert box . So , we can pass the value of a php variable to javascript "alertbox" function to do the trick.
Here is a very short code to acheive this


When u run the page you will see an alert box popping up displaying 10 which is the value of the php variable $s.
In the line
alert('<?= $s ?>')
we are passing a php variable "$s " to javascript "alert " function. In this way we can pass php variables to javascript functions.
Next I show more complex example of this



Here , the output will be 10 popups displaying 1-9. We are executing a php loop that increases the value of a php variable $i from 1 -9 ,. and inside the loop there is a javascript messagebox which takes the value of the php variable $i and display it ..
This method is an extremely useful debugging technique as you can see the intermediate values of php variables while running a long script.

Back to Tutorial Index page

Your comments are most welcome admin[AT]koderguru.com