Shri Vaishnav Institute of Computer Application
Assignment – III
Semester – IV
Section - C
Submitted by : Submitted To :
Mohammad Zeeshan Khan Prof. Priyasi Jain
Enrollment no:
2204MCA0013238
INDEX
S.no Subject Page No
1. Question 1 1-2
2. Question 2 3-4
3. Question 3 5-7
4. Question 4 8-11
5. Question 5 12-14
Q1. What is jQuery and how is it used in PHP.
jQuery is a popular JavaScript library designed to simplify client-side scripting of HTML. It
provides a concise and easy-to-use API that abstracts away many of the complexities of raw
JavaScript, making it simpler to manipulate HTML documents, handle events, perform
animations, and interact with server-side technologies like PHP.
In the context of PHP, jQuery is typically used on the client-side within web pages that are
generated by PHP scripts. Here's how jQuery is used in conjunction with PHP:
Client-Side Interactivity: PHP generates HTML content (which may include
JavaScript/jQuery code) that is sent to the user's browser. This content often includes
jQuery scripts embedded within `<script>` tags. For example, a PHP script might
generate an HTML page that includes jQuery to handle form validation or dynamic UI
updates.
<?php
// PHP generating HTML with embedded jQuery
?>
<html>
<head>
<title>jQuery and PHP Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
// jQuery code here to manipulate the DOM
$("button").click(function(){
$("p").text("Hello World!");
});
});
</script>
</head>
<body>
<p>Click the button to change this text.</p>
<button>Click me</button>
</body>
</html>
Handling AJAX Requests: jQuery is commonly used to make AJAX
(Asynchronous JavaScript and XML) requests to PHP scripts on the server. This
allows parts of a web page to be updated dynamically without requiring a full page
reload. PHP scripts can process the incoming requests, interact with databases,
perform computations, and return data (usually in JSON format) back to the client-side
jQuery code for further processing.
$.ajax({
Page | 1
url: 'process.php',
type: 'POST',
data: {name: 'John', age: 30},
success: function(response) {
// Process response from PHP script
console.log(response);
},
error: function(xhr, status, error) {
// Handle errors
console.error(error);
}
});
UI Enhancements and Animations: jQuery simplifies the process of adding
interactive elements and animations to a webpage. PHP can dynamically generate
HTML content that is then enhanced with jQuery to provide a more engaging user
experience.
Page | 2
Q2. What is ajax and how is it used in PHP.
Ajax
It just uses a combination of a built-in XML Http requests objects of the browser and
JavaScript in the background which is used to request data from the server. We can
display the data via HTML to the web browser where user will see the data response
within penalty of time gap.
Different tasks are done by all:
HTML & CSS is for the representation
Json/Xml is for sorting data
XMLHttp request object is for action in the background
JavaScript puts all this together.
Asynchronous JavaScript and XML is used in web pages or web applications to be
updated asynchronously by swapping the data with the web server behind the scene. This
makes possible for the developers to update the particular part of the websites without
reloading the whole page.
If we talk about JavaScript, it is known as a client-side programming language on the
other hand we have XML which stands for Extensible Markup Language that is used to
define data.
Types of Ajax
There are two types of Ajax:
Asynchronous Ajax
Asynchronous type call allows us to execute the next line even if the previous code isn’t
completed.
Synchronous Ajax
Synchronous type call stops JavaScript execution until the response from the server.
Remember that Asynchronous Ajax will affect the SEO because we will not have specific
URL’s of the pages.
Ajax in PHP
A majority of PHP based sites are using Ajax & jQuery-based solutions for better user
experience on their websites because asynchronous JavaScript and XML allows for such
rich features in the website which PHP alone or just JavaScript can’t provide us in the
website. Some of the features are:
Search auto suggestions
Form submitting without page reload
Content loading on the scroll
So while working with PHP a server-side language using Ajax for some small tasks will
make our lives easy. We don’t need to make multiple pages and it also provides a better
user experience because user have not to wait for the page to be reloaded again.
Page | 3
One of the best things is that it is very easy to write Ajax code with jQuery in comparison
to the Ajax with JavaScript.
There are many advantages of using Ajax in our web applications, some of them are
mentioned below:
Callbacks:
Ajax make a callback in background. It makes possible to communicate with the
server very fast without the page reload.
Asynchronous:
By using Ajax we can achieve asynchronous call to website servers. This method
allows the browsers to avoid the waiting period to get the data.
User-Friendly:
The applications which use Ajax are faster and more responsive to the user in
comparison of others web applications which don’t use Ajax.
Page | 4
Q3.Give an example of ajax in PHP.
The following example will demonstrate how a web page can communicate with a web
server while a user types characters in an input field:
Explanation
In the example above, when a user types a character in the input field, a function called
"showing()" is executed.
The onkeyup event triggers the function.
Here is the HTML code:
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form action="">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
Code explanation:
Page | 5
First, check if the input field is empty (str.length == 0). If it is, clear the content of the
txtHint placeholder and exit the function.
Do the following, if the input field is not empty:
Create an XMLHttpRequest object
Create the function to be executed when the server response is ready
Send the request off to a PHP file (gethint.php) on the server
Notice that the q parameter is added to the url (gethint.php?q="+str)
And the str variable holds the content of the input field
The PHP File - "gethint.php."
The PHP file checks an array of names, and returns the corresponding name(s) to the
browser:
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";
// fetch q parameter from URL
$q = $_REQUEST["q"];
Page | 6
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
// It results in "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>
Output:
Page | 7
Q4. What are the framework used in PHP.
A PHP framework is a platform which allows a web developer to develop the web
application. In simple words, it provides a structure to develop web application. These
frameworks save lots of time, stop rewriting the repeated code and provide rapid application
development (RAD). PHP frameworks help the developers to rapid development of
application by providing the structure.
Following a list of top 10 PHP frameworks is given below:
Laravel Framework
CodeIgniter Framework
Symfony Framework
Zend Framework
Laravel
Laravel is an open-source web application framework which released in June 2011. It is
developed by Taylor Otwell. Laravel is very popular because it handles complex web
applications more securely and faster than other frameworks. Laravel makes lots of common
tasks of web projects much easier, such as authentication, session, routing, and caching, etc.
The main goal of Laravel is to make web development easy and best for the developers
without sacrificing application functionality. It provides powerful tools which are necessary
for large and robust application. An inversion of control container, tightly integrated unit
testing support, and expression migration system provides the tools that help to build any
application with which we are tasked.
Advantage of Laravel Framework
o Laravel follows the MVC design pattern. It offers the following benefits that are given
below:
o Laravel makes web application scalable and more secure than other frameworks.
o It includes interfaces and namespace, which helps to organize and manage resources.
o Laravel reuse the components from the other frameworks in web application
development that saves the time of developer to design the web application.
Disadvantage of Laravel Framework
o Legacy systems do not easily transfer to Laravel.
o Some upgrades might be problematic in Laravel
o Methods like reverse routing and caching are complex.
Page | 8
CodeIgniter
CodeIgniter is an application development framework with small footprints which makes it
much faster than other frameworks. It was released on February 28, 2006, by EllisLab. It
minimizes the size of code needed for a given task and provides easy solutions to the
complex coding problems.
CodeIgniter is not totally based on the MVC framework. It is one of the oldest frameworks
with faster and high performance. We can develop projects much faster than a scratch, as it
provides a large set of libraries, simple interface, and logical structure to access these
libraries. It can be easily installed, and it requires minimal user configuration.
Advantage of CodeIgniter Framework
o CodeIgniter is an open-source and lightweight framework.
o CodeIgniter is faster with database tasks than compared to other frameworks.
o It is easy to install and well documented, so it is good for PHP beginners.
o It offers built-in security tools.
o CodeIgniter provides exceptional performance.
o
Disadvantage of CodeIgniter Framework
o CodeIgniter does not support modular code separation. Hence, developers need to put
extra effort and time to maintain the code.
o It is the only PHP based but not object-oriented in some parts.
o CodeIgniter has no built-in ORM.
o It has fewer tools and built-in libraries than other frameworks.
Page | 9
Symfony
Symfony is another popular framework which was introduced on October 22, 2005,
by Fabian Potencier. It is released under MIT license. It is a set of PHP components to create
websites and web application. Symfony framework is a perfect choice among frameworks to
develop large-scale enterprise projects.
Symfony is an open-source framework of PHP which is sponsored by SensioLabs. This
framework is designed for developers who create a full-featured web application. Lots of
open source projects like Drupal, Composer, and phpBB use Symfony components. Symfony
integrates with PHP Unit and independent library. Symfony framework is flexible and
handles enterprise application with billions of connections. It is used to build micro-sites.
Advantage of Symfony Framework
o Symfony is a flexible and powerful framework.
o It prevents from web attacks and SQL injection.
o Symfony framework provides code reusability and easy maintenance.
o It maintains complete, clearly written, well-structured, and up-to-date documentation.
o
Disadvantage of Symfony Framework
o Symfony framework needs more efforts to learn than other frameworks such as
Laravel and Yii.
o Security is a bit hard in the Symfony framework.
o Performance and speed are the main drawbacks of it.
o Large-scale applications are developed using Symfony rather than small-scale.
o
Page | 10
Zend Framework
Zend is an open-source, web application framework, which is developed on March 3, 2006.
It is a collection of 60+ packages which is available on GitHub and can be installed via
composer. Zend is pure object-oriented, which is developed on the basis of the MVC design
pattern. Zend was developed in agile methodology that helps to deliver a high-quality
application to the enterprise client.
IBM, Google Microsoft, and Adobe are the partners of Zend. There are many features comes
with Zend Framework version 2 such as drag and drop editor with front-end technology
support (HTML, JavaScript, CSS), cryptographic coding tool, PHP Unit testing tool, instant
online debugging, and a connected database wizard.
Advantage of Zend Framework
o Zend is highly customizable framework. Performance of Zend Framework version 3 is
four times higher than its previous version.
o We can easily test the framework because PHPUnit is integrated with Zend.
o Zend has a large community base, and it is well-documented.
o We can delete modules and components which has no use in the application.
o We can use the components of our own choice.
o It supports multiple databases, such as MySQL, PostgreSQL, Microsoft SQL,
and Oracle.
o
Disadvantage of Zend Framework
o Zend Framework is heavy and huge as it has a larger set of libraries, components, and
classes.
o Cost of plug-ins of Zend Framework is higher than other frameworks.
o It has large documentation with minimal detail of the framework, hence
documentation is hard to use as a guideline for the whole development of the project.
Page | 11
Q5. How are JavaScript and PHP used to create a website.
JavaScript and PHP are two fundamental technologies used together to create dynamic and
interactive websites. They play complementary roles in handling different aspects of web
development: client-side (JavaScript) and server-side (PHP). Here's how they work together
to build a website:
JavaScript (Client-Side)
JavaScript is a scripting language that runs in the web browser (client-side). It is primarily
used to enhance the user interface and interactivity of a website.
DOM Manipulation: JavaScript is used to manipulate the Document Object Model
(DOM) of a webpage. This allows developers to dynamically change the content,
structure, and style of elements on a webpage based on user interactions or other
events.
```javascript
document.getElementById('myButton').addEventListener('click', function() {
document.getElementById('myElement').style.color = 'red';
});
```
Event Handling: JavaScript is used to handle various user events such as clicks, key
presses, form submissions, etc. These events trigger specific actions or functions
defined in JavaScript code.
```javascript
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission
// Validate form fields using JavaScript
// Make AJAX requests to the server
});
```
AJAX Requests: JavaScript enables asynchronous communication with the server
using XMLHttpRequest (XHR) or the newer Fetch API. This allows parts of a
webpage to be updated without reloading the entire page.
```javascript
fetch('data.php')
.then(response => response.json())
.then(data => {
// Process data received from PHP script
Page | 12
console.log(data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
```
PHP (Server-Side)
PHP is a server-side scripting language designed for web development. It runs on the web
server and is used to generate dynamic web pages, handle form submissions, interact with
databases, and perform other server-side tasks.
Generating HTML: PHP scripts generate HTML content dynamically based on data
retrieved from databases, user input, or other sources. The generated HTML is then
sent to the client's web browser for display.
```php
<?php
$name = 'John';
echo "<h1>Hello, $name!</h1>";
?>
```
Handling Form Submissions: PHP processes form data submitted by users, validates
it, and performs necessary actions (e.g., saving to a database, sending an email).
```php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
// Validate and process the submitted form data
}
?>
```
Database Interaction: PHP interacts with databases (e.g., MySQL, PostgreSQL) to
retrieve or store data requested by the client-side JavaScript.
```php
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
Page | 13
$users = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($users);
mysqli_close($conn);
?>
```
Integration
JavaScript and PHP work together to create dynamic and interactive websites:
- JavaScript handles client-side interactions, DOM manipulation, and AJAX requests.
- PHP processes server-side logic, generates dynamic content (HTML, JSON, etc.), interacts
with databases, and handles form submissions.
Developers often use JavaScript frameworks/libraries like jQuery, React, or Vue.js alongside
PHP to streamline development and create more powerful and responsive web applications.
The integration of JavaScript and PHP enables the creation of feature-rich websites with
dynamic content and a smooth user experience.
Page | 14