2015-10-21 45 views
1

當使用下面的代碼時,我點擊提交按鈕,它會打開「domain.com/bhl.php?search=」,而不是像我需要的那樣傳遞$ _GET變量。onclick window.open不傳遞php變量

<form action="bhl.php" method="GET"> 
    <input name="search" type="text" placeholder="Type here"> 
    <input type="submit" onclick="window.open('bhl.php?search=<?php echo $_GET['search'] ?>','targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" /> 
</form> 
+0

刪除'onclick()',你可以在'bh1.php'中使用'$ _GET'。 –

+0

請注意,您以奇怪的方式使用引號。只是打開一堆他們將無法按預期工作(因爲它看起來像你正在關閉它)。 – FirstOne

+0

@FirstOne該代碼中沒有引號的問題。 – epascarello

回答

2

它不起作用,因爲您正在讀取頁面最初加載時的搜索參數值。頁面加載後PHP不運行。當點擊按鈕時,您需要使用JavaScript設置值。

<input type="submit" onclick="window.open('bhl.php?search=' + encodeURIComponent(this.form.search.value),'targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" /> 
+0

謝謝,這工作完美 –

0

由於頁面加載時沒有值,所以無法傳遞PHP變量。

嘗試用JavaScript發送值:

<form action="bhl.php" method="GET"> 
    <input id="search" type="text" placeholder="Type here"> 
    <input type="submit" onclick="window.open('bhl.php?search='+document.getElementById('search').value,'targetWindow','toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=700, height=500'); return false;" /> 
</form> 

雖然我會定義一個函數,那麼只需要調用該函數,而不是使用內嵌的JavaScript。