2017-04-24 123 views
0

我使用window.open()方法打開一個新窗口並使用localStorage.setItem()方法傳遞一個Javascript變量(String)。 在打開的新窗口中,使用localStorage.getItem()方法檢索傳遞的變量。 現在我需要這個變量的SQL查詢使用PHP, 類似於「SELECT rowname from tablename WHERE id = TheJSVariable」將JavaScript變量(String)值轉換爲PHP變量

但我找不到任何解決方案。

有什麼建議嗎? 謝謝

+0

你是如何將查詢發送到PHP?使用Ajax或表單? – Manngo

+0

通過ajax發送此值到同一頁面或不同的php頁面並使用$ _POST獲取該值(使用ajax post方法) –

+0

Manngo:我不使用Ajax或表單,只使用簡單的查詢,然後使用mysqli_query獲取結果。 活着死:我怎樣才能在window.open方法中使用ajax post發送Javascript字符串值?任何鏈接?謝謝(我是新手) – Foobarer

回答

1

當用window.open打開時,你可以添加你的參數作爲通用獲取參數。即:

window.open(「mysite.com/page.php?param=123」);

然後在page.php文件就可以讀取參數值的方式:

$param = $_GET['param']; 

更新:

如果你想POST請求,您可以添加(隱藏)表格形式字段包含您想要傳遞的值並從代碼提交該表單。喜歡的東西:

<form id="TheForm" method="post" action="test.asp" target="TheWindow"> 
<input type="hidden" name="something" value="something" /> 
<input type="hidden" name="more" value="something" /> 
<input type="hidden" name="other" value="something" /> 
</form> 

<script type="text/javascript"> 
window.open('', 'TheWindow'); 
document.getElementById('TheForm').submit(); 
</script> 

Window.open and pass parameters by post method

+0

謝謝,以及幾個問題: (1)如果param是一個變量,即var x =「String」;我應該輸入window.open(「mysite.com/page.php?param=」+ x);? (2)如果我想要一個POST而不是GET,那麼使用window.open編寫它的方式是什麼? – Foobarer

+0

更新了我的答案,是的,window.open應該生成類似於您在評論中所寫的內容 – MilanG

+0

謝謝! 順便說一下,我的錯誤,我一直使用window.location而不是window.open,所以我應該將所有window.location切換到window.open,對吧?因爲window.location沒有能力添加參數?還是呢? :) – Foobarer