0

I understand I can't pass AJAX Vars directly to PHP, being a client versus server side script. But that being said, here's my code:

setInterval(function()
{   
    //do something after you receive the result
    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("message_area").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","messages.txt",true);
    xmlhttp.send();
}

I know I can indirectly process AJAX variables by POSTing them to a PHP page using AJAX, like so:

$.ajax (
{
    type: "POST",
    url: "decode.php",
}); 

I just need to know how to pass the contents of the "messages.txt" file used in the xmthttp.open call to a PHP file for further processing.

How can I do this?

1
  • It's not clear what the non-jQuery code means regarding this question-if you're simply asking how to use jQuery AJAX features, the documentation contains several examples. Also, you mention "AJAX variables" several times and that term does not have a established meaning. Commented Jan 13, 2014 at 16:53

3 Answers 3

2

If you are using pure javascript:

var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

or if you're using jquery, simply:

$.ajax (
{
    type: "POST",
    url: "decode.php",
    params: {param1: "val1", param2: "val2"}
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

Hope this helps :

$.get( "messages.txt", function( data ) { //Fetching contents of messages.txt file.
    $.ajax ({
        type: "POST",
        url: "decode.php",
        data: data, //passing contents of messages.txt file to decode.php
        success: function(result){
              alert(result);//Getting result from decode.php
        }
    }); 
});

cheers

2 Comments

Will this take the messages.txt result from the xmlhtttp request?
Probably a stupid question.. but i use $_POST['data'] to retrieve the var in the PHP script right? because its not working :/
1

Are you using jquery? You first code block is regular js, the second is jQuery's ajax short hand.

That said, if you want to POST data with ajax using jQuery, you would do something like the following.

var PostData = "something";

$.ajax({
    type: "POST", 
    url: "someurl", 
    data: PostData
}).done(function(returnData) {
    //process data returned from server, if there is any
});

1 Comment

I know this, however, xmlhttp.open retrieves a txt file, i need the contents of that file posted to a php page.. So is it possible to do something like Var PostData = xmlhttp.open(...)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.