Another way to send data from a Web page to the server is via POST method.
With Ajax, the request for sending data with POST uses the open() method of the XMLHttpRequest object, its syntax is:
open ("POST", URL, bool)- the "POST "is the method of transfer
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")- This function indicate what type of content we are including, it sends a header to tell the server to recognize the sent data as if they were sent with POST (like data from Forms).
<?php // if data are received via POST, with index of 'test' if (isset($_POST['test'])) { $str = $_POST['test']; // get data echo "The string: '<i>".$str."</i>' contains ". strlen($str). ' characters and '. str_word_count($str, 0). ' words.'; } ?>- This is a relatively simple PHP script. If it receives data via POST, with index of 'test', takes its value in the $str variable and, with "echo" returns a string that contains the received text and the number of characters and words in that text.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ro"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-2" /> <title>Example Ajax POST</title> <script type="text/javascript"><!-- // sends data to a php file, via POST, and displays the received answer function ajaxrequest(php_file, tagID) { var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object // create pairs index=value with data that must be sent to server var the_data = 'test='+document.getElementById('txt2').innerHTML; request.open("POST", php_file, true); // set the request // adds a header to tell the PHP script to recognize the data as is sent via POST request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.send(the_data); // calls the send() method with datas as parameter // Check request status // If the response is received completely, will be transferred to the HTML tag with tagID request.onreadystatechange = function() { if (request.readyState == 4) { document.getElementById(tagID).innerHTML = request.responseText; } } } --></script> </head> <body> <h5 style="cursor:pointer;" onclick="ajaxrequest('test_post.php', 'context')"><u>Click</u></h5> <div id="txt2">This string will be sent with Ajax to the server, via POST, and processed with PHP</div> <div id="context">Here will be displayed the response from the php script.</div> </body> </html>- The Ajax script is executed when the user click on the "Click" word. It calls the ajaxrequest() function with two parameters. The first parameter represents the name of the php file where the data will be sent, and the second parameter is the ID of the tag where the server response will be displayed.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net