Call PHP Function In Ajax, Synchronous XMLHttpRequest Deprecarted
Solution 1:
You should use true in the async parameter.
Something like this: xmlhttp.open("GET", "update.php?status=disp", true);.
You could use this helper function to make XMLHttpRequest.
Works in all browsers, incluiding IE6.
var newXHR = null;
function sendXHR(type, url, data, callback) {
newXHR = new XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP");
newXHR.open(type, url, true); // Use async = true to avoid bad user experience for waiting a Sychronous request that might block the UI.
newXHR.send(data);
newXHR.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
callback(this.response); // Callback function to process the response.
}
};
}
Usage:
sendXHR("GET", "update.php?status=disp", null, function(response) {
console.log(response);
});
You could retrieve data by using this demo:
(function() {
var newXHR = null;
function sendXHR(type, url, data, callback) {
newXHR = new XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP");
newXHR.open(type, url, true); // Use async = true to avoid bad user experience for waiting a Sychronous request that might block the UI.
newXHR.send(data);
newXHR.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
callback(this.response); // Callback function to process the response.
}
};
}
// Example 1: Get data.
var btnRequest = document.getElementById("btnRequest");
btnRequest.onclick = function() {
sendXHR("GET", "https://gist.githubusercontent.com/dannyjhonston/22851f9c21733973b2705b0b65443f90/raw/30cf99ceeb470a7ab6d2ffb51a78f1bb57f41ca3/data.txt", null, function(response) {
document.getElementById("myDiv").innerHTML = response; // response contains the data from the server.
});
};
// Example 2. Cancel a long request.
var btnCancelRequest = document.getElementById("btnCancelRequest");
btnCancelRequest.onclick = function() {
newXHR.abort(); // You can use the newXHR variable to abort a XMLHttpRequest that is taking long time to response, with the .abort() method.
};
})();
#myDiv {
border: solid 1px #ccc;
border-radius: 5px;
margin: 20px;
padding: 5px;
}
<button id="btnRequest" type="button">Request data</button>
<button id="btnCancelRequest" type="button">Cancel request</button>
<div id="myDiv">The new content will print here.</div>
It's good to know this:
XMLHttpRequest.responseproperty returns the response's body. It can be of the typeArrayBuffer,Blob,Document,JavaScriptobject, or aDOMString, depending of the value ofXMLHttpRequest.responseTypeproperty. Value of response is null if the request is not complete or was not successful. However, if the value ofresponseTypewas set to"text"or the empty string, response can contain the partial text response while the request is still in the loading state.
XMLHttpRequest.responseTextproperty returns aDOMStringthat contains the response to the request as text, ornullif the request was unsuccessful or has not yet been sent. TheresponseTextproperty will have the partial response as it arrives even before the request is complete. IfresponseTypeis set to anything other than the emptystringor"text", accessingresponseTextwill throwInvalidStateErrorexception.
Your php code must be like this:
$status = $_GET["status"];
if($status=="disp")
{
$link = mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"checkbox");
$res = mysqli_query($link,"SELECT * FROM table1");
$html = "<table>"; // Declare a variable to concat the html content.
while($row = mysqli_fetch_array($res))
{
$html += "<tr><td>";
$html += $row["id"];
$html += "</td><td>";
$html += $row["name"];
$html += "</td><td>";
$html += $row["city"];
$html += "</td></tr>";
}
$html += "</table>";
echo $html; // Prints the $html variable.
}
In you html code you need to remove:
<script src="jquery-3.2.1.min.js"></script>
It's not necessary if you gonna use native javascript.
Solution 2:
try this way
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "update.php?status=disp",false);
xmlhttp.send();
use xmlhttp .send(); method for send a request to athore page
To avoid this warning, do not use false in perameter:
async: false
Solution 3:
XMLHttpRequest? try the "new" Fetch API. See: Browser Support for Fetch.
Ok, it is not supported in IE. In my defense, IE 11 is the only version of IE in support, and if they have IE 11, they probably have Edge.
This is a very simple example:
fetch("example.com")
.then(
function(response)
{
response.text()
.then(
function(result)
{
console.log(result);
}
);
}
);
Of couse, you would change "example.com" to your url.
The following is a more elaborate example, showcasing different options such as headers, request methods, redirections, control credentials (do you want to send cookies and http auth?) and catch exceptions. I recommend to read the MDN article on Using Fetch for more details.
Note: you can pass GET parameters in the url as expected. For a POST request you have to add the member body to the initialization of the Request object.
var headers = new Headers();
headers.append('X-Requested-With', 'XMLHttpRequest'); // #lies
var request = new Request(
"example.com",
{
method: "GET",
headers: headers,
mode: 'cors',
credentials: 'same-origin',
cache: 'default',
redirect: 'follow'}
);
fetch(request)
.then(
function(response)
{
// response status
console.log('ok: ' + response.ok);
console.log('status: ' + response.status); //e.g: 200
// response headers
console.log('X-Header: ' + response.headers.get('X-Header'));
// response url (redirections)
console.log(response.url);
// body as text
response.text()
.then(
function(result)
{
console.log(result);
}
);
}
)
.catch(
function(err)
{
console.log(err);
}
);
Note: in the examples I use the method text to get the text of the body of the response. You may also use the method json that will get the body and parse it directly. There is also a blob method, which can be useful if the response is multimedia among other things.
Post a Comment for "Call PHP Function In Ajax, Synchronous XMLHttpRequest Deprecarted"