Ajax call to get Data - javascript

Dear I want to get Variable Value form PHP Value, and I want to write an ajax call in Java Script, tags,
How it is possible,
I want to get Value from get_result.php file,
and I have following code in get_result.php:
<?php
echo $val="abc";
?>
and I wrote follwing code in another file named ajax.php:
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="<?php echo "get_result.php"; ?>";
xmlhttp.open("GET", url, false);
xmlhttp.send(null);
var ok xmlhttp.responseText;
alert(ok);
</script>
is there any bug and Where?
I want to get value from get_result.php file and show this value in an Alert;

You can use jQuery. but for your code, you didn't assign the response
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="get_result.php";
xmlhttp.open("GET", url, false);
xmlhttp.send(null);
var ok = xmlhttp.responseText;
alert(ok);

The below code will help you in returning the value from the php page
<script>
function myfun()
{
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)
{
var demo=xmlhttp.responseText;
alert(demo);
}
}
xmlhttp.open("GET","result.php",true);
xmlhttp.send();
}
</script>
body
<body>
<input type="button" onclick="myfun();"/>
</body>

Related

Send ajax GET request if condition is met

This is my JS code. However, it only executed the last ajax request. Is there anyway to put them in If statement so that
if (main-content.html is loaded), it will execute xmlhttp.open("GET","main-content-city.php?q="+str,true)
xmlhttp.send();
elseif (child-content is loaded), execute: xmlhttp.open("GET","child-content-city.php?q="+str,true);
xmlhttp.send();
<script>
function sortResult(str)
{
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
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("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","main-content-city.php?q="+str,true);
xmlhttp.send();
xmlhttp.open("GET","child-content-city.php?q="+str,true);
xmlhttp.send();
}
</script>
My index.php file is like this and using GET method to navigate between main-content.html and child-content.html.
<?php
include('header.html');
include('main-content.html');
include('footer.html');
?>
My main-content and child-content is like this:
<div id="result">
Content displayed here
</div>
Take all the following code which will be identical for both ajax calls and make a separate function i..e
function ajaxCall (){
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("result").innerHTML=xmlhttp.responseText;
}
}
}
Then when you call ajaxCall you simply add relevant SEND:
function whatEver(){
if (main-content.html is loaded)
{ ajaxCall();
xmlhttp.open("GET","main-content-city.php?q="+str,true);
xmlhttp.send();
}
else if (child-content is loaded)
{
ajaxCall();
xmlhttp.open("GET","child-content-city.php?q="+str,true);
xmlhttp.send();
}
}

Javascript Ajax Call always returns readyState=1 and status=0

I am sending post request through AJAX as below.
I am always getting xmlhttp.readyState = 1 and xmlhttp.status= 0 . xmlhttp.responseText is always empty.
Could you please tell me what could be the problem ?
I expect xmlhttp.readyState==4 && xmlhttp.status==200
<script>
//Ajax to send request..
function sendPayment()
{
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()
{
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText=='1')
{
alert('success');
}
}
}
xmlhttp.open("POST","payments/callSSL.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(Id=100);
return false;
}
</script>
HTML PART
<input name="button" type="submit" id="button" value="Confirm" onclick="sendPayment()" />
If you are calling to your own another site,you have to give permission in your another site ie(http://my-other-site.com/payments/callSSL.php) to be accessed.
Put this header in your http://my-other-site.com/payments/callSSL.php
header('Access-Control-Allow-Origin: *');
for specific page
header('Access-Control-Allow-Origin: http://www.yourxmlrequestpage.php');
Hope this helps ,
thank you
you need to add a var as xmlhttp; on starting to get the status result please use below code i modify it,
<script>
//Ajax to send request..
function sendPayment()
{
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()
{
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText=='1')
{
alert('success');
}
}
}
xmlhttp.open("POST","payments/callSSL.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(Id=100);
return false;
}
</script>

Combine AJAX request to load new page with variables

