简体   繁体   English

如何将AJAX变量传递给PHP

[英]How to pass AJAX variables to PHP

I understand I can't pass AJAX Vars directly to PHP, being a client versus server side script. 我知道我不能将AJAX Vars直接传递给PHP,因为它是客户端还是服务器端脚本。 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将它们发布到PHP页面来间接处理AJAX变量,如下所示:

$.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. 我只需要知道如何将xmthttp.open调用中使用的“ messages.txt”文件的内容传递给PHP文件,以进行进一步处理。

How can I do this? 我怎样才能做到这一点?

If you are using pure javascript: 如果您使用的是纯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: 或者,如果您使用的是jQuery,则只需:

$.ajax (
{
    type: "POST",
    url: "decode.php",
    params: {param1: "val1", param2: "val2"}
}); 

Are you using jquery? 您在使用jQuery吗? You first code block is regular js, the second is jQuery's ajax short hand. 您的第一个代码块是常规js,第二个是jQuery的ajax简写。

That said, if you want to POST data with ajax using jQuery, you would do something like the following. 就是说,如果您想使用jQuery使用ajax来发布数据,则可以执行以下操作。

var PostData = "something";

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

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 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM