Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, August 15, 2013

Redirect URL with out changing the url in address bar using htaccess

Hi. This is a small trick to redirect the URL to another url with out changing the address bar url. We should write the below code in htaccess file.
RewriteEngine on

RewriteRule (.*)\.com$ $1.php [PT]
RewriteRule (.*)\.in$ $1.php [PT]


This will show an URL to user in the address bar,  but internally it will redirect and execute the code of other URL which we configure here.
After adding this to .htaccess file. check this. 

For example if you want to show url as

http://www.domainname.com/pages/mypage.com

to user and internally we want to call and execute the code of another page like

http://www.domainname.com/pages/mypage.php

then use this sample code in your htaccess file. while executing this, the URL will remain same as 
http://www.domainname.com/pages/mypage.com in address bar, but redirect and executes the content of mypage.php. 
For applying this transfers only to a folder follow this. Then we should write the below code in htaccess file.



RewriteEngine on

RewriteRule ^pages/(.*)\.com /pages/$1.php [PT]
RewriteRule ^pages/(.*)\.in /pages/$1.php [PT]


You can change the pages to your folder. The above code works only when the user requests a page from pages folder. other wise it will ignore this.

mypage.com is redirected to the page of mypage.php and will execute the content of the mypage.php when user type this url in address bar
http://www.domainname.com/pages/mypage.comor 
http://www.domainname.com/pages/mypage.in


Thank you

Monday, July 29, 2013

How to know if the browser is IE in PHP

