i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
Above we saw the example of getting the input hints using a PHP file. We can also get the data from the database for AJAX. The webpage will fetch the information from a database with the help of AJAX and give the results on the webpage without refreshing it.
For example: First, we will create a table named comments as below:
create table comments(
id int(10) not null auto_increment primary_key,
author text not null,
message text not null
);
We will manually add comments in the database as below:
insert into comments (author, message) values (‘John’, ‘The product is quite amazing.’);
insert into comments (author, message) values (‘Roy, ‘I love the quality of this product.);
insert into comments (author, message) values (‘John’, ‘It has a beautiful finish.’);
insert into comments (author, message) values (‘Sebastian’, ‘The glass is quite fragile.’);
insert into comments (author, message) values (‘Roy, ‘It is very durable and elegant.’);
Now, we will connect this database to our webpage using database connection.
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “MySQL”;
$conn = mysqli_connect($servername, $username, $password, $dbname);
?>
Now, we will write the code to show the comments.
$sql = SELECT * from comments LIMIT 2;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >0) {
while ($rows = mysqli_fetch_assoc($result))
{
echo “
”;
echo $row [‘author’]. “
”;
echo $row[‘message’]. “
;
}
} else {
echo “No comments”;
}
We need to write this php code inside an HTML form having a button with description Show more comments. The above code will only show first 2 comments. After adding below AJAX code, when we click on show more comments, then two more comments will be loaded without refreshing the page
Load more Comments
The load-comments.php file will have the PHP code we wrote above with a small variation. The LIMIT will be sent through the AJAX code instead of keeping it as LIMIT 2.
$comment_new_count = $POST [‘comment_new_count’];
$sql = SELECT * from comments LIMIT $comment_new_count;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >0) {
while ($rows = mysqli_fetch_assoc($result))
{
echo “
”;
echo $row [‘author’]. “
”;
echo $row[‘message’]. “
;
}
} else {
echo “No comments”;
}
The output of the above code will have 2 comments first, and it will keep on increasing with two more comments without refreshing the page as you click on Load more comments button.
Don't miss out!