Skip to main content

Posts

Showing posts from July, 2010

Default value in a field

This code is used to show a default value in the text field of a form. When the user clicks inside the field, the default text disappears and he can enter his own value. If the user does not enter anything, and when the field loses focus, the default text is shown back in the field. This is often used to show any guidelines to the user, like what to enter in the field. Example: Password: <input type="text" name="password" size="15" value="min. 6 chars" onFocus="if (value == 'min. 6 chars') {value=''}" onBlur="if (value== '') {value='min. 6 chars'}">

Get Selected Checkbox Values

This script gives you dynamically the values of the checkboxes selected in a form. Download script here Screenshot

Get page url

This code returns the URL of your browser, including the parameters. Try giving parameters at the end, and you can get all of them. php code: <?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?> output: (click image to enlarge)

Get file and directory names

Note: __FILE__ represents "F:\Program Files\XAMPP\htdocs\dirname.php" in my system. php code: <?php echo 'The current file: '.'<b>'.__FILE__.'</b><br><br>'; echo 'DIR name using dirname(__FILE__): '.'<b>'.dirname(__FILE__).'</b><br><br>'; // returns C:\xampp\htdocs echo 'Current working directory using getcwd(): '.'<b>'.getcwd().'</b><br><br>'; // returns C:\xampp\htdocs echo 'Basename of current file: '.'<b>'.basename(__FILE__).'</b><br><br>'; //returns dirname.php $arrStr = explode("/", $_SERVER['SCRIPT_NAME'] ); $arrStr = array_reverse($arrStr ); echo "This is yet another way to get this script name: ".'<b>'.$arrStr[0].'</b><br><br>'; echo 'Basename of current directory: '.'<b>'.basename(dirna

Format date

Generally we store the dates in mysql database with 'date' datatype, and when we have to show them in the front end, we just cant display it the same way. Because it is difficult for the users to understand quickly when they read the date format as YYYY-MM-DD, which mysql uses. So when displaying a mysql date in front end, you can just use this function, which takes the mysql date as parameter and returns the neatly formatted date, including the week day. php code: <?php function format_date($date) { $date = getdate(strtotime($date)); return $date['mday'].' - '.$date['month'].' - '.$date['year'].', '.$date['weekday']; } $date = date("Y-m-d"); // Today's date (You get it from database in real time) echo "<b>Date of birth: </b>".format_date($date); // format it ?> For ex, a person's date of birth is stored as date type in database. It is shown in the front end after passing thr

Java vs php

Whats the comparison between java and php? Well, it may sound rude, but its not fair to compare two legends of the current Software Industry. Both are good languages and hold strengths of their own... both support OOPS, both have frameworks, and both are highly used in the real world. There are some key things to consider before comparing java and php. 1) All Java programs need the necessary libraries to compile. For example, while working with files, a java program needs to import the io libraries and for date, you need util package classes. But php is a precompiled language (as the name suggests, it is Hypertext Preprocessor). You dont need to import anything for all standard functionalities in your file. 2) People working on Java are called Java programmers, as they would be mostly involved in writing their own code in the module and contributing to the project. They are assigned a module independently and they are much focused on that single module. Whereas people working on php ar