最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Jquery Onclick Send data to new opened window - Stack Overflow

programmeradmin3浏览0评论

I know this is a mon 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.

I know this is a mon 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.

Share Improve this question edited Sep 8, 2012 at 19:35 Ashley Strout 6,2686 gold badges27 silver badges48 bronze badges asked Sep 8, 2012 at 18:54 Kaushil RambhiaKaushil Rambhia 1952 gold badges6 silver badges19 bronze badges 2
  • 1 what if you wrap data in "? $.post(myurl,{"data":myVar},function(){}); – nuala Commented Sep 8, 2012 at 19:28
  • 1 Also you don't have to provide a success function if you're not using it function(){} it's an optional parameter indicated by the [ ] brackets in the documentation: api.jquery./jQuery.post – nuala Commented Sep 8, 2012 at 19:29
Add a ment  | 

2 Answers 2

Reset to default 4

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;

   });

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 pletely, 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
发布评论

评论列表(0)

  1. 暂无评论