AJAX stands for Asynchronous JavaScript And XML, it is a technique for creating fast and dynamic web pages.
Ajax lets us send requests from the browser to the server, and manage the server response without page reload, so we can update a part of the page while the user remains on the same page.
jQuery has multiple methods (functions) for AJAX. In this tutorial it's presented the load() method.
$('element').load(url, data, complete)- url - specifies the URL to which the request is sent.
For security reasons, the external file with the content you load must be stored on the same domain (server) as the web page from which your script is running.
<h2>Content in extern.html</h2> <div id="cnt">Example jQuery Ajax, the load() method. <p>Some text content in extern.html</p></div>
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Ajax Test</title> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // select all the links with class="lnk", when one of them is clicked, get its "href" value // load the content from that URL and place it into the tag with id="content" $('a.lnk').click(function() { var url = $(this).attr('href'); $('#content').load(url); return false; }); }); --></script> </head> <body> <h1>Web page test.html</h1> <a href="extern.html" title="Get extern" class="lnk">Get extern</a> <div id="content">Initial content in test.html</div> </body> </html>We select all the links with class="lnk", when one of them is clicked, gets its "href" value (with .attr('href') ) and pass it into the load() function, which will load the content from that URL and place it into the tag with id="content".
$('element').html('loading...').load('url selector')- The "url" and the "selector" must be separated by space, in the same string.
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Ajax Test</title> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // select all the links with class="lnk", when one of them is clicked, get its "href" value // adds a "loading..." notification, load the content from that URL and // place only the paragraph which is in the #cnt into the tag with id="content" $('a.lnk').click(function() { var url = $(this).attr('href'); $('#content').html('<h4>Loading...</h4>').load(url+ ' #cnt p'); return false; }); }); --></script> </head> <body> <h1>Web page test.html</h1> <a href="extern.html" title="Get extern" class="lnk">Get extern</a> <div id="content">Initial content in test.html</div> </body> </html>To see the result, click on the link below:
$('element').load('script.php', data)- data can contain a string with "key=value" pairs separated by "&" that will be send through GET method. Or can contain an object (JSON type). If data is provided as an object, will be send through POST method.
<?php // check if there is data sent through GET, with key "pag" if(isset($_GET['pag'])) { $pag = strip_tags($_GET['pag']); // gets data and removes posible tags // sets a HTML content according to the value of $pag if($pag=='php') { $rehtml = '<b>PHP-MySQL course and tutorials:</b> <u>https://coursesweb.net/php-mysql/</u>'; } else if($pag=='ajax') { $rehtml = '<b>AJAX course:</b> <u>https://coursesweb.net/ajax/</u>'; } else { $rehtml = '<b>Free Web Development courses:</b> <u>https://coursesweb.net</u>'; } echo $rehtml; // output (return) the value of $rehtml } else { // if no $_GET with key "pag", returns a error message echo 'Error: Invalid data'; } ?>- The necessary explanations about this script are in its code.
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Ajax - PHP</title> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // when the user submit a form $('form').submit(function() { var url = $(this).attr('action'); // gets the url from "action" attribute of the current form // define data to be send to PHP // with the key "pag" and the value selected in the select list (with id="pag") var data = 'pag='+ $('#pag').val(); // adds a "loading..." notification, load the content from url (passing the data) // and place it into the tag with id="content" $('#content').html('<h4>Loading...</h4>').load(url, data); return false; }); }); --></script> </head> <body> <div id="content">Initial content ...</div> <form action="script.php" method="get"> Select course: <select name="pag" id="pag"> <option value="other">Other</option> <option value="php">PHP</option> <option value="ajax">Ajax</option> </select> <input type="submit" id="submit" value="Submit" /> </form> </body> </html>
$('element').load(url, data, function(response, status, xhr){ // code to execude when the Ajax request is sent })- response - contains the result data from the request.
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Ajax - check status</title> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // select all the links with class="lnk", when one of them is clicked, get its "href" value // load the content from that URL and check the "status" of the request // place the response or an error message into the tag with id="content" $('a.lnk').click(function() { var url = $(this).attr('href'); $('#content').load(url, function(response, status, xhr) { // check the status, if "success", place the response into #content // else, if "error", define an error message and insert it into #content // else, display an Alert with the status if(status=='success') { $('#content').html(response); } else if(status=='error') { var ermsg = '<i>There was an error: '+ xhr.status+ ' '+ xhr.statusText+ '</i>'; $('#content').html(ermsg); } else { alert(status); } }); return false; }) }); --></script> </head> <body> <h2>jQuery Ajax example</h2> <div id="content">Initial content...</div> <a href="script.php?pag=ajax" title="link to script.php" class="lnk">Link to script.php</a> <a href="nofile.php" title="link to nofile.php" class="lnk">Link to nofile.php</a> </body> </html>- The else { alert(status); } statement was added for the case when the status of the request is other then "success" or "error".
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);