2017-07-29 51 views
2

需要一些幫助。我有一個帶有簡單js函數的主頁index.php,比如說function1(),它在點擊後打開一個頁面,將test.php稱爲彈出窗口,其中包含來自另一頁p1.php的內容。我需要調整它,以便當用戶單擊function2()時,它會打開相同的頁面test.php,但頁面p2.php中的內容不同。在php中調用js函數

index.php代碼在下面;

<script language="JavaScript" type="text/JavaScript"> 
    <!-- 
    function function1(theURL,winName,features) { 
    window.open(theURL,winName,features); 
    } 
    //--> 
    <!-- 
    function function2(theURL,winName,features) { 
    window.open(theURL,winName,features); 
    } 
    //--> 
    </script> 

<td> 
<p><a href="#" class="left-content" onclick="function1('test.php','','scrollbars=yes,width=auto,height=600')">Page 1 contents</a></p> 
</td> 

<td> 
    <p><a href="#" class="left-content" onclick="function2('test.php','','scrollbars=yes,width=auto,height=600')">Page 2 contents</a></p> 
    </td> 

我的頁面進行以下條件語句test.php的

<?php 

    $a = include ("p1.php"); 
    $b = include ("p2.php"); 

    if(isset($_GET['<script>function1()</script>'])) 
     echo $a; 

    else if(isset($_GET['<script>function2()</script>'])) 
     echo $b; 
?> 

結果我得到的是,無論p1.php和p2.php的內容test.php的是反映(我們的彈出窗口)。如何對它進行調整,以便僅在點擊function1()時反映p1.php內容,並在點擊functions2()時反映p2.php內容。任何幫助表示讚賞。

+0

在哪裏'function1'和'function2'甚至定義? - 順便說一句,我很驚訝你得到p1.php或p2.php與該PHP代碼,看到這兩個條件永遠不會是真的 –

+0

是的,他們在index.php文件中定義爲 - function1(theURL,winName ,功能){window.open(theURL,winName,features); } function2(theURL,winName,features){window.open(theURL,winName,features); } –

+0

因此,它們是PHP函數...您需要考慮PHP的工作原理...它預先處理頁面,然後將HTML發送到瀏覽器... PHP函數無法訪問javascript –

回答

1

在請求中添加查詢/搜索字符串的URL即像?name=value

你不要被方式需要兩個不同的JS功能 - 事實上,你不需要任何JS功能你在做

的index.php

<td> 
    <p><a href="#" class="left-content" onclick="window.open('test.php?v=1','','scrollbars=yes,width=auto,height=600')">Page 1 contents</a></p> 
</td> 
<td> 
    <p><a href="#" class="left-content" onclick="window.open('test.php?v=2','','scrollbars=yes,width=auto,height=600')">Page 2 contents</a></p> 
</td> 

test.php的

<?php 
    $a = include("p1.php"); 
    $b = include("p2.php"); 

    var page = isset($_GET['v']) ? $_GET['v'] : 0; 
    if (page == "1") 
     echo $a; 
    else if (page == "2") 
     echo $b; 
?> 
+0

好吧。必須在你的代碼中做一些細微的修改,但它的理念明智!非常感謝您的幫助:) –

+0

我的代碼哪部分出錯了?對於將來的讀者:p –

+0

用$ page替換var頁面/刪除$ a = include ... /用包含「p1.php」替換echo $ a等等。這將反映點擊index.php中包含php函數(查詢/搜索字符串到URL)的鏈接的特定頁面。 –