MY mENU


Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, 25 January 2013

Methods in Java


Method: method is a block of a class that contains logic of that class. Logic must be placed only inside a method, not directly at class level. If we place logic at class level compiler throws error.

Method Prototype: the head portion of the method is called method prototype.

Method body and logic: the “{}” region is called method body, and the statements placed inside method body is called logic.

Method parameters and arguments: the variables declared in method parenthesis “( )” are called parameters. We can define method with ZERO to ‘n’ number of parameters.
The values passing to those parameters are called arguments. In method invocation we must pass arguments according to method parameters order and type.

Ex: void add (int a, float b) {  }     //here int a, float b are method parameters
Add (50, 60.5);   // here 50, 60.5 are arguments.

Method Signature: the combination of [Method name+parameters list] is called method signature.

Ex: void add (int a, int b) { }   // here add (int a, int b) is the method signature.

Method Return type: the keyword that is placed before method name is called method return type. It tells to compiler and JVM about the type of the value is returned from this method after its execution.

Void return type keyword: if we do not want to return any value a method, we must use “void” as return type. It tells that the method does not return any value. If we want to return any value a method, we must place data type keyword as return type.

Main method terminology with all above parts:

Public static void main (String[] args)

Public -- Accessibility modifier
Static----Modifier
Void----Return Type
Main----Method Name
String [ ]----Parameter Type
Args------Parameter Name

The process of creating method with body is called method definition. This method is called Concrete Method.

Ex: public static void add(int a, int b){
System.out.println(a+b);
}

The process of creating method without body is called declaring a method/ method declaration. This method is called also abstract method. In method declaration the modifier “abstract” is mandatory and also should be terminated with “;”.

Ex: public native abstract void add(int a, int b);

  •  When we call a method control send to that method.
  •  If we pass argument that value is stored in parameter variable.
  • After method execution that parameter variable is destroyed and control is sent back to calling method.
  •   Control is sent back to calling method with value if method has return type is not void.


    Types of Methods: Basically concrete methods are divided into 3 types:
       Based on static modifier we have two types of methods
  •  Static methods
  •  Non-static methods

We can call static methods directly from main method, but we cannot call non-static methods directly from main method. It leads CE:”non-static method cannot be referenced from static context”, because class level members will not get memory directly. JVM provides memory only if we use either “static or new” keywords.

Main method has static keyword in its prototype since it is the initial point of class login execution; it must be identified and gets memory directly by JVM. Hence it has static keyword in its definition. Of course it must be called without object creation.

class MethodStatic
{          static void m1(){
                                System.out.println("in M1()");
                }
                void m2(){
                                System.out.println("M2() method");
                }
                public static void main(String[] args)
                {
                                System.out.println("Hello World! this is main Method");
                                m1();
                                MethodStatic ms=new MethodStatic();
                                ms.m2(); //it gets memory with reference to “ms” variable.
                }  }

2.  Based on return type we have two types of methods
  •  Void methods
  •  Non-void methods

In void methods statements are optional, but in non-void methods return statement is mandatory with value range less than or equals to method return type range.
If we do not place return statement in non-void methods compiler throws CE: “missing return statement”

Types of return statements:  return is only allowed in void method and constructor, and it is optional.

 Return is only allowed in non-void methods, and it is mandatory.

Basically return statement is used to terminate method execution and for sending control back to calling method.

In general, void methods are called in three ways 1. Directly—m1(); 2. As variable initialization statement—int x=m1();; 3.As SOPLN() argument—Sopln(m1());

The non-void methods can be called in all three ways.  In the first way the return value is lost. In the second way the returned value is stored in the destination variable. In the third way the returned value is printed on console.

3Based on parameter we have two types of methods
  •          Parameterized methods
  •          Non-parameterized methods


Wednesday, 23 January 2013

Why OOPs?

  • OOPs stands for “Object Orient Programming System”. It’s a technique not technology.
  • It means, it does not provide any syntaxes or API’s, instead it provides suggessitions to design and develop objects in programming language.
  • This technique is introduced to represent real world object in programming languages, the objects such as person, Animal, Shape, Vehicle, etx… and further to achieve security and to communicate with other programming languages, such as C,C++,.NET,PHP,HTML,JAVA etc… with proper connectors.
  • All living and non-living things are considered as object.
  •  If we developed project according to real world object behavior we will get many advantages.
Definition of OOPs:
  • OOP is an approach that provides a way of modularizing a program by creating partitioned memory area for both data and methods that can be used as template for creating copies of such modules on demand.
  • Unlike procedural programming, here in the OOP programming model, programs are organized around objects and data rather than actions and logic.
Building blocks of OOP: The building blocks of OOP are
  • Class
  • Object
  • Every java program must start with a class, because using class only we can represent real world objects like person, Bike, Animal.

Sunday, 30 September 2012

Create fancy contact form with CSS 3 and jQuery


This tutorial is about creating a simple contact form using XHTML CSS, PHP, and jQuery. The Aim of this article is to show some of the nice CSS 3 style properties such as adding Background Gradient, Corners and Borders to Form Elements.

Step 1 – The HTML Mark Up

