2

Need some help here. I have a main page index.php with a simple js function, say function1() which upon clicking, opens a page, say test.php as a pop-up window, which includes contents from another page p1.php. I need to condition it so that when user clicks function2(), it opens same page test.php but with different content from page p2.php.

index.php code is below;

<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>

I am making the following conditional statements in page 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;
?>

The result I get is that the contents of both p1.php and p2.php are reflecting in test.php (our pop-up window). How do I condition it so that only p1.php content is reflected onclicking function1() and p2.php content is reflected onclicking functions2(). Any help is appreciated.

6
  • where are function1 and function2 even defined? - By the way, I'm surprised you get either p1.php or p2.php with that PHP code, seeing as both those conditions would never be true Commented Jul 29, 2017 at 8:18
  • yes they are defined in index.php file as - function1(theURL,winName,features) { window.open(theURL,winName,features); } function2(theURL,winName,features) { window.open(theURL,winName,features); } Commented Jul 29, 2017 at 8:21
  • so, they are PHP functions ... you need to think how PHP works ... it pre-processes the page, then sends HTML to the browser ... the PHP functions are not accessible to javascript Commented Jul 29, 2017 at 8:22
  • function1() and function2() are js functions to open popwindow. Commented Jul 29, 2017 at 8:25
  • can you show them please - in the question ... and, there's no difference in what they send to the server, so how would the server know which button was pressed Commented Jul 29, 2017 at 8:26

1 Answer 1

1

add a query/search string to the URL in the request i.e. like ?name=value

you don't need two different JS functions by the way - in fact, you don't need any JS function for what you are doing

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;
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Well. Had to do minor changes in your code, but concept wise it works!!! Thanks a lot for your help :)
replace var page with $page / remove $a = include... / replace echo $a with include "p1.php" and so on. This will reflect specific page on clicking the link in index.php containing the php function (query/search string to the URL).
Yes. My PHP is rusty

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.