Getting started with AJAX is super easy when you use the jQuery
library.
Most of the time, you’ll use one or two of them, but there is one method that rules them all. It’s the generic version of the call and it’s simply called ajax(). Learning how to use that method will take care of most of the types of request you’ll need for your apps.
PHP has a built in function called gethostbyname(). It returns the IPv4 address of a hostname.
You would use it like this:
If that’s working fine, you’re ready for some AJAX.
Now, we’ll need to create a couple of elements. One that will let us type in the domain, and one that will display what we get back from our PHP document.
Let’s keep it simple and use this:
1) What is AJAX?
AJAX, stands for Asynchronous JavaScript and XML. It’s just a way to have the client request data from the server without having to reload the page. jQuery has a number of built-in AJAX functions.Most of the time, you’ll use one or two of them, but there is one method that rules them all. It’s the generic version of the call and it’s simply called ajax(). Learning how to use that method will take care of most of the types of request you’ll need for your apps.
2) Setting up your PHP
Before we dig into the AJAX call, let’s create a small PHP script that we can use. It doesn’t have to be too complicated. Our script will take a domain name and return the IP address of the server it belongs to. The IP address is the physical location of the server on the internet. When you type in a domain into a browser like http://iviewsource.com, the domain gets converted to an IP address that your browser knows how to find on the internet.PHP has a built in function called gethostbyname(). It returns the IPv4 address of a hostname.
You would use it like this:
echo(gethostbyname('iviewsource.com'));
So the script we’ll write will use the server variable $_GET and
output the IP address. Your finished code should look like this:<?php
if ($_GET['ip']):
$ip = gethostbyname($_GET['ip']);
echo($ip);
endif;
?>
Coudn’t be simpler. To make sure this is working save this file as
reverseip.php and upload it somewhere on your server. To test it out,
type in a URL to it like:http://yourwebsite.com/getip.php?ip=iviewsource.com
Assuming you placed the file in the root of your browser, this should return a number like 64.207.155.59. Don’t type in the http://, just the domain name or it won’t work.If that’s working fine, you’re ready for some AJAX.
Preparing the page
First, we need to create a page that use the jQuery library. That code will look like this:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Get Reverse IP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
</body>
</html>
This shell is just an empty page that calls the jQuery library from
Google’s CDN. A Content Delivery Network that keeps a copy of popular
libraries like jQuery.Now, we’ll need to create a couple of elements. One that will let us type in the domain, and one that will display what we get back from our PHP document.
Let’s keep it simple and use this:
Please enter a domain name
<input type="text" id="searchip">
<div id="resultip"></div>
Now, let’s start writing the script that will trigger our AJAX call.
This is usually placed at the bottom of the screen right before the
closing <body> tag.<script>
$(document).ready(function() {
}); //document.ready
</script>
This script will just execute our code as soon as the document has
finished loading. The next step would be to run a function when
something happens. So, since we’re using an input field, we can check to
see when the input changes. <script>
$(document).ready(function() {
$('#searchip').change(function(){
}); //event handler
}); //document.ready
</script>
This is called an event handler. Different types of html elements require that you use different types of event handlers. jQuery event handlers are similar, but a little different than JavaScript event handlers. In our case, we’re interested in the change() event that happens when an input field or a select field change in value.Ready for the AJAX
We’re now ready for the AJAX call. We’ll use the generic ajax() jQuery method. It looks like this.<script>
$(document).ready(function() {
$('#searchip').change(function(){
$.ajax({
}); // Ajax Call
}); //event handler
}); //document.ready
</script>
The AJAX call can be done in a couple of ways: You can pass along a
URL as well as some settings, or just pass a JSON object with the
settings. This second way works really well. I need to set up four
variables.<script>
$(document).ready(function() {
$('#searchip').change(function(){
$.ajax({
type: "GET",
url: "getip.php",
data: 'ip=' + $('#searchip').val(),
success: function(msg){
$('#resultip').html(msg);
}
}); // Ajax Call
}); //event handler
}); //document.ready
</script>
The type variable tells the browser the type of call you
want to make to your PHP document. You can choose GET or POST here just
as if you were working with a form.
No comments:
Post a Comment