如何将值从AJAX Javascript传递到PHP文件

时间:2012-06-14 06:52:48

标签: php javascript ajax

我想使用下面的脚本将值从AJAX文件传递给PHP,但它失败了。这样做的正确方法是什么?感谢

示例代码如下:

function createNewWindow()
{
var newWindowModel = new DHTMLSuite.windowModel({windowsTheme:true,id:'newWindow1',title:'Response Time to Invitation',xPos:130,yPos:400,minWidth:100,minHeight:100 } );
newWindowModel.addTab({ id:'myTab1',htmlElementId:'myTab1',tabTitle:'TAB',textContent:'Send data', contentUrl:'load.php?loadNo:loadNo' } );
var newWindowWidget = new DHTMLSuite.windowWidget(newWindowModel);
newWindowWidget.init();
}

3 个答案:

答案 0 :(得分:0)

传递价值?你的意思是参数?如果是的话:

  1. 首先创建一个AJAX obj:var http = new XMLHttpRequest();
  2. GET方法:

    var url = "load.php";
    var params = "loadNo=loadNo&param=value";
    http.open("GET", url+"?"+params, true);
    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(null);
    

    POST方法:

    var url = "laod.php";
    var params = "loadNo=loadNo&param=value";
    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);
    

答案 1 :(得分:0)

   if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
        var x=xmlhttp.responseText;
                    alert(x);
         }
}
}
xmlhttp.open("GET","load.php?loadNo="+loadNo+"&param="+value,true);
xmlhttp.send();

答案 2 :(得分:0)

使用Jquery

执行AJAX请求的简单方法
var request = $.ajax({
  url: "script.php", // script path goes here
  type: "GET",
  data: {id : param}, // Parameters go here
  dataType: "html"
});

request.done(function(msg) {
  $("#log").html( msg ); // On success 
});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus ); // On failure
});