PHP
Laravel Tutorial for Beginners
What is Laravel? Laravel is an open-source web MVC framework for PHP. Laravel is a robust...
AJAX full form is Asynchronous JavaScript & XML. It is a technology that reduces the interactions between the server and client. It does this by updating only part of a web page rather than the whole page.The asynchronous interactions are initiated by JavaScript.The purpose of AJAX is to exchange small amounts of data with server without page refresh.
JavaScript is a client side scripting language. It is executed on the client side by the web browsers that support JavaScript.JavaScript code only works in browsers that have JavaScript enabled.
XML is the acronym for Extensible Markup Language. It is used to encode messages in both human and machine readable formats. It’s like HTML but allows you to create your custom tags. For more details on XML, see the article on XML
We will create a simple application that allows users to search for popular PHP MVC frameworks.
Our application will have a text box that users will type in the names of the framework.
We will then use mvc AJAX to search for a match then display the framework’s complete name just below the search form.
Index.php
<html> <head> <title>PHP MVC Frameworks - Search Engine</title> <script type="text/javascript" src="/auto_complete.js"></script> </head> <body> <h2>PHP MVC Frameworks - Search Engine</h2> <p><b>Type the first letter of the PHP MVC Framework</b></p> <form method="POST" action="index.php"> <p><input type="text" size="40" id="txtHint" onkeyup="showName(this.value)"></p> </form> <p>Matches: <span id="txtName"></span></p> </body> </html>
HERE,
“onkeyup="showName(this.value)"” executes the JavaScript function showName everytime a key is typed in the textbox.
This feature is called auto complete
frameworks.php
<?php
$frameworks = array("CodeIgniter","Zend Framework","Cake PHP","Kohana") ;
$name = $_GET["name"];
if (strlen($name) > 0) {
$match = "";
for ($i = 0; $i < count($frameworks); $i++) {
if (strtolower($name) == strtolower(substr($frameworks[$i], 0, strlen($name)))) {
if ($match == "") {
$match = $frameworks[$i];
} else {
$match = $match . " , " . $frameworks[$i];
}
}
}
}
echo ($match == "") ? 'no match found' : $match;
?>
auto_complete.js
function showName(str){
if (str.length == 0){ //exit function if nothing has been typed in the textbox
document.getElementById("txtName").innerHTML=""; //clear previous results
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("txtName").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","frameworks.php?name="+str,true);
xmlhttp.send();
}
HERE,
Assuming you have saved the file index.php In phututs/ajax, browse to the URL http://localhost/phptuts/ajax/index.php
Type the letter C in the text box You will get the following results.
The above example demonstrates the concept of AJAX and how it can help us create rich interaction applications.
What is Laravel? Laravel is an open-source web MVC framework for PHP. Laravel is a robust...
Why use Comments? If you don’t work on the source code for some time, it’s easy to forget what the code...
PHP is a server-side scripting language used to develop static and dynamic websites or web...
What is PHP? PHP is a server side scripting language. that is used to develop Static websites or...
What is a Function? A function is a reusable piece or block of code that performs a specific...
$20.20 $9.99 for today 4.4 (119 ratings) Key Highlights of PHP Tutorial PDF 269+ pages eBook...