2011-12-23 51 views
0

到目前爲止,我已經使用這個code.It正在工作,當我從aspx發送數據到aspx。如何從aspx發送數據到php頁面

但ASPX到PHP它不工作的情況下.....

protected void Button1_Click(object sender, EventArgs e) 
{ 
    RemotePost myremotepost = new RemotePost(); 
    myremotepost.Url = "http://172.16.126.32/Riyas/marggroup.com/get-current-openings.php"; 
    myremotepost.Add("field1", TextBox1.Text); 
    myremotepost.Post(); 
} 
public class RemotePost 
{ 
    private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection(); 


    public string Url = ""; 
    public string Method = "post"; 
    public string FormName = "form1"; 

    public void Add(string name, string value) 
    { 
     Inputs.Add(name, value); 
    } 

    public void Post() 
    { 
     System.Web.HttpContext.Current.Response.Clear(); 

     System.Web.HttpContext.Current.Response.Write("<html><head>"); 

     System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName)); 
     System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url)); 
     for (int i = 0; i < Inputs.Keys.Count; i++) 
     { 
      System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]])); 
     } 
     System.Web.HttpContext.Current.Response.Write("</form>"); 
     System.Web.HttpContext.Current.Response.Write("</body></html>"); 

     System.Web.HttpContext.Current.Response.End(); 
    } 
} 

在PHP的情況下,我必須使用此代碼:

<script runat="server"> 

    function formload() 
    { 
     alert("its working"); 
     if(Request.Form["field1"] != null){ 
      alert("its working"); 
      Response.Write("field1 : " + Request.Form["field1"] + "</br>"); 
     } 
     if(Request.Form["field2"] != null){ 
      Response.Write("field2 : " +Request.Form["field2"] + "</br>"); 
     } 
    } 
    </script> 
    </head> 
<body onload="JavaScript:formload()"> 
<script language="JavaScript">// change to text/javascript or even remove, no effect 
window.onload = function() { 
    formload(); 
}; 
</script> 
</body> 

我的目的是我想將數據從aspx發送到不在查詢字符串中的php。

回答

0

有趣......

在onload功能

,您呼叫formload(),這是一個服務器端的功能,是不是?

這是不正確的,在客戶的網站,你應該只調用客戶端站點javascript函數。

如果你想發佈信息到另一臺服務器,不管它是php/asp.net/jsp。

只需使用一種形式....

在remotepost類的帖子功能

,你沒有張貼到你的遠程服務器...只是產生一些HTML標籤,並在頁面加載功能,自動提交表格,

這不好,但應該工作。

先糾正第一個問題,然後看看它是怎麼回事。

+0

我是新來的這個請幫我通過代碼 – 2011-12-23 10:43:10

0

如果不在查詢字符串中,爲什麼不將它作爲POST數據發送呢?

+0

只有我試過你有什麼想法嗎? – 2011-12-23 09:39:15

1

修改您的PHP腳本以包含下面給出的代碼,看看它是否工作。除了給定的兩行外,您不需要任何其他內容。

<?php 

print_r($_POST);