I currently have three stored JS variables (wordChoiceOne, wordChoiceTwo, wordChoiceThree). Now that I have these stored, I want to run a function called saveUsername(). This function should do the following:
Take the jQuery variables above and pass them into PHP and load the file register-form.php into the div register-login-form
So I currently have the following AJAX request, but this only handles the reloading of the form. I've never combined jQuery with PHP before so how can I add the part in this to pass the three variables?
function saveUsername(wordChoiceOne, wordChoiceTwo, wordChoiceThree){
{
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("login-register-form").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","register-form.php",true);
xmlhttp.send();
}
What do I add to this to pass the three variables and convert them to PHP vars like with a $ sign?
function saveUsername(wordChoiceOne, wordChoiceTwo, wordChoiceThree){
{
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("login-register-form").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","register-form.php?one="+wordChoiceOne+"&two="+wordChoiceTwo+"&three="+wordChoiceThree,true);
xmlhttp.send();
}
Then PHP side, use $_GET['one'], $_GET['two'] and $_GET['three']

multiple ajax calls using only javascript

isn't it possible to execute multiple ajax calls using javascript only and without using Jquery. I tried the below javascript code with two ajax calls but the code is not working when I place the second ajax call
<script type="text/javascript">
var class1;
var class2;
var sec1;
var sec2;
function func()
{
class1 = document.getElementById('selcla');
class2 = class1.options[class1.selectedIndex].value;
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("secdiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsec.php?q="+class2,true);
xmlhttp.send();
}
function funcsec()
{
sec1 = document.getElementById('selsec');
sec2 = sec1.options[sec1.selectedIndex].value;
alert("selecting class and section details " + class2 + " " + sec2 );
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)
{
alert("successfully received " );
document.getElementById("studiv").innerHTML=xmlhttp.responseText;
}
else
alert("unsuccessful ajax second call ");
}
xmlhttp.open("GET","getstu.php?x="+class2"&y="+sec2,true);
xmlhttp.send();
}
It is, but you need a different "xmlhttp" handler for each request you make.
Setup a new xmlhttp object and make a second request with the new object.
My suggestion is to break out the part with you initializing the xmlhttp object, into a standalone function, and use it to create a few instances of those objects.
However I must advise against such an approach. It is better to use a library for ajax requests.

Automatically pass the retrieved value from a javascript function to another javascript function?

I have two inline java script functions, one intended to get a token value using xhr and second one will use that value to get the full response from server. But when i uploaded the same on server nothing happened.
Here goes the sample code:
function getUser(user)
{
var xmlhttp;
var result,x,i;
var uservalue=user;
var url="http://abc.xyz.com:8090/uauth/" +uservalue;
///here starts the getToken method..
var tokensave=function getToken(uservalue)
{
var xmlhttp;
var token,a,b;
var url="http://abc.xyz.com:8090/uauth/" +uservalue;
var data="op=login&pass=" +uservalue;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST",url, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
token="";
a=xmlDoc.getElementsByTagName("token");
token=token + a.childNodes.nodeValue;
document.getElementById("myDiv").innerHTML=token;
}
};
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(data);
return token;
}
var header="Xyz-Authorization: "+tokensave;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
result="";
x=xmlDoc.getElementsByTagName("response");
for (i=0;i<x.length;i++)
{
result=result + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myNewDiv").innerHTML=result;
}
};
xmlhttp.setRequestHeader("Xyz-Authorization: ", +tokensave);
xmlhttp.send();
}
HTML CODE:
<div id="myDiv"></div>
<br />
<br />
<div id="myNewDiv"></div >
<br />
<form>
<input name="textbox" id="textbox" type="text" />
<input name="buttonExecute" onclick="getUser(document.getElementById('textbox').value)" type="button" value="Get User" />
</form>
What's wrong with this apparoach?
Thanks in advance for your valuable help...
mrana
You need to use a return token in the first function and use it like this getUser(getToken(user))
Just create a global variable, and you set change the value inside de function but it's still accesible from a global scope for all the functions you want.
var data = false;
function getData() {
// some ajax...
data = {cool:true};
}
function foo () {
if(data.cool) {
alert (data.cool)
}
}

Categories