Hi. This is a small code to check the browser is Internet explorer or not using the PHP.
<?php
if(preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT']))
{
   // The browser is IE 
   // write your code here
}
else
{
   // The browser is other than IE
   // write your code here
} 
?>

If you are executing the above code in Internet explorer browser then if condition will execute. and if you are executing the code in other than Internet explorer then else condition will be executed.

Thank you

Friday, July 12, 2013

How to display the previous days date using PHP

Hi to every one. Today am going to give a small code to show the previous day date using the PHP. Just copy and paste the below code into your editor and change the static number to your number.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d'))));

date('Y/m/d') will gives current date.
strtotime(date('Y/m/d')) will convert the current date to UNIX Timestamp.
Here '-' represents the previous days to show. 
Here 'N' is the number to show 'Nth' days ago date from the current date.
'-N days' will gives the previous 'Nth' day date.
strtotime('-N days',strtotime(date('Y/m/d')) will gives the UNIX Timestamp string of previous 'Nth' day date.
date('Y/m/d', strtotime('-N days',strtotime(date('Y/m/d')))) will display the UNIX Timestamp to date format of YYYY/MM/DD.

For example:
Assume that current date is 2013/07/12. and if we want to display 7 days ago date then write the following code.
date('Y/m/d', strtotime('-7 days',strtotime(date('Y/m/d'))));
It will show the last 7 days ago date in the format of 2013/07/05.


Thursday, June 13, 2013

How to navigate forward and back to the pages using JavaScript

Follow the steps to navigate forward and back to the pages using JavaScript.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.back();
}
</script>
</head>
<body>
<input type='button' name='back' value='Go Back' title='Click here to go back' onClick='goBack()'>
</body>
</html>
When the user click on the button it will go back to last visited page which means previous page.
We can write this code in another way.
<script language='JavaScript' type='text/javascript'>
function goBack() {
     window.history.go(-1);
}
</script>
This script is also works same as the above script. This is the same as the back button functionality of the browser. Here '-1' represents the previous page.

And we can use this script to load the next URL in in the history list.
<html>
<head>
<script language='JavaScript' type='text/javascript'>
function goForward() {
     window.history.forward();
}
</script>
</head>
<body>
<input type='button' name='forward' value='Go Forward' title='Click here to go Forward' onClick='goForward()'>
</body>
</html>
This is the same as the forward button functionality of the browser.

Thank you...

Wednesday, June 12, 2013

How to do HTTP redirect to a URL using JavaScript

Follow the simple steps to do HTTP redirect to a URL using JavaScript.
<html>
<head>
<script type='text/javascript'>
forwardUrl(); // calling the function to redirect
function forwardUrl() {
     window.location.href = 'http://www.phpboyz.com/page1.php';
}
</script>
</head>
<body></body>
</html>
Change the URL to your url to forward. And call the function to forward.

And we can send the parameters along with the url using this script also.
<script type='text/javascript'>
var value1 = 10;
var value2 = 'testing';
     window.location.href = 'http://www.phpboyz.com/page1.php?param1='+value1+'&param2='+value2;
</script>
This script will forward from current page to requested URL location along with the 2 GET parameters param1 and param2. we can use this 2 parameters where ever we need.
Use $param1 = $_GET['param1'] and $param2 = $_GET['param2'] to get the values in the page1.php.

Thank you...

Thursday, December 27, 2012

JavaScript Form Validations


JavaScript is also used to validate the Form data of HTML pages before sending the data to server.

JavaScript can be validate any type of data.
(Ex. NULL values, Text, Numeric, Email, Date etc.)
For example take a HTML Form like below : ( Save this file as textData.php)
<form name="myForm" action="send_data.php" onsubmit="return validateForm()" method="post">
User Name : <input type="text" name="username">
First Name: <input type="text" name="firstname">
Password : <input type="password" name="password">
Mobile : <input type="text" name="mobile">
Gender : <select name="gender">
               <option value="">Select</option>
               <option value="male">Male</option>
               <option value="female">Female</option>
               </select>
Date of Birth (yyyy/mm/dd) :  <input type="text" name="dateofbirth">
Email Id : <input type="text" name="emailid">
<input type="submit" name="submit" value="Submit">
</form>
Note: If you want to validate any data you should write in the below function only as per the above html code. You can change the function names and field names as per your needs.
function validateForm()
{
      //write below code here if required
      return true;
}
To Test Required Fields :
   
     This script is used to check the username is empty or not, if empty it will promt an alertbox to user with message "User Name Required" and it will focus on the particular username field.
if( document.myForm.username.value == null || document.myForm.username.value == "" )
{
      alert("User Name Required");
      document.myForm.username.focus();
      return false;
}
To Test Text Data or Digits(Numbers) Only:
 
     This script is used to check the field value is text or digits, based on the condition it will prompt an error, other wise ignores.
Here we need to create two expressions based on our requirements to test whether the field value is Text or Number.
var numbexp = /^[0-9]+$/;
var charexp = /^[a-zA-Z]+$/;

if( !document.myForm.firstname.value.match(charexp) )
{
      alert("Please Enter Text Data Only");
      document.myForm.firstname.value = "";  //This is used to remove the data from the field
     document.myForm.firstname.focus();
     return false;
}

if( !document.myForm.mobile.value.match(numbexp) )
{
     alert("Please Enter Numbers Only");
     document.myForm.mobile.value = "";  //This is used to remove the data from the field
     document.myForm.mobile.focus();
     return false;
}
To Test Email Fields :
     This script is used to check the field contains value is valid email or not, it will accept the value only if contains a '@' symbol, a '.' symbol and 2 to 3 charactors after dot symbol. Other wise prompt alert to user.
var emailexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if( !document.myForm.emailid.value.match(emailexp) )
{
     alert("Please Enter Valid Email Id");
     document.myForm.emailid.value = "";
     document.myForm.emailid.focus();
     return false;
}
To Check Length of the Field :
     This script used to check the length of the field value, if it has more than 10 characters it will show an alert message, other wise it will ignore.
if( document.myForm.username.value.length >= 10 )
{
     alert("Maximum 10 Characters only");
     document.myForm.username.value = "";
     document.myForm.username.focus();
     return false;
}
     This script is used to validate a value between a given range i.e, minimun and maximum value. Assume username should be Minimum 6 Characters and Maximum 12 Characters
if( document.myForm.username.value.length < 6 || document.myForm.username.value.length >12 )
{
     alert("User Name should contain min 6 characters and max 12 characters");
   document.myForm.username.value = "";
   document.myForm.username.focus();
   return false;
}
To Test Date Format :
     This script is used to check the date format according to your needs. using this expression we can validate the date field mostly.
var dateexp = /^[0-9]{4}\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])+$/;
if( document.myForm.dateofbirth.value.match(dateexp) )
{
     alert("Invalid Date Format... Correct it");
     document.myForm.dateofbirth.focus();
     return false;
}
To Test Select Boxes :
     This script is used to check the select box is selected or not.
if(document.myForm.gender.selectedIndex == 0)
{
      alert("Please Select Gender");
      document.myForm.gender.focus();
      return false;
}