Follow the order in this post and make sure to practice all the tutorials and also share your knowledge with our awesome community on our official forum.
If you got to learn anything from our website, then please do not forget to share this page with your friends on Facebook, Google Plus, LinkedIn, Twitter etc..
Things covered: Defining class. Creating Objects. Public and Private access of Properties/Data. Separating Class file and the application file.
What is a Class ? A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.
class is a keyword. Add is the name of the class we’ve assigned. $a and $b are two variables of class Add. These have private access specifier, meaning, they’re not accessible directly from outside the class. Public access specifier indicates that they can be accessed even outside the scope of a class. Private variables are treated as safe, so we prefer that.
Since private variables are accessed inside the class, we declared two methods inside the class Add. i.e., setValues() and add() setValues() has two parameters which are then assigned to the local variables $a and $b using the $this pointer.
$this pointer references to the object which is currently pointing to it.
add() method adds the user passed values and returns the result.
Here we include the add.php file and create an object of (class)type Add. Using this object we pass values to setValues(), and then call add() method, which returns the added value of the passed numbers. which is then output to the browser.
We could create as many objects as we wish and then pass and output as many results as required, without altering the class file what so ever, once finalized.
So this is the power of Object Oriented Programming. This way we could manage complex applications easily or in an organized/standard way. Increase code re-usability. Also reduce maintenance cost etc..
Why OOP in PHP ? Most web projects still use procedural i.e., function based software development approach. And honestly, it has no issue, because most web projects are so small scale and straight forward that they don’t require OOP in most cases.
But for complex application development, you need OOP for effectiveness. Like, if you want to build a home, you need a lot of planning, preparation and some standard approach to build it.
In this Video tutorial, we build a login form which checks username and password and displays appropriate message on the same page, without refreshing the page.
Here we get the username and password from the user and check it against our database table records.
If the username, password combination is present, then the login success message is displayed, else login failed message is shown to the user.
We protect our application against sql injection by wrapping the user entered data with mysql_real_escape_string() method. Next we check whether the username and password is not empty: server side validation. If it’s not empty: We check if the username and password combination entered by the user is actually present inside our database. Also note that, we md5() encrypt the user entered password, inorder to match with the md5() encrypted data present in our database: encrypted while registering. If the username, password combination is present, then we display “Login Successful” else “Failed To Login”.
These messages will be caught by the call back function of jQuery and is displayed to the user.
jQuery File: With jQuery AJAX Mehod, $.post my_script.js
Here we validate the data at client end by checking if the user has entered both username and the password, if entered, we post the user entered data to login.php file after serializing it with jQuery serializeArray() method.
Once the login.php processes the data, the call back function present in our $.post() method accepts the data from login.php and displays it to the user. Also we disable the form redirection by returning false, once the user clicks on the submit button of the form.
We have just illustrated the way by which you can ajaxify the login form. Using this method, you could do more. Like, redirect or include a page after the login is successful etc..
This video tutorial is a basic building block to all those amazing jQuery AJAX login forms.
If the username and the password fields are empty, we display appropriate message and skip the execution of $.post() method. This ensures that, we do not request data from the server when there is no need for it.
If the user has entered both username and password, then we execute $.post() method and pass the user entered data to process.php file.
At the beginning we check if the username or the password is empty. If they are empty, we echo Username and Password are madatory – from PHP and then stop further execution of the script.
If the username and password are not empty, then we check the user entered username against the usernames present inside the database. If the username is already present inside the database, then we intimate it to the user with a customized message.
Registration Form Validation: PHP + jQuery + AJAX (PART 2)
Why Validate both client side as well as server side ? What if javascript has been disabled on client machine i.e., the browser ? In this situation, our client side validation completely fails. So, server side validation is also important.
Then Why client side validation when server side validation could serve our purpose ? This is because, client side validation is faster. i.e., if user tries to register empty data, it needs to travel across, reach the server, execute the validation rules script and then travel back to report that the user had submitted empty data and is not acceptable! Instead of this lengthy, time consuming and costly process, we could simply write a client side validation and it responds back instantly, saving time, bandwidth and hence the cost of processing the data.
Video tutorial illustrates development of user registration form using jQuery AJAX method. In this tutorial we shall build a registration form using which users can register without leaving the webpage. i.e., without being redirected to any other pages once he / she hits the submit/register button.
Here we have a simple form with username, password, first name, last name fields and a button. Also a div with an id of ack where appropriate messages are displayed to the users.
Form has an id of myForm. process.php file in the action field. POST method.
Here we use mysql_real_escape_string() method to avoid sql injection by the user. We accept all the values entered by the user and store them in php variables.
Later we formulate MySQL query and pass these user entered values to it and execute it using mysql_query() PHP method.
Using the conditional logic statement we check if the query got executed or not. Then output appropriate message using echo.
This message will be retrieved by the call back function in jQuery and is being further processed as required.
jQuery File: Accessing The Data And Displaying my_script.js
Once the user clicks on the button with an id submit, we call $.post() method. First parameter being the URL to which the data is being sent. Second parameter is the actual data, which is being serialized using serializeArray() method of jQuery. It formats the user entered data into key => value pairs. Third parameter, is the call back function.
Using the data returned by process.php we display the data in div tag with an id of ack.
We also disable the redirection of the webpage by returning false once the user clicks on the button.
jQuery File: Clearing the input fields my_script.js
1
2
3
4
5
6
7
function clear() {
$("#myForm :input").each( function() {
$(this).val("");
});
}
function clear() {
$("#myForm :input").each( function() { $(this).val("");
});
}
It’s a custom function which selects all the input fields, loops through them and assigns an empty value.