Kaushil Rambhia
Kaushil Rambhia

Reputation: 195

Jquery Onclick Send data to new opened window

I know this is a common question but I can't find any answer. I have a PHP file which contains a link. When that link is clicked, I want to open another page in a new window, and on this newly opened page I want to get the ID attribute of the link clicked on the previous page.

Current page= 'patientreg.php'
Newpage to open in new window= 'patient_history.php'
Post data from current page to new loaded page.

Current page Code:

HTML:

<a id="37" onclick="phistory(this.id);" href="#">View history</a>  

JavaScript:

function phistory(myVar) {  
    var myurl = '../main/patient_history.php';  
    $.post(myurl,{data:myVar},function(){});  
    window.open(myurl,'_blank');                
} 

New page code:

$phid = $_REQUEST["data"];  
echo phid;

The problem is that it is not echoing the data on the next page, but I can see echo with the full new page in the Firebug response tab of the current page.

Upvotes: 0

Views: 12467

Answers (2)

andres descalzo
andres descalzo

Reputation: 14967

try this, so using a form to send data to your page, and you can have more verbose the anchor element:

HTML:

<form id="myform" action="../main/patient_history.php" method="POST">
   <input type="hidden" name="data" id="data" />
</form> 


<a data-idref="37" class="see-history" href="#">View Histroy</a>

Javascript:

$(function() {

   $('.see-history').bind('click', function(event) {
      try { 
         window.open('about:blank', 'mynewwindow');   
         $('#myform').find('#data').val($(this).data('idref'));
         $('#myform').attr('target', 'mynewwindow').submit();
      } catch(e) {}

      return false;

   });

});

Edit

if you need to open a new window for each story, you can create a new window with the name of the element id:

   $('.see-history').bind('click', function(event) {
      var id = $(this).data('idref');  
      try { 
         window.open('about:blank', 'mynewwindow_' + id);   
         $('#myform').find('#data').val(id);
         $('#myform').attr('target', 'mynewwindow_' + id).submit();
      } catch(e) {}

      return false;

   });

Upvotes: 4

Ashley Strout
Ashley Strout

Reputation: 6258

Try this instead. For your JavaScript onclick handler:

function phistory(myVar) {  
    var myurl = '../main/patient_history.php?data='+myVar;
    window.open(myurl);                
}

Then, in your PHP file:

$phid = $_REQUEST["data"];  
echo $phid;

Your problem is that you are misunderstanding the use of jQuery's .post() method. It simply retrieves the page (with the specified POST parameters) - it does not modify the page such that when you open it in a new window, it will have your specified ID parameter. I switched it so that it no longer uses jQuery's .post(), but simply uses the GET parameters by opening the new window with ?data=[YOUR-ID]. The PHP, you'll notice, stays mostly the same, but I changed the echo statement to include the $ sign before the variable name. That might have been a typo just in the question, but if that's in your code, it's part of the problem.

Another possibility is to remove the ID from the query string completely, and instead store it as a global variable on the first page, then retrieve that with the window.opener property on the second page, like so:

First page:

var pid=0;
function phistory(myVar) {  
    pid=myVar;
    window.open("../main/patient_history.php");                
}

Second page (JavaScript):

pid=window.opener["pid"];
alert(pid); //Just to check that it's working

Upvotes: 1

Related Questions