Practical Web Programming

Friday, February 27, 2009

How to Pass Value from Javascript to PHP

Although JavaScript and PHP is not the same technology, JavaScript being a client-side technology and PHP a server-side, you can actually pass values from PHP to JavaScript and vice-versa. To do this, all you need is to use a cookie.

In this example source code, I will create a cookie in JavaScript and retrieve the cookie value using PHP. Read on.

JavaScript code to create cookie.

<script type='text/javascript'>
function createCookie(name, value,expiredays)
{
var expiry_date = new Date();
expiry_date.setDate(expiry_date.getDate() + expiredays);
document.cookie=name+ "=" +escape(value)+
((expiredays==null) ? ""
:";expires="+expiry_date.toGMTString());
alert("'" + name + "' cookie created.");
}
</script>


HTML code to call the createCookie function.

<input type="button" value="Create Cookie using JavaScript" 
onclick="createCookie('YourName', 'Joel Badinas')"/>


HTML form for submitting request to the server for the PHP code.

<form method="POST" action=""> 
<p>
<input type="submit" name="submit" value="Get Cookie using PHP"/>
</p>
</form>


PHP code that will read the cookie.

<?php
if (isset($_POST["submit"]))
{
print "Cookie value is '".$_COOKIE["YourName"]."'";
}
?>


Putting it all together.

<html>
<head>
<title>Practical Web Programming</title>

<script type='text/javascript'>
function createCookie(name, value,expiredays)
{
var expiry_date = new Date();
expiry_date.setDate(expiry_date.getDate() + expiredays);
document.cookie=name+ "=" +escape(value)+
((expiredays==null) ? ""
:";expires="+expiry_date.toGMTString());
alert("'" + name + "' cookie created.");
}
</script>

</style>

</head>
<body>

<p method="POST" action="">
<p>
<input type="button" value="Create Cookie using JavaScript"
onclick="createCookie('YourName', 'Joel Badinas')"/>
<input type="submit" name="submit" value="Get Cookie using PHP"/>
</p>
</form>

<?php
if (isset($_POST["submit"]))
{
print "Cookie value is '".$_COOKIE["YourName"]."'";
}
?>

</body>
</html>


There you have it.

Thursday, February 19, 2009

PHP: How to Get the Time Elaped

Using PHP's time function, you can get the time elapsed of any process in your web application. The time function returns the current Unix timestamp.

Here's a simple script to demonstrate it.


<?php

$t1 = time();

print "Start time : ".$t1."<br>";

for ($i = 0; $i < 90000000; $i++)
{
print "";
}

$t2 = time();
print "End time : ".$t2."<br>";
print "Time elapsed (s) : ".($t2 - $t1)."<br>";

?>


The script above will output the following.


Start time : 1235100956
End time : 1235100980
Time elapsed (s) : 24

Sunday, February 15, 2009

Solved: Can't Change Blogger Blog Shortcut Icon

Ok, you followed my instructions on how to change your blog/website shortcut icon but your Blogger blog is still showing the default Blogger shortcut logo. Don't despair, you are not alone. I myself was in that situation before when I tried to change this blog's shortcut icon.

In your Blogger template head tag section, you will see a b:include tag named all-head-content (highlighted in red) . When your is blog is visited (or loaded in HTML), the Blogger engine will replace that tag with all the meta tags for you blog including the shortcut icon, and you have no control over this.

<head>
<b:include data='blog' name='all-head-content'/>
<title><data:blog.pageTitle/></title>
<b:skin><![CDATA[/*


Fortunately, I found a simple trick to outsmart Blogger. When you insert your HTML shortcut icon code, put it after the b:include tag named all-head-content. Here's an example from this blog.

<head>
<b:include data='blog' name='all-head-content'/>
<title><data:blog.pageTitle/></title>
<link href='http://kabalweg.googlepages.com/blog.com.ico' rel='shortcut icon'/>
<b:skin><![CDATA[/*


Doing it this way will override the shortcut icon from the Blogger enginge.

Recent Post