Trigger php file to run - javascript

I'm trying to get couple of data from a mysql db.
How do I just trigger a php from a javascript file?
How do I fetch the results back from the javascript file?

Use Ajax to get data from mysql db.
Tutorial
http://www.w3schools.com/ajax/

You can use the script for your requirement:
<script type="text/javascript">
function showInformation()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
mlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
console.log(xmlhttp.responseText);
//this is the result you can get here.
}
}
xmlhttp.open("GET","xxx.php",true);
//xxx.php is the php file you want to fetch data from
xmlhttp.send();
}
</script>
Thanks
Here is the jquery script to do the same operation:
function getInformation(){
$.post('xxx.php',{'param':'..value..'},function(response){
console.log(response);
});
}
Note: before this include the jquery.js file in your script.
Thanks

Related

Javascript get plain text from server

I want to receive a plain text(or any document) from my server as a string variable and for example show it in alert I tried this solution
wordsurl = "http://alpha/test";
function ButtonClicked() {
showsentence(wordsurl)
}
function httpGet(theUrl)
{
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)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send(); // NS_ERROR_FAILURE is here
}
function showsentence(generatorurl) {
alert(httpGet(generatorurl));
}
but i'm getting NS_ERROR_FAILURE. Which is referring to the send.
Here is a
How to get plain text from a server?
Here is Server code
Ah, now I see..
You should serve your html page from a http:// server.. Not as a file file://
So setup a simple http server, and try to access it again. You could also serve it from the same server, like your app logic
URL not found.(Is http://alpha/test in your computer?)
Maybe you can change file:// to http://

How to get web url content using Javascript

I used Webrequest in c# to get url Content. Then how to get url content using JavaScript/ Jquery please help to get url content...
WebRequest request = WebRequest.Create ("https://api.pcpartpicker.com/api/2015.1/part/categories/?apikey=XXXXXXXXXXXXXX");//Any Other Google Https Address
WebResponse response = request.GetResponse ();
This code is working and i got data from that url and how we write this code in Javascript
It is not possible to get external webpages content in your site with javascript.
only js,images formats are allowed from external source into your site
You can try this code
function httpGet(theUrl)
{
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)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}

Catch every xhr request

I'm trying to achieve $.ajaxSuccess in javascript. I take some refrence from this question. The method I am using works fine when I use it every XHR request but I'm in a situation where there are scripts on the page which I have no control over and I want to be able to hook into any ajax request made by any library (or with vanilla js). So how to attached XMLHttpRequest.prototype.check to document so that we can catch every XHR request. Here is some working
//Script reference taken from w3schools link
XMLHttpRequest.prototype.check=function(){
this.addEventListener("loadend", function(){
alert(0);
}, false);
}
function loadXMLDoc()
{
var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.check()
xmlhttp.send();
}

XML http request with a URL

I want to retrieve an XML from a URL and store it in a variable xmlDoc.
I have the following:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","localhost:8080/rest/xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
But I am not getting the XML file, is there something I need to add?
The open method is passing false as the last parameter, making this a synchronous request. The OP's original code is correct, except for one thing: the URL.
xmlhttp.open("GET","http://localhost:8080/rest/xml",false);
Or if you want to make the URL agnostic to the protocol of the current page:
xmlhttp.open("GET","//localhost:8080/rest/xml",false);
before sending, attach a callback like this.
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseText;
}else if (xmlhttp.readyState==4 && xmlhttp.status != 200)
{
alert("error-" + xmlhttp.responseText);
}
}

How can javascript return a query parameter to client

Can javascript act like a web service and return a parameter value received in a query string to the client that posted the query? I am trying to return a query parameter in C# with no success. For example, if the query string is http://www.mypage/service?hubchallenge=1234 what javascript code would be used to return the value 1234 to the client without returning the web page itself?
You should have to you AJAX for it in your page. It cannot be done without passing a request from the client.
The below javascript code has to be placed in the page which send request.
function test()//the function can be called on events
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for other browsers
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET"," http://www.mypage/service?hubchallenge=1234",true);
xmlhttp.send();
}
In javascript, you can get the url into a string like this:
var urlString=document.URL;
then you can parse out parameters like
var qs=urlString.split("?")[1];
var qsArray=qs.split("&");

Categories