The Step 1 is the backbone of the form. It Contains the HTML Code to add textboxes, textarea and a submit Button.
01<form>
02<p>
03<input name="name" type="text" id="name"/>
04<label for="name">Name </label>
05</p>
06<p>
07<input name="name" type="text" id="email"/>
08<label for="email">Email </label>
09 </p>
10<p>
11<input name="website" type="text" id="website"/>
12<label for="website">Name </label>
13 </p>
14<p>
15<textarea name="text"></textarea>
16 </p>
17<p>
18<input value="Send" type="submit" id="Send"/>
19 </p>
20</form>
Step 1 - xHTML Form Backbone

Step 2 – Styling with CSS ( Without CSS3 Properties )

In this step, Basic CSS Style is added to make the form a bit more elegant .Pretty Simple Stuff here, nothing special. The wooden background texture used has been taken from flickR here .
01body { padding:50px 100px; font:12px Verdana, Geneva, sans-serif;
02background:url("wooden_background.jpg") repeat scroll 0 0 #282828;
03 }
04 
05input, textarea {
06 padding: 8px;
07 border: solid 1px #E5E5E5;
08font: normal 12px Verdana, Tahoma, sans-serif;
09 width: 200px;
10 
11 }
12 
13textarea {
14 width: 400px;
15 max-width: 400px;
16 height: 150px;
17 line-height: 150%;
18 }
19 
20.form label {
21 margin-left: 10px;
22 color: #999999;
23 }
24 
25.submit input {
26 width: auto;
27 padding: 9px 15px;
28 background: #617798;
29 font-size: 14px;
30 color: #FFFFFF;
31 cursor:pointer;
32 }
33#form-div {
34background-color:#F5F5F5;
35padding:15px;
36}
37 
38#wrapper {
39margin:30px auto;
40width:500px;
41}
Step 2 : Basic CSS Style

Step 3 -Adding CSS 3 to beautify the Form

We now use some CSS3 Style properties to add some shadow effect , a gradient to the background of each form element and also rounded corners .The thing about Css 3 Property is because it is not the standard and officially yet , so we have to use browser-version CSS3 Property . So for Mozilla we use ,we use the  ‘-moz’ prefix and  for webkit browsers such as safari and chrome we use ‘-webkit’ .
  • Add Shadow Effect
  • 1box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/* Default property recognized by some browsers- a Good practice to include it*/
    2-moz-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/*For Mozilla Firefox Browsers*/
    3-webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;/*For Webkite browsers - Chrome and Safari*/
    4    
    This CSS Property Takes a color and 3 Length as attribute .
    Color - It can be in Hex/Rgb/Rgba
    Horizontal offset-Positive Value means shadow will be on the right, and a negative value indicates shadow will be on the left.
    Vertical Offset
     -Positive value means shadox will be on the top and a Negative value indicates shadow will be below .
    Blur Radius - The higher the number , the more blurred it will be .
    For this example , i have used RGBA .As compared to RGB , RGBA is simply color with Opacity .
    Syntax for RGBA :
    1rgba[RED][GREEN][BLUE][ALPHA]
    2 
    3/*Alpha  = Opacity*/
  • Add Gradient to Background
  • 1background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #EEEEEE), to(#FFFFFF));/*Chrome and Safari*/
    2 background: -moz-linear-gradient(top, #FFFFFF, #EEEEEE 1px, #FFFFFF 25px);/* Firefox Browsers */
    3background: #FFFFFF url('bg_form.png') left top repeat-x;
    4    
    The 3rd one is not a CSS 3 Property .It’s purpose here is to mimic it in browsers like Internet Explorer 6,7,8 since it does not support CSS3 .
  • Add Rounded Corners
  • 1-moz-border-radius:12px 12px 12px 12px;
    2-webkit-border-radius: 12px 12px 12px 12px;
    Unfortunately rounded corners will not be rendered in Internet Explorer .You can go to CurvyCorners to make it work on IE Browsers .
    Step 3 : Form with CSS3 Properties

Step 4-Client Side Validation and Ajax Submission with jQuery

I used the Validation Engine Plug in to add Validation to the form. You can find more about this with. Cedric’s Post .
01$(document).ready(function() {
02 // SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() },
03 $("#form1").validationEngine({
04 ajaxSubmit: true,
05 ajaxSubmitFile: "ajaxSubmit.php",
06 ajaxSubmitMessage: "Thank you, We will contact you soon !",
07 success :  false,
08 failure : function() {}
09 })
10 
11});
Step 4 : jQuery Validation Added

Step 5- Sending Mail with PHP

The PHP Code below handles the Email Submission For the online Demo ,the Email Submission has been disabled to prevent abuses. You can download the source code using the download link below to get the full working code.
01//Code Updated on 18 Feb 2011
02 
03$name = $_POST['name']; // contain name of person
04$email = $_POST['email']; // Email address of sender
05$web = $_POST['web']; // Your website URL
06$body = $_POST['text']; // Your message
07$receiver = "ismaakeel@gmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
08if (!empty($name) & !empty($email) && !empty($body)) {
09    $body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
10    $send = mail($receiver, 'Contact Form Submission', $body, "From: {$email}");
11    if ($send) {
12        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
13    }
14 
15}