2013-04-08 181 views
-1

我需要編寫一個簡單的網頁,可以通過參數的POST是更新得到最後的檢索放慢參數值:如何通過參數,將其發送POST請求更新網頁,並通過發送GET請求,它

  1. 使用參數向頁面發送POST請求 - 將不斷更新網頁。
  2. 發送GET請求的頁面將返回參數

例如最後獲取的值(也許每個請求是不同的會話):

POST /mypage.asp?param1=Hello 

GET /mypage.asp >> Response: Hello 

POST /mypage.asp?param1=Changed 

GET /mypage.asp >> Response: Changed 

回答

0
<% 
dim fileSystemObject,Text_File,Text_File_Content,NewValue 

NewValue=Request.QueryString("MyParam") 
set fileSystemObject=Server.CreateObject("Scripting.FileSystemObject") 

If NewValue <> "" Then 
    set Text_File=fileSystemObject.CreateTextFile("C:\MyPath\myTextFile.txt") 
    Text_File.write(NewValue) 
    Text_File.close 
End If 

if fileSystemObject.FileExists("C:\MyPath\myTextFile.txt")=true then 
    set Text_File=fileSystemObject.OpenTextFile("C:\MyPath\myTextFile.txt",1,false) 
    Text_File_Content=Text_File.ReadAll 
    Text_File.close 
else 
    set Text_File=fileSystemObject.CreateTextFile("C:\MyPath\myTextFile.txt") 
    Text_File.write("Please send GET request to this page with paramter MyParam!") 
    Text_File.close 
    set Text_File=fileSystemObject.OpenTextFile("C:\MyPath\myTextFile.txt",1,false) 
    Text_File_Content=Text_File.ReadAll 
    Text_File.close 
end if 

Response.Write(Text_File_Content) 
%> 
0

使用$ _SESSION

session_start(); 

if($_SERVER['REQUEST_METHOD'] == "POST") 
    $_SESSION['last_val'] = $_POST['some_val']; 
} 

if($_SERVER['REQUEST_METHOD'] == "GET") 
    echo $_SESSION['last_val']; 
} 

Learn more about SESSION

+0

我需要的網頁返回之後的參數的值時,我會在你的OP – SharonBL 2013-04-08 16:10:19

+0

你說'POST'到該頁面 - 更新它。 GET - 檢索上次POST的參數 – 2013-04-08 16:10:43

+0

POST發送GET請求,它 – SharonBL 2013-04-08 16:11:46

0

Evan的回答是在概念上是正確的,但我認爲他沒有解決不同的會話,也沒有使用「經典ASP」(vbscript或jscript)。

要在會話和請求之間保持一個值,您需要某種形式的存儲。可能最簡單的選項是一個應用程序變量(我在下面顯示)。其他選項是「線程安全」存儲,如廣泛使用的CAPROCK DICTIONARY或數據庫。

代碼:

<%@ Language="VBScript" %> 
<% 
If Request.ServerVariables("REQUEST_METHOD")= "POST" Then 
Application.Lock 
Application("StoredValue") = Request.Form("param1") 
Application.Unlock 
Else 
Application.Lock 
Response.Write Application("StoredValue") 
Application.Unlock 
End If 
%>